import { KeyPair } from '#types'\r
import { Block } from '../block'\r
import { ACCOUNT_KEY_BYTE_LENGTH, ACCOUNT_KEY_HEX_LENGTH, ALPHABET, BURN_ADDRESS, PREFIX, PREFIX_LEGACY } from '../constants'\r
-import { base32, bytes, hex } from '../convert'\r
-import { Blake2b, NanoNaCl } from '../crypto'\r
+import { bytes, hex } from '../convert'\r
+import { NanoNaCl } from '../crypto'\r
import { Rpc } from '../rpc'\r
import { Address } from './address'\r
import { _refresh } from './refresh'\r
set representative_block (v: string | undefined) { this.#representative_block = v }\r
set weight (v: bigint | number | string) { this.#weight = BigInt(v) }\r
\r
- private constructor (address: string, publicKey: string | Uint8Array<ArrayBuffer>, index?: number) {\r
+ private constructor (address: Address, publicKey: Uint8Array<ArrayBuffer>, index?: number) {\r
if (!Account.#isInternal) {\r
throw new Error('Account cannot be instantiated directly. Use `load()` instead.')\r
}\r
Account.#isInternal = false\r
- this.#address = new Address(address)\r
- this.#publicKey = typeof publicKey === 'string'\r
- ? hex.toBytes(publicKey)\r
- : publicKey\r
+ this.#address = address\r
+ this.#publicKey = publicKey\r
this.#index = index\r
}\r
\r
return _validate(address)\r
}\r
\r
- /**\r
- * Converts a Nano address to a public key.\r
- *\r
- * @param {string} address - Prefixed with `nano_`\r
- * @returns Public key bytes as Uint8Array\r
- */\r
- static #addressToKey (address: string): Uint8Array<ArrayBuffer> {\r
- const publicKey = base32.toBytes(address.slice(-60, -8))\r
- const checksum = base32.toBytes(address.slice(-8))\r
- const rechecksum = new Blake2b(5).update(publicKey).digest().reverse()\r
- if (bytes.toHex(checksum) !== bytes.toHex(rechecksum)) {\r
- throw new Error('Checksum mismatch in address')\r
- }\r
- return publicKey\r
- }\r
-\r
/**\r
* Instantiates an Account object from its private key which is used to derive\r
* the corresponding public key and then discarded.\r
throw new TypeError(`Private key must be ${ACCOUNT_KEY_BYTE_LENGTH} bytes`)\r
}\r
const publicKey = await NanoNaCl.convert(privateKey)\r
- const address = Address.fromPublicKey(publicKey)\r
+ const address = new Address(publicKey)\r
this.#isInternal = true\r
accounts.push(new this(address, publicKey, index))\r
}\r
\r
const accounts: Account[] = []\r
for (let keypair of keypairs) {\r
- const { index } = keypair\r
- if (typeof keypair.publicKey === 'string') {\r
- if (RegExp(`^[A-F0-9]{${ACCOUNT_KEY_HEX_LENGTH}}$`, 'i').test(keypair.publicKey)) {\r
- const publicKey = hex.toBytes(keypair.publicKey)\r
- const address = Address.fromPublicKey(publicKey)\r
- this.#isInternal = true\r
- accounts.push(new this(address, publicKey, index))\r
- } else if (RegExp(`(${PREFIX}|${PREFIX_LEGACY})`).test(keypair.publicKey)) {\r
- const address = keypair.publicKey\r
- const publicKey = new Address(address).toPublicKey()\r
- this.#isInternal = true\r
- accounts.push(new this(address, publicKey, index))\r
- } else {\r
- throw new TypeError('Invalid string', { cause: keypair.publicKey })\r
- }\r
- } else if (keypair.publicKey instanceof ArrayBuffer) {\r
- if (keypair.publicKey.byteLength === ACCOUNT_KEY_BYTE_LENGTH) {\r
- const publicKey = new Uint8Array(keypair.publicKey)\r
- const address = Address.fromPublicKey(publicKey)\r
- this.#isInternal = true\r
- accounts.push(new this(address, publicKey, index))\r
- } else {\r
- throw new TypeError('Invalid buffer', { cause: keypair.publicKey })\r
- }\r
- } else {\r
- throw new TypeError('Invalid Account input', { cause: keypair.publicKey })\r
+ if (keypair.publicKey == null) {\r
+ throw new TypeError('Account address or public key is required', { cause: keypair.publicKey })\r
}\r
+ const { index } = keypair\r
+ const address = new Address(keypair.publicKey)\r
+ const publicKey = address.toPublicKey()\r
+ this.#isInternal = true\r
+ accounts.push(new this(address, publicKey, index))\r
}\r
return accounts\r
} catch (err) {\r
}\r
return true\r
}\r
-\r
- /**\r
- * Converts a public key to a Nano address.\r
- *\r
- * @param {Uint8Array} publicKey - Public key bytes as Uint8Array\r
- * @returns Nano address string using `nano_` prefix\r
- */\r
- static #keyToAddress (publicKey: Uint8Array<ArrayBuffer>): string {\r
- const checksum = new Blake2b(5).update(publicKey).digest().reverse()\r
- const encodedPublicKey = bytes.toBase32(publicKey)\r
- const encodedChecksum = bytes.toBase32(checksum)\r
- return `${PREFIX}${encodedPublicKey}${encodedChecksum}`\r
- }\r
}\r
\r
export class AccountList extends Object {\r