Bitcoin ('1...') Address to ZCash t-Address

node v4.9.1
version: 1.0.1
endpointsharetweet
Nodejs code to convert Bitcoin Addresses into ZCash t-Addresses, and vice-versa. The same private ('spending') key that controls funds of one address can also control funds of the other, provided it is exported from its original wallet software and imported to wallet software for the alternate system. Use at your own risk. Before relying on any such converted address to receive funds, it would be prudent to verify that: * your private key is available to you; * that private key can be imported into the alternate-system wallet of your choice; and... * that wallet-system reports the same public address as this conversion
var bs58check = require('bs58check') // https://github.com/bitcoinjs/bs58check const assert = require('assert'); /** * Converts a Bitcoin "Pay To Public Key Hash" (P2PKH) public * address to a ZCash t-address. (Such Bitcoin addresses always * start with a '1'. * * The same private key (aka "spending key") that generated * the Bitcoin address can be used to control funds associated * with the ZCash t-address. (This requires a ZCash wallet system * which allows that private key to be imported. For example, * zcashd with its `importprivkey` function.) */ function baddr_to_taddr(baddr_str) { assert(baddr_str[0] == '1', "not a supported Bitcoin address"); var baddr = bs58check.decode(baddr_str).slice(1); // discard type byte var taddr = new Uint8Array(22); taddr.set(baddr, 2); taddr.set([0x1c,0xb8], 0); // set zcash type bytes return bs58check.encode(Buffer.from(taddr)); } /** * Converts a ZCash t-address (transparent address) to a Bitcoin * "Pay To Public Key Hash" (P2PKH) address. * * The same private key (aka "spending key") that generated * the ZCash t-address can be used to control funds associated * with the Bitcoin address. (This requires a Bitcoin wallet system * which allows that private key to be imported. For example, * bitcoin-core/bitcoind with its `importprivkey` function.) */ function taddr_to_baddr(taddr_str) { assert(taddr_str.substring(0,2) == 't1', "not a supported t-address"); var taddr = bs58check.decode(taddr_str).slice(2); // discard type bytes var baddr = new Uint8Array(21); baddr.set(taddr, 1); // leave 0x00 as Bitcoin's type byte return bs58check.encode(Buffer.from(baddr)); } // a couple famous addresses for testing var b_genesis = '1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa' var t_marshalls = 't1Xrh9nD8y6RzNCZwTF47V8ZmrqS7WhYM8i' 'ok'
What would Satoshi's genesis-block Bitcoin address be, as a ZCash t-address?
baddr_to_taddr(b_genesis)
Confirm roundtrips work as expected.
assert(taddr_to_baddr(baddr_to_taddr(b_genesis))==b_genesis) assert(baddr_to_taddr(taddr_to_baddr(t_marshalls))==t_marshalls)
Offer as Runkit 'endpoint' HTTP service. To use, append address to endpoint path for conversion. For example: https://runkit.io/gojomo/baddr2taddr/1.0.1/t1StbPM4X3j4FGM57HpGnb9BMbS7C1nFW1r
exports.endpoint = function(request, response) { var in_addr = request.url.split('/').slice(-1)[0] var out_addr; if(in_addr.substring(0,2) == 't1') { out_addr = taddr_to_baddr(in_addr); } else { out_addr = baddr_to_taddr(in_addr); } response.end(out_addr); }
Loading…

3 comments

  • posted 3 years ago by zedzeroth
    How easy would it be to get this script running completely offline? I'm confused how to get dependencies working...?
  • posted 3 years ago by zedzeroth
    How easy would it be to get this script running completely offline? I'm confused how to get dependencies working...?
  • posted 3 years ago by zedzeroth
    Oops, sorry for the double-post!

sign in to comment