Skip to content

Commit

Permalink
Merge branch 'main' into fix-custom-client-example
Browse files Browse the repository at this point in the history
  • Loading branch information
gregnazario authored Mar 26, 2024
2 parents 3918ee5 + 5376192 commit febc877
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ All notable changes to the Aptos TypeScript SDK will be captured in this file. T
- Add support to allow setting per-backend (fullnode, indexer, faucet) configuration
- [`Breaking`] `AUTH_TOKEN` client config moved to be under `faucetConfig` property
- Handle `Unauthorized` server error
- Add function to create object address locally
- Add function to create token object address locally

# 1.10.0 (2024-03-11)

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ const modules = await aptos.getAccountModules({ accountAddress: "0x123" });
```ts
const account = Account.generate(); // defaults to Legacy Ed25519
const account = Account.generate({ scheme: SingingSchemeInput.Secp256k1 }); // Single Sender Secp256k1
const account = Account.generate({ scheme: SingingSchemeInput.Ed25519, legacy: false }); // Single Sender Ed25519
const account = Account.generate({ scheme: SigningSchemeInput.Secp256k1Ecdsa }); // Single Sender Secp256k1
const account = Account.generate({ scheme: SigningSchemeInput.Ed25519, legacy: false }); // Single Sender Ed25519
```
#### Derive from private key
Expand Down
1 change: 1 addition & 0 deletions src/core/account/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./Ed25519Account";
export * from "./Account";
export * from "./SingleKeyAccount";
export * from "./utils";
39 changes: 39 additions & 0 deletions src/core/account/utils/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { sha3_256 } from "@noble/hashes/sha3";
import { AccountAddress } from "../../accountAddress";
import { DeriveScheme } from "../../../types";

/**
* Creates an object address from creator address and seed
*
* @param creatorAddress The object creator account address
* @param seed The seed in either Uint8Array | string type
*
* @returns The object account address
*/
export const createObjectAddress = (creatorAddress: AccountAddress, seed: Uint8Array | string): AccountAddress => {
const creatorBytes = creatorAddress.bcsToBytes();

const seedBytes = typeof seed === "string" ? Buffer.from(seed, "utf8") : seed;

const bytes = new Uint8Array([...creatorBytes, ...seedBytes, DeriveScheme.DeriveObjectAddressFromSeed]);

return new AccountAddress(sha3_256(bytes));
};

/**
* Creates a token object address from creator address, collection name and token name
*
* @param creatorAddress The token creator account address
* @param collectionName The collection name
* @param tokenName The token name
*
* @returns The token account address
*/
export const createTokenAddress = (
creatorAddress: AccountAddress,
collectionName: string,
tokenName: string,
): AccountAddress => {
const seed = `${collectionName}::${tokenName}`;
return createObjectAddress(creatorAddress, seed);
};
1 change: 1 addition & 0 deletions src/core/account/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./address";
40 changes: 40 additions & 0 deletions tests/unit/address.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/* eslint-disable max-len */
import { AccountAddress } from "../../src";
import { createTokenAddress, createObjectAddress } from "../../src/core/account/utils/address";

describe("address", () => {
/**
* Reference: {@link https://explorer.aptoslabs.com/account/0xf0995d360365587c500cc171d1416bad10a331b9c71871a1aec5f2c37ff43124/modules/view/migration_helper/migration_object_address?network=testnet}
* creatorAddr = 0x120e79e45d21ef439963580c77a023e2729db799e96e61f878fac98fde5b9cc9
* seed = "migration::migration_contract"
* Expect = 0xbe376272a5c4361ee96bc147525b26b3bf2ee25f433cbd410a7b3b4b881ffcbf
*/
test("create an object address from creator address and seed as Uint8Array type", () => {
const creatorAddress = AccountAddress.from("0x120e79e45d21ef439963580c77a023e2729db799e96e61f878fac98fde5b9cc9");
const seed = Buffer.from("migration::migration_contract", "utf8");
const address = createObjectAddress(creatorAddress, seed);
expect(address.toString()).toEqual("0xbe376272a5c4361ee96bc147525b26b3bf2ee25f433cbd410a7b3b4b881ffcbf");
});

test("create an object address from creator address and seed as string type", () => {
const creatorAddress = AccountAddress.from("0x120e79e45d21ef439963580c77a023e2729db799e96e61f878fac98fde5b9cc9");
const seed = "migration::migration_contract";
const address = createObjectAddress(creatorAddress, seed);
expect(address.toString()).toEqual("0xbe376272a5c4361ee96bc147525b26b3bf2ee25f433cbd410a7b3b4b881ffcbf");
});

/**
* Reference: {@link https://explorer.aptoslabs.com/txn/473081244/userTxnOverview?network=mainnet}
* creatorAddr = 0x9d518b9b84f327eafc5f6632200ea224a818a935ffd6be5d78ada250bbc44a6
* collectionName = SuperV Villains
* tokenName = Nami #5962
* Expect = 0x44697f48d1e1a899953b4ea6c03a92c567f3741f0b415a74d1c23cdf141368be
*/
test("create token object address", () => {
const creatorAddress = AccountAddress.from("0x9d518b9b84f327eafc5f6632200ea224a818a935ffd6be5d78ada250bbc44a6");
const collectionName = "SuperV Villains";
const tokenName = "Nami #5962";
const address = createTokenAddress(creatorAddress, collectionName, tokenName);
expect(address.toString()).toEqual("0x44697f48d1e1a899953b4ea6c03a92c567f3741f0b415a74d1c23cdf141368be");
});
});

0 comments on commit febc877

Please sign in to comment.