$npx -y skills add aptos-labs/aptos-agent-skills --skill ts-sdk-addressHow to create and use AccountAddress in @aptos-labs/ts-sdk. Covers address format (AIP-40), from/fromString/fromStrict, special addresses, LONG vs SHORT form, and derived addresses (object, resource, token, user-derived). Triggers on: 'AccountAddress', 'AccountAddress.from', 'AIP
| 1 | # TypeScript SDK: AccountAddress |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | Guide correct creation, parsing, and formatting of **account addresses** in `@aptos-labs/ts-sdk`. Addresses are 32-byte |
| 6 | values; string format follows **AIP-40**. |
| 7 | |
| 8 | ## ALWAYS |
| 9 | |
| 10 | 1. **Use `AccountAddress.from()` for flexible input** – accepts string (with or without `0x`), `Uint8Array`, or existing |
| 11 | `AccountAddress`. |
| 12 | 2. **Use addresses as `AccountAddress` or string in API** – SDK accepts `AccountAddressInput` (string or |
| 13 | `AccountAddress`) in most APIs. |
| 14 | 3. **Use `AccountAddress.fromStringStrict()` / `AccountAddress.fromStrict()`** when you need AIP-40 strict: LONG (0x + |
| 15 | 64 hex chars) or SHORT only for special (0x0–0xf). |
| 16 | 4. **Use derived address helpers** from the SDK for object/resource/token addresses – do not hand-roll hashing. |
| 17 | |
| 18 | ## NEVER |
| 19 | |
| 20 | 1. **Do not use raw strings for comparison** – use `addr.equals(other)` or normalize with `AccountAddress.from()`. |
| 21 | 2. **Do not assume SHORT form for non-special addresses** – non-special addresses must be LONG (64 hex chars) in strict |
| 22 | mode. |
| 23 | 3. **Do not use the `Hex` class for account addresses** – use `AccountAddress` only (per SDK docs). |
| 24 | |
| 25 | --- |
| 26 | |
| 27 | ## Address format (AIP-40) |
| 28 | |
| 29 | - **Length:** 32 bytes (64 hex chars in LONG form). |
| 30 | - **String:** Must start with `0x`. LONG = `0x` + 64 hex chars; SHORT = shortest form (e.g. `0x1`, `0xf`). |
| 31 | - **Special addresses:** `0x0`–`0xf` (last byte < 16, rest zero). These may be written in SHORT form (`0x1`, `0xa`). |
| 32 | - **Reference:** [AIP-40](https://github.com/aptos-foundation/AIPs/blob/main/aips/aip-40.md). |
| 33 | |
| 34 | --- |
| 35 | |
| 36 | ## Creating AccountAddress |
| 37 | |
| 38 | ### From string (recommended: relaxed) |
| 39 | |
| 40 | ```typescript |
| 41 | import { AccountAddress } from "@aptos-labs/ts-sdk"; |
| 42 | |
| 43 | // Relaxed: accepts with or without 0x, SHORT or LONG |
| 44 | const addr1 = AccountAddress.from("0x1"); |
| 45 | const addr2 = AccountAddress.from("0xaa86fe99004361f747f91342ca13c426ca0cccb0c1217677180c9493bad6ef0c"); |
| 46 | const addr3 = AccountAddress.from("1"); // no 0x ok |
| 47 | ``` |
| 48 | |
| 49 | ### From string (strict AIP-40) |
| 50 | |
| 51 | ```typescript |
| 52 | // Strict: LONG (0x + 64 chars) or SHORT only for special (0x0–0xf) |
| 53 | const addrStrict = AccountAddress.fromStringStrict( |
| 54 | "0x0000000000000000000000000000000000000000000000000000000000000001" |
| 55 | ); |
| 56 | // Or use fromStrict for any AccountAddressInput |
| 57 | const a = AccountAddress.fromStrict("0x1"); // ok: special address in SHORT form |
| 58 | ``` |
| 59 | |
| 60 | ### From bytes |
| 61 | |
| 62 | ```typescript |
| 63 | const bytes = new Uint8Array(32); |
| 64 | bytes[31] = 1; |
| 65 | const addr = new AccountAddress(bytes); |
| 66 | // or |
| 67 | const addrFrom = AccountAddress.from(bytes); |
| 68 | ``` |
| 69 | |
| 70 | ### Built-in constants |
| 71 | |
| 72 | ```typescript |
| 73 | AccountAddress.ZERO; // 0x0 |
| 74 | AccountAddress.ONE; // 0x1 |
| 75 | AccountAddress.TWO; // 0x2 |
| 76 | AccountAddress.THREE; // 0x3 |
| 77 | AccountAddress.FOUR; // 0x4 |
| 78 | AccountAddress.A; // 0xa |
| 79 | ``` |
| 80 | |
| 81 | --- |
| 82 | |
| 83 | ## String output |
| 84 | |
| 85 | | Method | Use case | |
| 86 | | ---------------------------------- | -------------------------------------------------- | |
| 87 | | `addr.toString()` | AIP-40 default: SHORT for special, LONG for others | |
| 88 | | `addr.toStringLong()` | Always 0x + 64 hex chars | |
| 89 | | `addr.toStringShort()` | Shortest form (no leading zeros) | |
| 90 | | `addr.toStringLongWithoutPrefix()` | 64 hex chars, no `0x` | |
| 91 | |
| 92 | ```typescript |
| 93 | const addr = AccountAddress.from("0x1"); |
| 94 | addr.toString(); // "0x1" |
| 95 | addr.toStringLong(); // "0x0000...0001" (64 chars after 0x) |
| 96 | ``` |
| 97 | |
| 98 | --- |
| 99 | |
| 100 | ## Validation |
| 101 | |
| 102 | ```typescript |
| 103 | const result = AccountAddress.isValid({ |
| 104 | input: "0x1", |
| 105 | strict: false |
| 106 | }); |
| 107 | if (result.valid) { |
| 108 | // use address |
| 109 | } else { |
| 110 | console.log(result.invalidReason, result.invalidReasonMessage); |
| 111 | } |
| 112 | ``` |
| 113 | |
| 114 | --- |
| 115 | |
| 116 | ## Derived addresses (object / resource / token / user-derived) |
| 117 | |
| 118 | Import from `@aptos-labs/ts-sdk` (via core): |
| 119 | |
| 120 | ```typescript |
| 121 | import { |
| 122 | AccountAddress, |
| 123 | createObjectAddress, |
| 124 | createResourceAddress, |
| 125 | createTokenAddress, |
| 126 | createUserDerivedObjectAddress |
| 127 | } from "@aptos-labs/ts-sdk"; |
| 128 | ``` |
| 129 | |
| 130 | ### Object address (e.g. named object) |
| 131 | |
| 132 | ```typescript |
| 133 | const creator = AccountAddress.from("0x120e79e45d21ef439963580c77a023e2729db799e96e61f878fac98fde5b9cc9"); |
| 134 | const seed = "migration::migration_contract"; // or Uint8Array |
| 135 | const objectAddr = createObjectAddress(creator, seed); |
| 136 | // objectAddr.toString() => deterministic 0x... address |
| 137 | ``` |
| 138 | |
| 139 | ### Resource account address |
| 140 | |
| 141 | ```typescript |
| 142 | const creator = AccountAddress.from("0x41e724e1d4fce6472ffcb5c9886770893 |