UNPKG

@helium/address

Version:

Helium public key utilities

80 lines 3.27 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const sha2_1 = require("multiformats/hashes/sha2"); const utils_1 = require("./utils"); const KeyTypes_1 = require("./KeyTypes"); const NetTypes_1 = require("./NetTypes"); const Address_1 = __importDefault(require("./Address")); class MultisigAddress extends Address_1.default { constructor(version, netType, M, N, publicKey) { if (M > 256) { throw new Error('required signers cannot exceed 256'); } if (N > 256) { throw new Error('total signers cannot exceed 256'); } if (M > N) { throw new Error('required signers cannot exceed total signers'); } super(version, netType, KeyTypes_1.MULTISIG_KEY_TYPE, publicKey); this.M = M; this.N = N; } get bin() { return Buffer.concat([ // eslint-disable-next-line no-bitwise Buffer.from([this.netType | this.keyType]), Buffer.from(new Uint8Array([this.M])), Buffer.from(new Uint8Array([this.N])), Buffer.from(this.publicKey), ]); } static fromB58(b58) { const keyType = (0, utils_1.bs58KeyType)(b58); if (keyType !== KeyTypes_1.MULTISIG_KEY_TYPE) { throw new Error('invalid keytype for multisig address'); } const version = (0, utils_1.bs58Version)(b58); const netType = (0, utils_1.bs58NetType)(b58); const M = (0, utils_1.bs58M)(b58); const N = (0, utils_1.bs58N)(b58); const publicKey = (0, utils_1.bs58MultisigPublicKey)(b58); return new MultisigAddress(version, netType, M, N, publicKey); } static fromBin(bin) { const version = 0; const byte = bin[0]; const netType = (0, utils_1.byteToNetType)(byte); const keyType = (0, utils_1.byteToKeyType)(byte); if (keyType !== KeyTypes_1.MULTISIG_KEY_TYPE) { throw new Error('invalid keytype for multisig address'); } const M = bin[1]; const N = bin[2]; const publicKey = bin.slice(3, bin.length); return new MultisigAddress(version, netType, M, N, publicKey); } static async create(addresses, M, netType) { if (addresses.some((addr) => addr.keyType === KeyTypes_1.MULTISIG_KEY_TYPE)) { return Promise.reject(new Error('cannot create multisig with invalid child keytype')); } const version = 0; const multisigPubKeysBin = (0, utils_1.sortAddresses)(addresses).map((address) => address.bin); const publicKey = (await sha2_1.sha256.digest(multisigPubKeysBin.reduce((acc, curVal) => new Uint8Array([...acc, ...curVal]), new Uint8Array()))); return new MultisigAddress(version, netType || NetTypes_1.MAINNET, M, addresses.length, publicKey.bytes); } static isValid(b58) { try { MultisigAddress.fromB58(b58); return true; } catch (error) { return false; } } } exports.default = MultisigAddress; //# sourceMappingURL=MultisigAddress.js.map