Skip to content

Commit 7d58085

Browse files
committed
docs(root): use embed for omni example OP_RETURN
BTC-1864 TICKET: BTC-0
1 parent dfb18a7 commit 7d58085

File tree

2 files changed

+49
-46
lines changed

2 files changed

+49
-46
lines changed

examples/ts/btc/omni/config.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { BitGoAPI } from '@bitgo/sdk-api';
22
import { Btc, Tbtc4 } from '@bitgo/sdk-coin-btc';
3+
import * as utxolib from '@bitgo/utxo-lib';
34

45
const env = 'test' as 'test' | 'prod';
56

@@ -17,6 +18,7 @@ sdk.authenticateWithAccessToken({ accessToken });
1718
export const omniConfig = {
1819
env,
1920
coin: env === 'test' ? 'tbtc4' : 'btc',
21+
network: env === 'test' ? utxolib.networks.bitcoinTestnet4 : utxolib.networks.bitcoin,
2022
sdk,
2123
walletPassphrase,
2224
walletId,

examples/ts/btc/omni/index.ts

Lines changed: 47 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,47 @@
66
import { Wallet } from 'modules/bitgo/src';
77
import { omniConfig } from './config';
88
import * as superagent from 'superagent';
9+
import * as utxolib from '@bitgo/utxo-lib';
910

1011
const RECEIVE_ADDRESS = '';
1112
const SEND_ADDRESS = '';
12-
const NETWORK = omniConfig.coin === 'tbtc4/' ? 'testnet4' : '';
13-
const AMOUNT = 1234;
13+
const MEMPOOL_PREFIX = omniConfig.coin === 'tbtc4' ? 'testnet4/' : '';
14+
const AMOUNT = 729100000n;
1415
const ASSET_ID = 31;
16+
const OMNI_PREFIX = Buffer.from('6f6d6e69', 'hex');
1517

1618
async function getWallet() {
1719
return await omniConfig.sdk.coin(omniConfig.coin).wallets().get({ id: omniConfig.walletId });
1820
}
1921

20-
function strToHex(s: string) {
21-
return (
22-
s
23-
.split('')
24-
.map((c) => c.charCodeAt(0).toString(16))
25-
.join('') + '0'
26-
);
27-
}
28-
2922
// eslint-disable-next-line @typescript-eslint/no-unused-vars
3023
async function mintOmniAsset(wallet: Wallet, address: string, feeRate = 20_000) {
31-
const transactionVersion = '0000';
32-
const trnasactionType = (50).toString(16).padStart(4, '0');
33-
const ecoSystem = '02';
34-
const propertyType = (2).toString(16).padStart(4, '0');
35-
const previousPropertyID = '00000000';
36-
const category = strToHex('Other\0');
37-
const subCategory = strToHex('Other\0');
38-
const propertyTitle = strToHex('Testcoin\0');
39-
const propertyURL = strToHex('https://example.com\0');
40-
const propertyData = strToHex('\0');
41-
const amount = (100000 * 10**8).toString(16).padStart(16, '0');
24+
const transactionVersion = Buffer.alloc(2);
25+
const transactionType = Buffer.alloc(2);
26+
transactionType.writeUint16BE(50);
27+
const ecoSystem = Buffer.alloc(1);
28+
ecoSystem.writeInt8(2);
29+
const propertyType = Buffer.alloc(2);
30+
propertyType.writeUint16BE(2);
31+
const previousPropertyID = Buffer.alloc(4);
32+
33+
const category = Buffer.from('Other\0');
34+
const subCategory = Buffer.from('Other\0');
35+
const propertyTitle = Buffer.from('Testcoin\0');
36+
const propertyURL = Buffer.from('https://example.com\0');
37+
const propertyData = Buffer.from('\0');
38+
39+
const amount = Buffer.alloc(8);
40+
amount.writeBigUint64BE(BigInt(100000 * 10 ** 8));
4241

43-
const res = await superagent.get(`https://mempool.space/${NETWORK}api/address/${address}/utxo`);
42+
const res = await superagent.get(`https://mempool.space/${MEMPOOL_PREFIX}api/address/${address}/utxo`);
4443
const unspent = res.body[0];
4544
const unspent_id = unspent.txid + ':' + unspent.vout;
4645

47-
const omniScript = [
48-
'6f6d6e69', // omni
46+
const omniScript = Buffer.concat([
47+
OMNI_PREFIX, // omni
4948
transactionVersion,
50-
trnasactionType,
49+
transactionType,
5150
ecoSystem,
5251
propertyType,
5352
previousPropertyID,
@@ -57,21 +56,18 @@ async function mintOmniAsset(wallet: Wallet, address: string, feeRate = 20_000)
5756
propertyURL,
5857
propertyData,
5958
amount,
60-
].join('');
59+
]);
6160

62-
// scriptPubkey: op_return omni simple_send tether amount
63-
const script =
64-
'scriptPubkey:' +
65-
[
66-
'6a', // op_return
67-
(omniScript.length / 2).toString(16).padStart(2, '0'),
68-
omniScript,
69-
].join('');
61+
const output = utxolib.payments.embed({ data: [omniScript], network: utxolib.networks.bitcoin }).output;
62+
if (!output) {
63+
throw new Error('Invalid output');
64+
}
65+
const script = output.toString('hex');
7066
const tx = await wallet.sendMany({
7167
recipients: [
7268
{
7369
amount: '0',
74-
address: script,
70+
address: `scriptPubkey:${script}`,
7571
},
7672
],
7773
isReplaceableByFee: true,
@@ -87,21 +83,26 @@ async function sendOmniAsset(
8783
wallet: Wallet,
8884
receiver: string,
8985
sender: string,
90-
amountMicroCents: number,
86+
amountMicroCents: bigint,
9187
assetId = 31,
9288
feeRate = 20_000
9389
) {
94-
// convert amountMicroCents to hex string of length 16
95-
const amountHex = amountMicroCents.toString(16).padStart(16, '0');
96-
97-
const assetHex = assetId.toString(16).padStart(8, '0');
98-
99-
const res = await superagent.get(`https://mempool.space/${NETWORK}/api/address/${sender}/utxo`);
90+
const res = await superagent.get(`https://mempool.space/${MEMPOOL_PREFIX}/api/address/${sender}/utxo`);
10091
const unspent = res.body[0];
10192
const unspent_id = unspent.txid + ':' + unspent.vout;
10293

10394
// scriptPubkey: op_return omni simple_send tether amount
104-
const script = ('scriptPubkey: 6a14 6f6d6e69 00000000' + assetHex + amountHex).split(' ').join('');
95+
const transactionType = Buffer.alloc(4);
96+
const assetHex = Buffer.alloc(4);
97+
assetHex.writeUInt32BE(assetId);
98+
const amountHex = Buffer.alloc(8);
99+
amountHex.writeBigUInt64BE(amountMicroCents);
100+
const omniScript = Buffer.concat([OMNI_PREFIX, transactionType, assetHex, amountHex]);
101+
const output = utxolib.payments.embed({ data: [omniScript], network: omniConfig.network }).output;
102+
if (!output) {
103+
throw new Error('Invalid output');
104+
}
105+
const script = output.toString('hex');
105106
const tx = await wallet.sendMany({
106107
recipients: [
107108
{
@@ -110,7 +111,7 @@ async function sendOmniAsset(
110111
},
111112
{
112113
amount: '0',
113-
address: script,
114+
address: `scriptPubkey:${script}`,
114115
},
115116
],
116117
isReplaceableByFee: true,
@@ -128,7 +129,7 @@ async function sendOmniAsset(
128129
async function main() {
129130
console.log('Starting...');
130131

131-
const feeRateRes = await superagent.get(`https://mempool.space/${NETWORK}api/v1/fees/recommended`);
132+
const feeRateRes = await superagent.get(`https://mempool.space/${MEMPOOL_PREFIX}api/v1/fees/recommended`);
132133
const feeRate = feeRateRes.body.fastestFee;
133134

134135
const wallet = await getWallet();

0 commit comments

Comments
 (0)