Skip to content

Instantly share code, notes, and snippets.

@gregdhill
Created July 10, 2020 11:38
Show Gist options
  • Save gregdhill/79f0f1248e369658d21d6d7d3bb8ce02 to your computer and use it in GitHub Desktop.
Save gregdhill/79f0f1248e369658d21d6d7d3bb8ce02 to your computer and use it in GitHub Desktop.
import AppBtc from "@ledgerhq/hw-app-btc";
import Transport from '@ledgerhq/hw-transport-u2f';
import * as bitcoin from 'bitcoinjs-lib';
import {toBufferLE} from 'bigint-buffer';
const OPEN_TIMEOUT = 10000;
const LISTENER_TIMEOUT = 300;
const NETWORK = bitcoin.networks.testnet;
async function main() {
const transport = await Transport.create(OPEN_TIMEOUT, LISTENER_TIMEOUT);
const app = new AppBtc(transport);
const txIndex = 1;
const utxoHex = "*****";
const utxo = app.splitTransaction(utxoHex, true);
const script = bitcoin.payments.p2sh({ address: "*****", network: NETWORK });
const outputScriptHex = app.serializeTransactionOutputs({
version: Buffer.from("01000000", 'hex'),
inputs: [],
outputs: [{
amount: toBufferLE(BigInt(100), 8),
script: script.output || Buffer.from(""),
}]
}).toString('hex');
const publicKeyHex = "*****";
const { publicKey } = bitcoin.ECPair.fromPublicKey(Buffer.from(publicKeyHex, 'hex'));
const p2wpkh = bitcoin.payments.p2wpkh({ pubkey: publicKey, network: NETWORK });
const p2wsh_p2wpkh = bitcoin.payments.p2wsh({ redeem: p2wpkh, network: NETWORK });
const redeemScript = p2wsh_p2wpkh.redeem?.output;
const signature = await app.signP2SHTransaction({
inputs: [[utxo, txIndex, redeemScript?.toString('hex') || "", null]],
associatedKeysets: [ "49'/1'/0'/0" ],
outputScriptHex,
lockTime: 0,
segwit: false,
transactionVersion: 1,
sigHashType: 1,
});
console.log(signature);
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment