forked from trezor/connect
/
signtx-paytoaddress.html
111 lines (93 loc) · 3.75 KB
/
signtx-paytoaddress.html
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
<!DOCTYPE html>
<html>
<head>
<title>TREZOR Sign Transaction Test</title>
<script>
// this example requires a device loaded with seed:
// alcohol woman abuse must during monitor noble actual mixed trade anger aisle
// Python tests:
// https://github.com/trezor/python-trezor/blob/master/tests/device_tests/test_msg_signtx.py
function trezorSignTx(testId) {
// legacy input
var inp1 = {
//address_n: [44 | 0x80000000, 0 | 0x80000000, 0 | 0x80000000, 0, 0],
address_n: [0],
prev_index: 0,
prev_hash: "d5f65ee80147b4bcc70b75e4bbf2d7382021b871bd8867ef8fa525ef50864882"
};
// segwit input
var inp2 = {
address_n: [49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 1, 0],
amount: 123456789,
prev_index: 0,
prev_hash: "20912f98ea3ed849042efed0fdac8cb4fc301961c5988cba56902d8ffb61c337",
script_type: "SPENDP2SHWITNESS"
};
// legacy outputs
var out1 = {
address: '1MJ2tj2ThBE62zXbBYA5ZaN3fdve5CPAz1',
amount: 390000 - 10000,
script_type: 'PAYTOADDRESS'
}
//
var out2 = {
//address_n: [44 | 0x80000000, 0 | 0x80000000, 0 | 0x80000000, 1, 0],
address_n: [1],
amount: 80000,
script_type: 'PAYTOADDRESS'
}
// segwit outputs
var out3 = {
address: "mhRx1CeVfaayqRwq5zgRQmD7W5aWBfD5mC",
amount: 12300000,
script_type: "PAYTOADDRESS"
}
var out4 = {
address_n: [49 | 0x80000000, 1 | 0x80000000, 0 | 0x80000000, 1, 0],
script_type: "PAYTOP2SHWITNESS",
amount: 123456789 - 11000 - 12300000,
}
var inputs, outputs;
switch(testId) {
case 0: // One output external
inputs = [ inp1 ];
outputs = [ out1 ];
TrezorConnect.setCurrency('btc');
break;
case 1: // First output external, second output internal
var cloneOfOut1 = JSON.parse(JSON.stringify(out1));
cloneOfOut1.amount = out1.amount - out2.amount;
inputs = [ inp1 ];
outputs = [ cloneOfOut1, out2 ];
TrezorConnect.setCurrency('btc');
break;
case 2: // Segwit input, Segwit change output + external output
inputs = [ inp2 ];
outputs = [ out3, out4 ];
TrezorConnect.setCurrency('testnet');
break;
}
TrezorConnect.signTx(inputs, outputs, function (response) {
if (response.success) {
console.log('Serialized TX:', response.serialized_tx); // tx in hex
console.log('Signatures:', response.signatures); // array of signatures, in hex
} else {
console.error('Error:', response.error); // error message
}
document.getElementById("response").innerHTML = JSON.stringify(response, undefined, 2);
});
}
</script>
</head>
<body>
<p>This example requires a device loaded with seed:</p>
<pre>alcohol woman abuse must during monitor noble actual mixed trade anger aisle</pre>
<button onclick="trezorSignTx(0)">One output external</button><br/>
<button onclick="trezorSignTx(1)">First output external, second output internal</button><br/>
<p>This example requires a device loaded with seed:</p>
<pre>all all all all all all all all all all all all</pre>
<button onclick="trezorSignTx(2)">Segwit input, Segwit change output + external output</button>
<pre id="response"></pre>
<script src="../connect.js?r=1"></script>
</body>
</html>