-
Notifications
You must be signed in to change notification settings - Fork 2
/
example.js
221 lines (183 loc) · 5.95 KB
/
example.js
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
const { execSync } = require("child_process");
const rgblib = require("./wrapper");
const PROXY_URL = "rpc://127.0.0.1:3000/json-rpc";
function mine(numBlocks) {
try {
execSync(`./rgb-lib/tests/regtest.sh mine ${numBlocks}`);
console.log("Mined " + numBlocks + " blocks");
} catch (e) {
throw new Error(`Unable to mine: ${e}`);
}
}
function sendToAddress(address, amt) {
try {
let res = execSync(
`./rgb-lib/tests/regtest.sh sendtoaddress ${address} ${amt}`,
);
console.log("Sent, TXID: " + res.toString().trim());
} catch (e) {
throw new Error(`Unable to send bitcoins: ${e}`);
}
}
/* Run this method and monitor memory usage to check there are no memory leaks */
function checkMemoryLeak() {
for (let i = 0; i < 50; i++) {
let [wallet, online] = initWallet();
rgblib.dropOnline(online);
wallet.drop();
}
}
function initWallet(vanillaKeychain) {
let bitcoinNetwork = rgblib.BitcoinNetwork.Regtest;
let keys = rgblib.generateKeys(bitcoinNetwork);
console.log("Keys: " + JSON.stringify(keys));
let restoredKeys = rgblib.restoreKeys(bitcoinNetwork, keys.mnemonic);
console.log("Restored keys: " + JSON.stringify(restoredKeys));
let walletData = {
dataDir: "./data",
bitcoinNetwork: bitcoinNetwork,
databaseType: rgblib.DatabaseType.Sqlite,
maxAllocationsPerUtxo: "1",
pubkey: keys.accountXpub,
mnemonic: keys.mnemonic,
vanillaKeychain: vanillaKeychain,
};
console.log("Creating wallet...");
let wallet = new rgblib.Wallet(new rgblib.WalletData(walletData));
console.log("Wallet created");
let btcBalance = wallet.getBtcBalance(null, true);
console.log("BTC balance: " + JSON.stringify(btcBalance));
let address = wallet.getAddress();
console.log("Address: " + address);
sendToAddress(address, 1);
console.log("Wallet is going online...");
let online = wallet.goOnline(false, "tcp://localhost:50001");
console.log("Wallet went online");
btcBalance = wallet.getBtcBalance(online, false);
console.log("BTC balance: " + JSON.stringify(btcBalance));
let created = wallet.createUtxos(online, false, "25", null, "1.0", false);
console.log("Created " + created + " UTXOs");
return [wallet, online];
}
function main() {
let [wallet, online] = initWallet(null);
let asset1 = wallet.issueAssetNIA(online, "USDT", "Tether", "2", [
"777",
"66",
]);
console.log("Issued a NIA asset " + JSON.stringify(asset1));
let asset2 = wallet.issueAssetCFA(
online,
"Cfa",
"desc",
"2",
["777"],
null,
);
console.log("Issued a CFA asset: " + JSON.stringify(asset2));
let asset3 = wallet.issueAssetUDA(
online,
"TKN",
"Token",
null,
"2",
"README.md",
[],
);
console.log("Issued a UDA asset: " + JSON.stringify(asset3));
let assets1 = wallet.listAssets([
rgblib.AssetSchema.Nia,
rgblib.AssetSchema.Cfa,
]);
console.log("Assets: " + JSON.stringify(assets1));
let assets2 = wallet.listAssets([]);
console.log("Assets: " + JSON.stringify(assets2));
let [rcvWallet, rcvOnline] = initWallet("3");
let receiveData1 = rcvWallet.blindReceive(
null,
"100",
null,
[PROXY_URL],
"1",
);
console.log("Receive data: " + JSON.stringify(receiveData1));
let receiveData2 = rcvWallet.witnessReceive(
null,
"50",
"60",
[PROXY_URL],
"1",
);
console.log("Receive data: " + JSON.stringify(receiveData2));
let recipientMap = {
[asset1.assetId]: [
{
recipientId: receiveData1.recipientId,
witnessData: null,
amount: "100",
transportEndpoints: [PROXY_URL],
},
],
[asset2.assetId]: [
{
recipientId: receiveData2.recipientId,
witnessData: {
amountSat: "1500",
blinding: null,
},
amount: "50",
transportEndpoints: [PROXY_URL],
},
],
};
let sendResult = wallet.send(
online,
recipientMap,
false,
"1.3",
"1",
false,
);
console.log("Sent: " + JSON.stringify(sendResult));
rcvWallet.refresh(rcvOnline, null, [], false);
wallet.refresh(online, null, [], false);
mine(1);
rcvWallet.refresh(rcvOnline, null, [], false);
wallet.refresh(online, null, [], false);
let rcvAssets = rcvWallet.listAssets([]);
console.log("Assets: " + JSON.stringify(rcvAssets));
let rcvAssetBalance = rcvWallet.getAssetBalance(asset1.assetId);
console.log("Asset balance: " + JSON.stringify(rcvAssetBalance));
wallet.sync(online);
let transfers = wallet.listTransfers(asset1.assetId);
console.log("Transfers: " + JSON.stringify(transfers));
let transactions = wallet.listTransactions(online, true);
console.log("Transactions: " + JSON.stringify(transactions));
let unspents = rcvWallet.listUnspents(rcvOnline, false, false);
console.log("Unspents: " + JSON.stringify(unspents));
try {
let feeEstimation = wallet.getFeeEstimation(online, "7");
console.log("Fee estimation: " + JSON.stringify(feeEstimation));
} catch (e) {
console.log("Error getting fee estimation: " + e);
}
let txid = wallet.sendBtc(
online,
rcvWallet.getAddress(),
"700",
"1.6",
false,
);
console.log("Sent BTC, txid: " + txid);
// these avoid memory leaks, unnecessary here since the program exits
rgblib.dropOnline(online);
wallet.drop();
rgblib.dropOnline(rcvOnline);
rcvWallet.drop();
}
try {
main();
} catch (e) {
console.error("Error running example: " + e);
process.exit(1);
}