UNPKG

@okxweb3/coin-bitcoin

Version:

@ok/coin-bitcoin is a Bitcoin SDK for building Web3 wallets and applications. It supports BTC, BSV, DOGE, LTC, and TBTC, enabling private key management, transaction signing, address generation, and inscriptions like BRC-20, Runes, CAT, and Atomicals.

68 lines 2.29 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.canAddToArray = exports.check = exports.expected = exports.encode = exports.decode = void 0; const typeFields_1 = require("../../typeFields"); function decode(keyVal) { if (keyVal.key[0] !== typeFields_1.InputTypes.PARTIAL_SIG) { throw new Error('Decode Error: could not decode partialSig with key 0x' + keyVal.key.toString('hex')); } if (!(keyVal.key.length === 34 || keyVal.key.length === 66) || ![2, 3, 4].includes(keyVal.key[1])) { throw new Error('Decode Error: partialSig has invalid pubkey in key 0x' + keyVal.key.toString('hex')); } const pubkey = keyVal.key.slice(1); return { pubkey, signature: keyVal.value, }; } exports.decode = decode; function encode(pSig) { const head = Buffer.from([typeFields_1.InputTypes.PARTIAL_SIG]); return { key: Buffer.concat([head, pSig.pubkey]), value: pSig.signature, }; } exports.encode = encode; exports.expected = '{ pubkey: Buffer; signature: Buffer; }'; function check(data) { return (Buffer.isBuffer(data.pubkey) && Buffer.isBuffer(data.signature) && [33, 65].includes(data.pubkey.length) && [2, 3, 4].includes(data.pubkey[0]) && isDerSigWithSighash(data.signature)); } exports.check = check; function isDerSigWithSighash(buf) { if (!Buffer.isBuffer(buf) || buf.length < 9) return false; if (buf[0] !== 0x30) return false; if (buf.length !== buf[1] + 3) return false; if (buf[2] !== 0x02) return false; const rLen = buf[3]; if (rLen > 33 || rLen < 1) return false; if (buf[3 + rLen + 1] !== 0x02) return false; const sLen = buf[3 + rLen + 2]; if (sLen > 33 || sLen < 1) return false; if (buf.length !== 3 + rLen + 2 + sLen + 2) return false; return true; } function canAddToArray(array, item, dupeSet) { const dupeString = item.pubkey.toString('hex'); if (dupeSet.has(dupeString)) return false; dupeSet.add(dupeString); return array.filter(v => v.pubkey.equals(item.pubkey)).length === 0; } exports.canAddToArray = canAddToArray; //# sourceMappingURL=partialSig.js.map