1

Trying to make a PSBT via BitcoinJS-lib.

I'm using BlockCypher's internal testnet (bcy/test) because I have no choice: literally every other testnet faucet I tested was broken.

Using BlockCypher's API to make new addresses, they give you an address, private, public and wif

(their documentation doesn't mention the wif, but it does indeed give one...)

Here's an example response

{
  "private": "9125ea9f573e23ce178d98cc2ec2a78655bd030c14b525729a026d6570b411c8",
  "public": "03f1fbc34305c61d7638c449030c32a66eb36726c208fca9962923a98ca75d77fe",
  "address": "C1E96vE5GvKd5kXU4T4hhF6QioZzACrmdD",
  "wif": "BtCBNtS2noEXwm4rJi9nyiVbmZJMR9CEBMbF5Uz6JTSaR9EQn8jS"
}

However none of this information is usable on it's own because the sign functions of BitcoinJS-lib expect a Signer (KeyPair) class object.. giving it a private key on it's own doesn't work.

i.e. psbt.signInput(0, privateKey); results in: Error: Need Signer to sign input

I tried to make a KeyPair out of a WIF, but because BitcoinJS-lib lacks the bitcoin.networks configuration appropriate for bcy/test it always fails to resolve..

ECPair.fromWIF(wif, bitcoin.networks.testnet); // Error: Invalid network version
ECPair.fromWIF(wif, bitcoin.networks.bitcoin); // Error: Invalid network version
ECPair.fromWIF(wif); // Error: Invalid network version

The error message suggests the WIF is not possible on those attempted networks.

How do I make a KeyPair for bcy/test ?

1 Answer 1

1

Blockcypher seems to be using custom network with some of the things changed, as they dont mention anything about WIF I recommend not using it, they only mention 2 changes the ScriptHash and the Pubkeyhash, you can use those to create a custom network object and use that for ECPair and PSBT:

const BLOCKCYPHER = {...bitcoin.networks.testnet}; // copy to not pollute original
BLOCKCYPHER.pubKeyHash = 0x1B;
BLOCKCYPHER.scriptHash = 0x1F;
BLOCKCYPHER.wif = 73;
bitcoin.networks.bcytestnet = BLOCKCYPHER; // add back as a new bitcoin.network

and it is implemented as follows

var keyPair = ECPair.fromPrivateKey(Buffer.from('9125ea9f573e23ce178d98cc2ec2a78655bd030c14b525729a026d6570b411c8', 'hex'), bitcoin.networks.bcytestnet);
const { address } = bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: bitcoin.networks.bcytestnet});
console.log(address); //outputs correct address: C1E96vE5GvKd5kXU4T4hhF6QioZzACrmdD
const psbt = new bitcoin.Psbt({ network: bitcoin.networks.bcytestnet });
4
  • This certainly seems like the correct approach, but still gives 'Invalid network version' when used with fromWIF. Making a KeyPair from a privateKey instead seems to work, but it works regardless of which network is supplied. So now I'm even more confused. Shouldn't either method fail since the network is not fully correct for blockcypher?
    – 1owk3y
    Commented Jan 14 at 1:44
  • Doing some reverse engineering I found that adding BLOCKCYPHER.wif = 73 to your answer might supress the error and hopefully fill any cryptographic holes in which case I will Accept your answer. Let me do some tests to see how well the revised answer works after being integrated. Thanks for your help!
    – 1owk3y
    Commented Jan 15 at 14:56
  • 1
    @1owk3y this approach should work unless they have changed any other parameters in their blockchain. They only mention 2 of them changed at: blockcypher.com/dev/bitcoin/#testing , but they have Wif changed as well. They don't seem to share the code for their private chain as well or else we could use that to find out all of the other parameters in the Network object. Commented Jan 15 at 19:47
  • Honestly I'm not entirely sure if adding wif=73 does anything at all except for supressing the warning about the wif's 'version' not being expected... but after testing it looks like it stops enough warnings to let signings and keypair generation start happening and the transactions do indeed get processed by BlockCypher's blockchain correctly. Thanks again!
    – 1owk3y
    Commented Jan 16 at 1:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.