UNPKG

@hdwallet/core

Version:

A complete Hierarchical Deterministic (HD) Wallet generator for 200+ cryptocurrencies, built with TypeScript.

46 lines 2.24 kB
// SPDX-License-Identifier: MIT import { Neo } from '../cryptocurrencies'; import { checkEncode, checkDecode } from '../libs/base58'; import { hash160 } from '../crypto'; import { SLIP10Nist256p1PublicKey, validateAndGetPublicKey } from '../eccs'; import { integerToBytes, bytesToString, concatBytes, getBytes, ensureString, equalBytes } from '../utils'; import { AddressError } from '../exceptions'; import { Address } from './address'; export class NeoAddress extends Address { static addressPrefix = integerToBytes(Neo.PARAMS.ADDRESS_PREFIX); static addressSuffix = integerToBytes(Neo.PARAMS.ADDRESS_SUFFIX); static addressVersion = integerToBytes(Neo.PARAMS.ADDRESS_VERSION); static alphabet = Neo.PARAMS.ALPHABET; static getName() { return 'Neo'; } static encode(publicKey, options = { addressVersion: this.addressVersion, alphabet: this.alphabet }) { const pk = validateAndGetPublicKey(publicKey, SLIP10Nist256p1PublicKey); const payload = concatBytes(this.addressPrefix, pk.getRawCompressed(), this.addressSuffix); const hashed = hash160(payload); const version = getBytes(options.addressVersion ?? this.addressVersion); return ensureString(checkEncode(getBytes(concatBytes(version, hashed)), options.alphabet ?? this.alphabet)); } static decode(address, options = { addressVersion: this.addressVersion, alphabet: this.alphabet }) { const decoded = checkDecode(address, options.alphabet ?? this.alphabet); const version = getBytes(options.addressVersion ?? this.addressVersion); const expectedLength = 20 + version.length; if (decoded.length !== expectedLength) { throw new AddressError('Invalid length', { expected: expectedLength, got: decoded.length }); } const versionGot = decoded.subarray(0, version.length); if (!equalBytes(version, versionGot)) { throw new AddressError('Invalid address version', { expected: bytesToString(version), got: bytesToString(versionGot) }); } return bytesToString(decoded.subarray(version.length)); } } //# sourceMappingURL=neo.js.map