Skip to content

Commit

Permalink
sdk-implement-new-way-to-parse-private-key (#55)
Browse files Browse the repository at this point in the history
Signed-off-by: michalrozekariane <michal.rozek@arianelabs.com>
  • Loading branch information
michalrozekariane authored Apr 23, 2024
1 parent 259ac95 commit f681a2b
Show file tree
Hide file tree
Showing 28 changed files with 121 additions and 209 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ Create new instance of `HederaNFTSDK` in the following parameters:
```typescript
type HederaNFTSDKType = {
accountId: string;
privateKey: string;
privateKey: PrivateKey;
network: Network;
localNode?: LocalNode;
localMirrorNode?: string;
Expand Down Expand Up @@ -713,14 +713,14 @@ Create collection method takes in the following parameters:
type CreateCollectionType = {
collectionName: string;
collectionSymbol: string;
treasuryAccountPrivateKey?: string;
treasuryAccountPrivateKey?: PrivateKey;
treasuryAccount?: string;
keys?: CreateCollectionKeysType;
maxSupply?: number;
customFees?: CustomFeeType[];
expirationTime?: Date;
autoRenewAccount?: string;
autoRenewAccountPrivateKey?: string;
autoRenewAccountPrivateKey?: PrivateKey;
autoRenewPeriod?: number;
memo?: string;
};
Expand Down Expand Up @@ -774,7 +774,7 @@ Estimate create collection in dollars method takes in the following parameters:
type EstimateCreateCollectionInDollarsType = {
collectionName: string;
collectionSymbol: string;
treasuryAccountPrivateKey?: string;
treasuryAccountPrivateKey?: PrivateKey;
treasuryAccount?: string;
keys?: CreateCollectionKeysType;
customFees?: CustomFeeType[];
Expand Down Expand Up @@ -821,7 +821,7 @@ Estimate create collection in hbar method takes in the following parameters:
type EstimateCreateCollectionInDollarsType = {
collectionName: string;
collectionSymbol: string;
treasuryAccountPrivateKey?: string;
treasuryAccountPrivateKey?: PrivateKey;
treasuryAccount?: string;
keys?: CreateCollectionKeysType;
customFees?: CustomFeeType[];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hashgraph/hedera-nft-sdk",
"version": "3.0.4",
"version": "3.0.5",
"description": "NFT SDK for Hedera Hashgraph",
"author": "Michiel Mulders",
"license": "Apache License",
Expand Down
35 changes: 0 additions & 35 deletions src/helpers/get-private-key-from-string.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import type {
MetadataError,
MetadataOnChainObjects,
} from './types/hip412-validator';
import { PrivateKey } from '@hashgraph/sdk';

import { HederaNFTSDK } from './nftSDKFunctions';
import { FeeFactory } from './feeFactory';
Expand Down Expand Up @@ -135,4 +136,7 @@ export {
PinataService,
AWSService,
MockStorageService,

// hashgraph/sdk
PrivateKey,
};
11 changes: 4 additions & 7 deletions src/nftSDKFunctions/create-collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { AccountId, PrivateKey, TokenCreateTransaction, TokenSupplyType, TokenTy
import { dictionary } from '../utils/constants/dictionary';
import { CreateCollectionType } from '../types/create-collection';
import { validatePropsForCreateCollection } from '../utils/validate-props';
import { getPrivateKeyFromString } from '../helpers/get-private-key-from-string';

export const createCollectionFunction = async ({
client,
Expand Down Expand Up @@ -52,16 +51,14 @@ export const createCollectionFunction = async ({
memo,
});

const treasuryAccountId = treasuryAccount ? AccountId.fromString(treasuryAccount) : client.getOperator()!.accountId;
const treasuryAccountPrivateKeyId = treasuryAccountPrivateKey
? getPrivateKeyFromString(treasuryAccountPrivateKey)
: getPrivateKeyFromString(myPrivateKey);
const treasuryAccountId = treasuryAccount ? treasuryAccount : client.getOperator()!.accountId;
const treasuryAccountPrivateKeyId: PrivateKey = treasuryAccountPrivateKey ? treasuryAccountPrivateKey : myPrivateKey;

let transaction = new TokenCreateTransaction()
.setTokenName(collectionName)
.setTokenSymbol(collectionSymbol)
.setTokenType(TokenType.NonFungibleUnique)
.setSupplyKey(keys?.supply || getPrivateKeyFromString(myPrivateKey))
.setSupplyKey(keys?.supply || myPrivateKey)
.setTreasuryAccountId(treasuryAccountId);

if (keys?.admin) {
Expand Down Expand Up @@ -122,7 +119,7 @@ export const createCollectionFunction = async ({
}

if (autoRenewAccountPrivateKey) {
signTx = await transaction.sign(getPrivateKeyFromString(autoRenewAccountPrivateKey));
signTx = await transaction.sign(autoRenewAccountPrivateKey);
}

const txResponse = await signTx.execute(client);
Expand Down
37 changes: 23 additions & 14 deletions src/nftSDKFunctions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* limitations under the License.
*
*/
import { Client, NftId } from '@hashgraph/sdk';
import { Client, NftId, PrivateKey } from '@hashgraph/sdk';
import { CreateCollectionKeysType, CustomFeeType } from '../types/create-collection';
import { Network } from '../types/mint-token';
import { createCollectionFunction } from './create-collection';
Expand All @@ -30,18 +30,17 @@ import { estimateNftMintingInHbar } from './estimate-nft-minting-in-hbar';
import { estimateNftMintingInDollars } from './estimate-nft-minting-in-dollars';
import { estimateCreateCollectionInDollars } from './estimate-create-collection-in-dollars';
import { estimateCreateCollectionInHbar } from './estimate-create-collection-in-hbar';
import { getPrivateKeyFromString } from '../helpers/get-private-key-from-string';

export class HederaNFTSDK {
accountId: string;
privateKey: string;
privateKey: PrivateKey;
client: Client;
network: Network;
mirrorNodeUrl?: string;

constructor(
accountId: string,
privateKey: string,
privateKey: PrivateKey,
network: Network,
localNode?: LocalNode,
localMirrorNode?: string,
Expand Down Expand Up @@ -76,14 +75,14 @@ export class HederaNFTSDK {
}: {
collectionName: string;
collectionSymbol: string;
treasuryAccountPrivateKey?: string;
treasuryAccountPrivateKey?: PrivateKey;
treasuryAccount?: string;
keys?: CreateCollectionKeysType;
maxSupply?: number;
customFees?: CustomFeeType[];
expirationTime?: Date;
autoRenewAccount?: string;
autoRenewAccountPrivateKey?: string;
autoRenewAccountPrivateKey?: PrivateKey;
autoRenewPeriod?: number;
memo?: string;
}) {
Expand Down Expand Up @@ -115,7 +114,7 @@ export class HederaNFTSDK {
}: {
collectionName: string;
collectionSymbol: string;
treasuryAccountPrivateKey?: string;
treasuryAccountPrivateKey?: PrivateKey;
treasuryAccount?: string;
keys?: CreateCollectionKeysType;
customFees?: CustomFeeType[];
Expand All @@ -140,7 +139,7 @@ export class HederaNFTSDK {
}: {
collectionName: string;
collectionSymbol: string;
treasuryAccountPrivateKey?: string;
treasuryAccountPrivateKey?: PrivateKey;
treasuryAccount?: string;
keys?: CreateCollectionKeysType;
customFees?: CustomFeeType[];
Expand Down Expand Up @@ -176,15 +175,15 @@ export class HederaNFTSDK {
amount: number;
batchSize?: number;
metaData: string;
supplyKey?: string;
supplyKey?: PrivateKey;
}) {
return mintSharedMetadataFunction({
client: this.client,
tokenId,
amount,
batchSize,
metaData,
supplyKey: supplyKey ? getPrivateKeyFromString(supplyKey) : getPrivateKeyFromString(this.privateKey),
supplyKey: supplyKey ? supplyKey : this.privateKey,
});
}

Expand All @@ -197,28 +196,38 @@ export class HederaNFTSDK {
}: {
tokenId: string;
batchSize?: number;
supplyKey: string;
supplyKey: PrivateKey;
pathToMetadataURIsFile?: string;
metadata?: string[];
}) {
return mintUniqueMetadataFunction({
client: this.client,
tokenId,
batchSize,
supplyKey: getPrivateKeyFromString(supplyKey),
supplyKey: supplyKey,
pathToMetadataURIsFile,
metadataArray: metadata,
});
}

increaseNFTSupply({ nftId, amount, batchSize = 5, supplyKey }: { nftId: NftId; amount: number; batchSize?: number; supplyKey?: string }) {
increaseNFTSupply({
nftId,
amount,
batchSize = 5,
supplyKey,
}: {
nftId: NftId;
amount: number;
batchSize?: number;
supplyKey?: PrivateKey;
}) {
return increaseNFTSupply({
client: this.client,
network: this.network,
nftId,
amount,
batchSize,
supplyKey: supplyKey ? getPrivateKeyFromString(supplyKey) : getPrivateKeyFromString(this.privateKey),
supplyKey: supplyKey ? supplyKey : this.privateKey,
mirrorNodeUrl: this.mirrorNodeUrl,
});
}
Expand Down
8 changes: 4 additions & 4 deletions src/test/__mocks__/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import { Client, PrivateKey } from '@hashgraph/sdk';
*/
export const myAccountId = '0.0.12345';
export const mySecondAccountId = '0.0.123';
export const myPrivateKey = '123e020100300506032b657004220420f8e9f8de8f7e06f7e9f8de8f7e06f7e9f8de8f7e06f7e9f8de8f7e06f7e9f8de';

export const mySecondPrivateKey = '1230020100300706052b8104000a042204206fc9f9a2aa45924d617effa5778afd2a617cef8f1b527fe50d96042731df6b42';
export const myPrivateKey = PrivateKey.generateED25519();
export const mySecondPrivateKey = PrivateKey.generateED25519();
export const treasuryAccountPrivateKey = PrivateKey.generateED25519();

export const pathToOneLineCSV = 'src/test/__mocks__/csv/csv-example-one-line.csv';
export const pathToRowCSV = 'src/test/__mocks__/csv/csv-example-rows.csv';
Expand Down Expand Up @@ -66,4 +66,4 @@ export const MOCK_CLIENT = {} as Client;
export const MOCK_META_DATA = 'meta1';
export const MOCK_TOKEN_ID = '0.0.123';
export const MOCK_SERIAL = 2;
export const MOCK_SUPPLY_KEY = PrivateKey?.fromString(myPrivateKey);
export const MOCK_SUPPLY_KEY = myPrivateKey;
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { LONG_E2E_TIMEOUT, MIRROR_NODE_DELAY } from '../__mocks__/consts';
import { calculateRarityFromOnChainData } from '../../rarity';
import { LINK_TO_JSON_OBJECT_WITHOUT_ERRORS, nftSDK, operatorPrivateKey } from './e2e-consts';
import { PrivateKey } from '@hashgraph/sdk';
import { LINK_TO_JSON_OBJECT_WITHOUT_ERRORS_ATTRIBUTES } from '../__mocks__/linkToJsonObjectWithoutErrorsAttributes';

describe('calculateRiskScoreFromTokenIdE2E', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import { estimateCreateCollectionInHbar } from '../../../nftSDKFunctions/estimat
import { Hbar, TokenCreateTransaction, TokenType } from '@hashgraph/sdk';
import { roundToPrecision } from '../../helpers/round-to-precision';
import { isWithinAcceptableDifference } from '../../helpers/is-within-acceptable-difference';
import { getPrivateKeyFromString } from '../../../helpers/get-private-key-from-string';

describe('estimateCreateCollectionInHbarE2E', () => {
it('should work properly with default values', async () => {
Expand All @@ -41,7 +40,7 @@ describe('estimateCreateCollectionInHbarE2E', () => {
.setTokenName(name)
.setTokenSymbol(symbol)
.setTokenType(TokenType.NonFungibleUnique)
.setSupplyKey(getPrivateKeyFromString(operatorPrivateKey))
.setSupplyKey(operatorPrivateKey)
.setTreasuryAccountId(operatorAccountId);
const txResponse = await createToken.execute(nftSDK.client);
const record = await txResponse.getRecord(nftSDK.client);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import { LONG_E2E_TIMEOUT, MIRROR_NODE_DELAY } from '../../__mocks__/consts';
import { nftSDK, operatorPrivateKey } from '../e2e-consts';
import { NftId, TokenId, TokenNftInfoQuery } from '@hashgraph/sdk';
import { getPrivateKeyFromString } from '../../../helpers/get-private-key-from-string';

describe('increaseNFTSupply function e2e', () => {
const testCases = [{ amount: 1 }, { amount: 3 }, { amount: 10 }];
Expand Down
5 changes: 3 additions & 2 deletions src/test/e2e/e2e-consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
import 'dotenv/config';
import { FeeFactory } from '../../feeFactory';
import { HederaNFTSDK } from '../../nftSDKFunctions';
import { PrivateKey } from '@hashgraph/sdk';

export const operatorAccountId = process.env.FIRST_ACCOUNT_ID!;
export const operatorPrivateKey = process.env.FIRST_PRIVATE_KEY!;
export const operatorPrivateKey = PrivateKey.fromStringDer(process.env.FIRST_PRIVATE_KEY!);

export const secondAccountId = process.env.SECOND_ACCOUNT_ID!;
export const secondPrivateKey = process.env.SECOND_PRIVATE_KEY!;
export const secondPrivateKey = PrivateKey.fromStringDer(process.env.SECOND_PRIVATE_KEY!);

export const nftStorageApiKey = process.env.NFTSTORAGE_API_KEY!;
export const pinataApiKey = process.env.PINATA_API_KEY!;
Expand Down
6 changes: 3 additions & 3 deletions src/test/e2e/get-holder-and-duration-e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

import { LONG_E2E_TIMEOUT, LONG_MIRROR_NODE_DELAY, MIRROR_NODE_DELAY } from '../__mocks__/consts';
import { nftSDK, operatorAccountId, operatorPrivateKey, secondAccountId, secondPrivateKey } from './e2e-consts';
import { AccountId, NftId, PrivateKey, TokenAssociateTransaction, TokenId, TransferTransaction } from '@hashgraph/sdk';
import { AccountId, NftId, TokenAssociateTransaction, TokenId, TransferTransaction } from '@hashgraph/sdk';
import { getHolderAndDuration } from '../../get-holder-and-duration';

afterAll(async () => {
Expand Down Expand Up @@ -76,14 +76,14 @@ describe('getHolderAndDuration', () => {
.setTokenIds([TokenId.fromString(tokenId)])
.freezeWith(nftSDK.client);

const associateSignTx = await associateTransaction.sign(PrivateKey.fromString(secondPrivateKey));
const associateSignTx = await associateTransaction.sign(secondPrivateKey);
await associateSignTx.execute(nftSDK.client);

// Transfer created NFT from first acc to second acc
const transaction = new TransferTransaction()
.addNftTransfer(nftId, AccountId.fromString(operatorAccountId), AccountId.fromString(secondAccountId))
.freezeWith(nftSDK.client);
const signTx = await transaction.sign(PrivateKey.fromString(operatorPrivateKey));
const signTx = await transaction.sign(operatorPrivateKey);
await signTx.execute(nftSDK.client);

await new Promise((resolve) => setTimeout(resolve, LONG_MIRROR_NODE_DELAY));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
BATCH_SIZE,
} from '../e2e-consts';
import { LONG_E2E_TIMEOUT, MIRROR_NODE_DELAY } from '../../__mocks__/consts';
import { getPrivateKeyFromString } from '../../../helpers/get-private-key-from-string';

describe('E2E test for validating shared NFT Collection Metadata Against HIP412 schema', () => {
let tokenId: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { PrivateKey } from '@hashgraph/sdk';
import { Hip412Validator } from '../../../hip412-validator';
import {
nftSDK,
Expand All @@ -11,7 +10,6 @@ import {
METADATA_TO_VALIDATE_OBJECT_SERIAL,
} from '../e2e-consts';
import { LONG_E2E_TIMEOUT, MIRROR_NODE_DELAY } from '../../__mocks__/consts';
import { getPrivateKeyFromString } from '../../../helpers/get-private-key-from-string';

describe('E2E test for validating single NFT Metadata Object Against HIP412 schema', () => {
let tokenId: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@
* limitations under the License.
*
*/
import { NftId, PrivateKey, TokenId, TokenNftInfoQuery } from '@hashgraph/sdk';
import { NftId, TokenId, TokenNftInfoQuery } from '@hashgraph/sdk';
import { nftSDK, operatorPrivateKey } from '../e2e-consts';
import { LONG_E2E_TIMEOUT } from '../../__mocks__/consts';
import { getPrivateKeyFromString } from '../../../helpers/get-private-key-from-string';

describe('mintSharedMetadata function e2e', () => {
const testCases = [{ amount: 1 }, { amount: 3 }, { amount: 10 }];
Expand Down
Loading

0 comments on commit f681a2b

Please sign in to comment.