Bitcoin 1-Address to ZCash t-Address

node v4.9.1
version: 1.0.4
endpointsharetweet
Nodejs code to convert Bitcoin addresses into ZCash t-addresses, and vice-versa. This conversion needs only the publicly-shareable 'pay-to' address – not your private 'spending' key. Still, the same private ('spending') key that controls funds for a Bitcoin address can then control funds of the converted ZCash t-address (or vice-versa). To do so, you would need to export the private key from its original wallet software and import it to wallet software for the alternate system. Use this code and notebook at your own risk. Before relying on any such converted address to receive funds, it would be prudent to verify that… * …your original private key is available to you (unlikely with web wallet services); * …the private key can be imported into the alternate-system wallet of your choice; and... * …the wallet-system then reports the same 'pay-to' address as this conversion does. An appropriate level of paranoia in using any page/service like this would be to consider: "What if it's not doing the conversion it claims, and is instead substituting someone else's address?" Even if you trust me, the author, or can review and trust all shown code, by running it here (or using the below webservice-endpoint), you must also trust that… * …runkit.com is uncompromised, and faithfully running the shown code and reporting the results; * …node libraries used (such as 'bs58check') were and remain faithfully functional upon each load; * …your own machine hasn't been compromised with malware that slyly-replaces cryptocurrency-address strings when you're not paying attention Depending on RunKit's logging policies, you may be creating a log record associating any pay-to addresses you enter to your IP address, browser, or RunKit account.
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)
RunKit also makes it easy to offer this as an 'endpoint' HTTP service, accessible at a public URL. To use the service created by the code below, append a Bitcoin 1-address or ZCash t1-address to the endpoint's URL path for conversion. For example: https://runkit.io/gojomo/baddr2taddr/1.0.4/1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa The 'appropriate paranoia' notes above apply equally to this service, or any other which claims to help manage/convert/interpret cryptocurrency addresses.
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); }
Do you have another Bitcoin address to convert? You could use 'clone this notebook' button to the left to make a runnable version of this notebook in your own RunKit account, and replace the illegal string below with your address.
Do you have another Bitcoin address to convert? You could use 'clone this notebook' button to the left to make a runnable version of this notebook in your own RunKit account, and replace the illegal string below with your address.
baddr_to_taddr('INSERT_BITCOIN_ADDRESS_HERE')
Loading…

3 comments

  • posted 7 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 7 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 7 years ago by zedzeroth
    Oops, sorry for the double-post!

sign in to comment