From 0aa330819f76b3a78834a79ced0f555ae839ed43 Mon Sep 17 00:00:00 2001 From: Sily <93791263+nujia2017@users.noreply.github.com> Date: Tue, 15 Nov 2022 07:21:50 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=90=8C=E6=97=B6=E6=94=AF?= =?UTF-8?q?=E6=8C=81P2SH=E5=92=8CP2PK=E4=BB=98=E6=AC=BE=E5=8D=8F=E8=AE=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 添加同时支持P2SH和P2PK付款协议 --- lib/util.js | 50 +++++++++++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/lib/util.js b/lib/util.js index 1ecdc3df..0fb6e6c7 100644 --- a/lib/util.js +++ b/lib/util.js @@ -265,34 +265,42 @@ For POW coins - used to format wallet address for use in generation transaction' // return Buffer.concat([new Buffer([0x76, 0xa9, 0x14]), bitcoin.address.fromBase58Check(addr).hash, new Buffer([0x88, 0xac])]); //}; -exports.addressToScript = function (network, addr) { - if (addr[0] === "E") { - + exports.addressToScript = function(network, addr){ + if (addr[0] === "T") { // 添加P2SH钱包协议,T为TDC钱包首个字母 var decoded = base58.decode(addr); - - if (!decoded) { + + if (decoded.length != 25){ + console.error('invalid address length for ' + addr); + throw new Error(); + } + + if (!decoded){ console.error('base58 decode failed for ' + addr); throw new Error(); } - - var pubkey = decoded.slice(1, -4); - - return Buffer.concat([Buffer.from([0x76, 0xa9, 0x14]), pubkey, Buffer.from([0x88, 0xac])]); - } - if (addr[0] === "T") { - + + var pubkey = decoded.slice(1,-4); + + return Buffer.concat([new Buffer([0xa9, 0x14]), pubkey, new Buffer([0x87])]); + } + if (addr[0] === "D") { // 添加P2PK钱包协议,D为DYN钱包首个字母 var decoded = base58.decode(addr); - - if (!decoded) { + + if (decoded.length != 25){ + console.error('invalid address length for ' + addr); + throw new Error(); + } + + if (!decoded){ console.error('base58 decode failed for ' + addr); throw new Error(); } - - var pubkey = decoded.slice(1, -4); - - return Buffer.concat([Buffer.from([0xa9, 0x14]), pubkey, Buffer.from([0x87])]); - } - return exports.uint256BufferFromHash(script); + + var pubkey = decoded.slice(1,-4); + + return Buffer.concat([new Buffer([0x76, 0xa9, 0x14]), pubkey, new Buffer([0x88, 0xac])]); + } + return exports.uint256BufferFromHash(script); }; exports.kotoAddressToScript = function (addr) { @@ -494,4 +502,4 @@ exports.getKotoFounderRewardScript = function (addr) { var pubkey = decoded.slice(decoded.length - 24, -4); return Buffer.concat([Buffer.from([0xa9, 0x14]), pubkey, Buffer.from([0x87])]); -} \ No newline at end of file +}