From 660ce0b38dec160675b9e178f125f740e22dcf26 Mon Sep 17 00:00:00 2001 From: ch4ot1c Date: Mon, 23 Apr 2018 17:18:44 -0500 Subject: [PATCH] Replace ZCL and BTC version bytes to convert to BTCP addresses --- lib/convert.js | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 lib/convert.js diff --git a/lib/convert.js b/lib/convert.js new file mode 100644 index 0000000..666e015 --- /dev/null +++ b/lib/convert.js @@ -0,0 +1,43 @@ +'use strict'; + +const bs58check = require('bs58check'); + +// Reserialize addresses from BTC or ZCL to BTCP +var convertAddresses = addresses => { + const btcpVersionBytes = Buffer.from([0x13, 0x25]); + + let btcpAddresses = []; + + try { + for (let a of addresses) { + let raw = bs58check.decode(a); + //console.log(a); + //console.log(raw); + + let first = raw.slice(0,1).toString('hex'); + if (first === '00' || first === '05') { // BTC + let newBuffer = Buffer.concat([btcpVersionBytes, raw.slice(1)]); + btcpAddresses.push(bs58check.encode(newBuffer).toString()); + } else { + let firstTwo = raw.slice(0,2).toString('hex') + if (firstTwo === '1325' || firstTwo === '13af') { // BTCP + btcpAddresses.push(a); + } else if (firstTwo === '1cb8' || firstTwo === '1cbd') { // ZCL + let newBuffer = Buffer.concat([btcpVersionBytes, raw.slice(2)]); + btcpAddresses.push(bs58check.encode(newBuffer).toString()); + } else { + console.log('UNKNOWN VERSION BYTES'); + } + } + } + } catch (e) { console.log('error'); throw e; } + + return btcpAddresses; +} + +//const exampleOutput = convertAddresses(['b1CypS2LjfM4qV5AwJkUudkJqfvwNSs2T6K', 't1euD4gB45npWpcpN7W5AuniU2sZoG3XfNE','1FH43sVnAtEkUchQyJtZGAdKGS7KZjPaJB']); +//console.log(exampleOutput); + +// Run with: node lib/convert.js + +module.exports = {convertAddresses: convertAddresses};