@relayprotocol/relay-bitcoin-wallet-adapter
Version:
A Bitcoin adapter for use in @relayprotocol/relay-sdk
57 lines • 2.25 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.adaptBitcoinWallet = void 0;
const relay_sdk_1 = require("@relayprotocol/relay-sdk");
const bitcoin = require("bitcoinjs-lib");
const adaptBitcoinWallet = (walletAddress, signPsbt, publicKey) => {
return {
vmType: 'bvm',
getChainId: async () => {
return 8253038;
},
address: async () => {
return walletAddress;
},
handleSignMessageStep: async () => {
throw new Error('Message signing not implemented for Bitcoin');
},
handleSendTransactionStep: async (_chainId, stepItem) => {
const client = (0, relay_sdk_1.getClient)();
const psbtHex = stepItem.data.psbt;
const psbt = bitcoin.Psbt.fromHex(psbtHex, {
network: bitcoin.networks.bitcoin
});
const signature = psbt.txInputs.map((_input, index) => {
return {
address: walletAddress,
signingIndexes: [index]
};
});
const dynamicParams = {
allowedSighash: [0, 1],
unsignedPsbtBase64: psbt.toBase64(),
signature
};
const signedPsbt = bitcoin.Psbt.fromBase64(await signPsbt(walletAddress, psbt, dynamicParams));
signedPsbt.finalizeAllInputs();
const rawTransaction = signedPsbt.extractTransaction().toHex();
client.log(['BTC Transaction', rawTransaction], relay_sdk_1.LogLevel.Verbose);
const mempoolResponse = await relay_sdk_1.axios
.post('https://mempool.space/api/tx', rawTransaction)
.then((r) => r.data);
client.log(['Transaction Broadcasted', mempoolResponse], relay_sdk_1.LogLevel.Verbose);
return mempoolResponse;
},
handleConfirmTransactionStep: async () => {
throw 'Not implemented';
},
switchChain: (chainId) => {
throw 'Not yet implemented';
},
metadata: {
publicKey
}
};
};
exports.adaptBitcoinWallet = adaptBitcoinWallet;
//# sourceMappingURL=adapter.js.map