forked from BitBoxSwiss/bitbox02-api-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo.js
302 lines (274 loc) · 10.5 KB
/
demo.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
import { isErrorAbort, constants, getDevicePath, BitBox02API, getKeypathFromString, HARDENED } from './bitbox02-api.js'
function reset() {
document.getElementById("demo").disabled = false;
document.getElementById("pairing").style.display = "none";
document.getElementById("pairingOK").disabled = true;
document.getElementById("initialized").style.display = "none";
document.getElementById("intro").style.display = "flex";
}
class BitBox02 {
constructor(logout) {
this.logout = logout;
}
async init() {
const pairingOKButton = document.getElementById("pairingOK");
const initializedDiv = document.getElementById("initialized");
document.getElementById("demo").disabled = true;
try {
const devicePath = await getDevicePath();
this.api = new BitBox02API(devicePath);
await this.api.connect(
pairingCode => {
document.getElementById("intro").style.display = "none";
document.getElementById("pairing").style.display = "flex";
document.getElementById("pairingCode").innerHTML = pairingCode.replace("\n", "<br/>");
},
() => new Promise(resolve => {
pairingOKButton.disabled = false;
pairingOKButton.addEventListener("click", resolve);
}),
attestationResult => {
console.log("Attestation check:", attestationResult);
},
this.logout,
status => {
console.log(status);
}
)
} catch (e) {
alert(e);
reset();
return;
}
switch (this.api.firmware().Product()) {
case constants.Product.BitBox02Multi:
console.log("This is a BitBox02 Multi");
let elements = document.getElementsByClassName("bb02-multi");
for (let i = 0; i < elements.length; i++) {
elements[i].disabled = false;
}
break;
case constants.Product.BitBox02BTCOnly:
console.log("This is a BitBox02 BTC-only");
break;
}
document.getElementById("pairing").style.display = "none";
document.getElementById("intro").style.display = "none";
initializedDiv.style.display = "flex";
}
}
let device;
const runDemo = async () => {
device = new BitBox02(reset)
await device.init();
}
// ---- Demo buttons
// Start the demo
document.getElementById("demo").addEventListener("click", runDemo);
// Close the connection (logout).
document.getElementById("close").addEventListener("click", () => {
device.api.close();
});
// Get ethereum xpub for given keypath
ethPub.addEventListener("click", async () => {
const ethPub = await device.api.ethGetRootPubKey("m/44'/60'/0'/0");
alert(ethPub);
});
// Get ethereum address for given keypath
// Only displays address on device, does not return. For verification, derive address from xpub
ethAddr.addEventListener("click", async () => {
await device.api.ethDisplayAddress("m/44'/60'/0'/0/0");
});
// Sign ethereum transaction
ethSign.addEventListener("click", async () => {
const signingData = {
keypath: "m/44'/60'/0'/0/0", // mainnet tx needs a mainnet keypath
chainId: 1, // mainnet tx
tx: {
nonce: new Uint8Array([0]),
gasPrice: new Uint8Array([76, 153, 237, 154, 0]),
gasLimit: new Uint8Array([82, 8]),
to: new Uint8Array([4, 242, 100, 207, 52, 68, 3, 19, 180, 160, 25, 42, 53, 40, 20, 251, 233, 39, 184, 133]),
value: new Uint8Array([6, 240, 91, 89, 211, 178, 0, 0]),
data: new Uint8Array([])
}
}
try {
const sig = await device.api.ethSignTransaction(signingData);
console.log(sig);
} catch(e) {
alert(e);
}
});
// Sign "hello world" ethereum message
ethSignMsg.addEventListener("click", async () => {
try {
const sig = await device.api.ethSignMessage({
keypath: "m/44'/60'/0'/0/0",
// "hello world"
message: new Uint8Array([104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100])
}
);
console.log(sig);
} catch(e) {
alert(e)
}
});
// Display single-sig BTC address on device for verification
document.getElementById("btcAddressSimple").addEventListener("click", async () => {
await device.api.btcDisplayAddressSimple(
constants.messages.BTCCoin.BTC,
getKeypathFromString("m/49'/0'/0'/0/0"),
constants.messages.BTCScriptConfig_SimpleType.P2WPKH_P2SH,
);
});
// Mock sample single-sig BTC transaction
function makeExampleTx(keypathAccount) {
const inputs = [
{
"prevOutHash": Uint8Array.from([0xc5, 0x8b, 0x7e, 0x3f, 0x12, 0x00, 0xe0, 0xc0, 0xec, 0x9a, 0x5e, 0x81, 0xe9, 0x25, 0xba, 0xfa, 0xce, 0x2c, 0xc1, 0xd4, 0x71, 0x55, 0x14, 0xf2, 0xd8, 0x20, 0x5b, 0xe2, 0x50, 0x8b, 0x48, 0xee]),
"prevOutIndex": 1,
"prevOutValue": "60005000", // satoshis as a decimal string
"sequence": 0xFFFFFFFF,
"keypath": keypathAccount.concat([0, 0]),
"prevTx": {
"version": 1,
"locktime": 0,
"inputs": [
{
"prevOutHash": new Uint8Array(32).fill(49), // arbitrary constant,
"prevOutIndex": 0,
"signatureScript": new TextEncoder().encode("some signature script"),
"sequence": 0xFFFFFFFF,
}
],
"outputs": [{
"value": "60005000",
"pubkeyScript": new TextEncoder().encode("some pubkey script"),
}],
},
},
{
"prevOutHash": Uint8Array.from([0xc5, 0x8b, 0x7e, 0x3f, 0x12, 0x00, 0xe0, 0xc0, 0xec, 0x9a, 0x5e, 0x81, 0xe9, 0x25, 0xba, 0xfa, 0xce, 0x2c, 0xc1, 0xd4, 0x71, 0x55, 0x14, 0xf2, 0xd8, 0x20, 0x5b, 0xe2, 0x50, 0x8b, 0x48, 0xee]),
"prevOutIndex": 1,
"prevOutValue": "60005000", // satoshis as a decimal string
"sequence": 0xFFFFFFFF,
"keypath": keypathAccount.concat([0, 1]),
"prevTx": {
"version": 1,
"locktime": 0,
"inputs": [
{
"prevOutHash": new Uint8Array(32).fill(49), // arbitrary constant,
"prevOutIndex": 0,
"signatureScript": new TextEncoder().encode("some signature script"),
"sequence": 0xFFFFFFFF,
}
],
"outputs": [{
"value": "60005000",
"pubkeyScript": new TextEncoder().encode("some pubkey script"),
}],
},
}
];
const outputs = [
{
"ours": true, // change
"keypath": keypathAccount.concat([1, 0]),
"value": "100000000", // satoshis as a decimal string
},
{
"ours": false,
"type": constants.messages.BTCOutputType.P2WSH,
"hash": new Uint8Array(32).fill(49), // arbitrary constant
"value": "20000000", // satoshis as a decimal string,
},
];
const version = 1;
const locktime = 0;
return { inputs, outputs, version, locktime };
}
// Sign single-sig BTC transaction
document.getElementById("btcSignSimple").addEventListener("click", async () => {
const keypathAccount = getKeypathFromString("m/84'/0'/0'");
const { inputs, outputs, version, locktime } = makeExampleTx(keypathAccount);
try {
const signatures = await device.api.btcSignSimple(
constants.messages.BTCCoin.BTC,
constants.messages.BTCScriptConfig_SimpleType.P2WPKH,
keypathAccount,
inputs,
outputs,
version,
locktime,
);
console.log("Signatures: ", signatures);
} catch(err) {
console.log(err);
if (isErrorAbort(err)) {
alert("aborted by user");
} else {
alert(err.Message);
}
}
});
// Mock sample multi-sig BTC transaction
async function makeExampleMultisigAccount(keypathAccount) {
const coin = constants.messages.BTCCoin.BTC;
const ourXPub = await device.api.btcXPub(coin, keypathAccount, constants.messages.BTCXPubType.ZPUB, false);
// One of the xpubs must be ours.
// The xpubs can be provided in any version (xpub, ypub, zpub, etc.).
const xpubs = [
ourXPub,
"Zpub75oAskzNv1fFuPc4ue6dVyZETFdroU4Bw2NNTL6Yz8WzLx3athVwRKmuMoSmwqXFL5jw5ZEdbinsW5TSEiHQTFUuZg4Pk2v2mma1ERpHWgq",
];
const account = {
"coin": coin,
"keypathAccount": keypathAccount,
"threshold": 1,
"xpubs": xpubs,
"ourXPubIndex": 0,
};
const getName = async () => prompt("Please give a name for the multisig account (max 30 chars)");
await device.api.btcMaybeRegisterScriptConfig(account, getName);
return account;
}
// Display single-sig BTC address on device for verification
document.getElementById("btcAddressMultisig").addEventListener("click", async () => {
const keypath = getKeypathFromString("m/48'/0'/0'/2'/0/0");
const keypathAccount = keypath.slice(0, keypath.length - 2);
try {
const account = await makeExampleMultisigAccount(keypathAccount);
await device.api.btcDisplayAddressMultisig(account, keypath);
} catch(err) {
if (isErrorAbort(err)) {
alert("aborted by user");
} else {
alert(err.Message);
}
}
});
// Sign multi-sig BTC transaction
document.getElementById("btcSignMultisig").addEventListener("click", async () => {
const coin = constants.messages.BTCCoin.BTC;
const keypathAccount = getKeypathFromString("m/48'/0'/0'/2'");
const account = await makeExampleMultisigAccount(keypathAccount);
const { inputs, outputs, version, locktime } = makeExampleTx(keypathAccount);
try {
const signatures = await device.api.btcSignMultisig(
account,
inputs,
outputs,
version,
locktime,
);
console.log("Signatures: ", signatures);
} catch(err) {
if (isErrorAbort(err)) {
alert("aborted by user");
} else {
alert(err.Message);
}
}
});