-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathtransfer-btc.ts
48 lines (40 loc) · 1.63 KB
/
transfer-btc.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { sendBtc, DataSource, NetworkType, bitcoin, ECPair } from '@rgbpp-sdk/btc';
import { BtcAssetsApi } from '@rgbpp-sdk/service';
// BTC SECP256K1 private key
const BTC_TEST_PRIVATE_KEY = '0000000000000000000000000000000000000000000000000000000000000001';
// https://btc-assets-api-develop.vercel.app/docs/static/index.html
const BTC_ASSETS_API_URL = 'https://btc-assets-api.testnet.mibao.pro';
// https://btc-assets-api.testnet.mibao.pro/docs/static/index.html#/Token/post_token_generate
const BTC_ASSETS_TOKEN = '';
// This example shows how to transfer BTC on testnet
const transferBtc = async () => {
const network = bitcoin.networks.testnet;
const keyPair = ECPair.fromPrivateKey(Buffer.from(BTC_TEST_PRIVATE_KEY, 'hex'), { network });
const { address: btcAddress } = bitcoin.payments.p2wpkh({
pubkey: keyPair.publicKey,
network,
});
console.log('btc address: ', btcAddress);
const networkType = NetworkType.TESTNET;
const service = BtcAssetsApi.fromToken(BTC_ASSETS_API_URL, BTC_ASSETS_TOKEN, 'https://btc-test.app');
const source = new DataSource(service, networkType);
const psbt = await sendBtc({
from: btcAddress!, // your P2WPKH address
tos: [
{
address: btcAddress!, // destination btc address
value: 100000, // transfer satoshi amount
},
],
feeRate: 1, // optional, default to 1 sat/vbyte
source,
});
// Sign & finalize inputs
psbt.signAllInputs(keyPair);
psbt.finalizeAllInputs();
// Broadcast transaction
const tx = psbt.extractTransaction();
const { txid: txId} = await service.sendBtcTransaction(tx.toHex());
console.log('txId:', txId);
}
transferBtc();