forked from ethereum/btcrelay
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sampleCall.html
193 lines (160 loc) · 7.7 KB
/
sampleCall.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
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
<!DOCTYPE html>
<html dir="ltr" lang="en-US">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta name="description" content="btc relay, ethereum, bitcoin, bitcoin relay, smart contracts, sidechain, peg" />
<link rel="apple-touch-icon" sizes="57x57" href="images/favicons/apple-icon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="images/favicons/apple-icon-60x60.png">
<link rel="apple-touch-icon" sizes="72x72" href="images/favicons/apple-icon-72x72.png">
<link rel="apple-touch-icon" sizes="76x76" href="images/favicons/apple-icon-76x76.png">
<link rel="apple-touch-icon" sizes="114x114" href="images/favicons/apple-icon-114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="images/favicons/apple-icon-120x120.png">
<link rel="apple-touch-icon" sizes="144x144" href="images/favicons/apple-icon-144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="images/favicons/apple-icon-152x152.png">
<link rel="apple-touch-icon" sizes="180x180" href="images/favicons/apple-icon-180x180.png">
<link rel="icon" type="image/png" sizes="192x192" href="images/favicons/android-icon-192x192.png">
<link rel="icon" type="image/png" sizes="32x32" href="images/favicons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="96x96" href="images/favicons/favicon-96x96.png">
<link rel="icon" type="image/png" sizes="16x16" href="images/favicons/favicon-16x16.png">
<link rel="manifest" href="images/favicons/manifest.json">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="images/favicons/ms-icon-144x144.png">
<meta name="theme-color" content="#ffffff">
<link href='https://fonts.googleapis.com/css?family=Lato:400,300,300italic,400italic,700,700italic|Montserrat:400,700' rel='stylesheet' type='text/css'>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>BTC Relay verifyTx eth_call sample</title>
<link rel="stylesheet" href="./css/bootstrap.min.css">
<link rel="stylesheet" href="./css/dapp.css">
<script src="./js/jquery-2.1.3.min.js"></script>
<script src="./js/bootstrap.min.js"></script>
<script src="./js/btcRelayAbi.js"></script>
<script src="./js/bignumber.js"></script>
<script src="./js/web3.min.js"></script>
<script src="./js/bitcoin-proof.js"></script>
<script type="text/javascript">
if (typeof web3 !== 'undefined') {
// Web3 has been injected by the browser (Mist/MetaMask)
web3 = new Web3(web3.currentProvider);
} else {
// fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
web3 = new Web3(new Web3.providers.HttpProvider("https://mainnet.infura.io:8545"));
}
var btcproof = require('bitcoin-proof');
// var gRelayAddr = web3.eth.namereg.addr('btcrelay');
var gRelayAddr = '0x41f274c0023f83391de4e0733c609df5a124c3d4'; // mainnet
var gMerkleProof;
var gBlockHashOfTx;
var gFeeVerifyFinney;
var RelayContractClass = web3.eth.contract(btcRelayAbi); // see ./js/btcRelayAbi.js
var gRelayContract = RelayContractClass.at(gRelayAddr);
// shows how to use web3 to make an eth_call to the relay contract
// verifyTx returns 1 (success) or 0 (verify failed)
function callVerifyTx(txBytes, txIndex, merkleSibling, txBlockHash) {
// gFeeVerifyFinney is transferred! coinbase must have it or verifyTx fails
var feeWei = web3.toWei(gFeeVerifyFinney, 'finney');
var objParam = { from: '0xde0b295669a9fd93d5f28d9ec85e40f4cb697bae', value: feeWei, gas: 3000000 };
var res = gRelayContract.verifyTx.call(txBytes, txIndex, merkleSibling, txBlockHash, objParam);
document.getElementById('result').innerText = res.toString(16);
}
function callContract() {
var txBytes = '0x' + $('#txHexText').val();
// console.log('txBytes: ' + txBytes)
var txBlockHash = '0x' + gBlockHashOfTx;
// web3.js wants 0x prepended
var merkleSibling = gMerkleProof.sibling.map(function(sib) {
return '0x' + sib;
});
callVerifyTx(txBytes, gMerkleProof.txIndex, merkleSibling, txBlockHash);
}
// includes sample of using the bitcoin-proof module
function getTxInfo() {
var txid = $('#transHex').val();
var urlJsonTx = "https://btc.blockr.io/api/v1/tx/raw/" + txid;
$.getJSON(urlJsonTx, function(data) {
$('#txHexText').val(data.data.tx.hex);
var blockNum = data.data.tx.blockhash;
var blockInfoUrl = "http://btc.blockr.io/api/v1/block/raw/"+blockNum;
$.getJSON(blockInfoUrl, function(res) {
gBlockHashOfTx = res.data.hash;
$('#txBlockHash').text(gBlockHashOfTx)
var txIndex;
for (var key in res.data.tx) {
if (res.data.tx[key] == txid) {
txIndex = key;
break;
}
}
gMerkleProof = btcproof.getProof(res.data.tx, txIndex);
console.log('merkle proof: ', gMerkleProof)
$('#mProof').val(JSON.stringify(gMerkleProof));
gFeeVerifyFinney = web3.fromWei(gRelayContract.getFeeAmount.call('0x'+gBlockHashOfTx), 'finney');
$('#feeVerifyTx').text(gFeeVerifyFinney);
})
})
}
$(function() {
// tx[1] of block 408000
$('#transHex').val('dd059634699e85b51af4964ab97d5e75fb7cd86b748d0ee1c537ca1850101dc7');
$('#btn-get-tx').click(getTxInfo);
$('#relayAddr').text(gRelayAddr);
});
</script>
</head>
<body>
<div class="container">
<div class="logo">
<img src="./images/BTCRelayLogo.png"/>
</div>
<p class="lead">BTC Relay at <span id="relayAddr"></span></p>
<p>This is a sample of calling <a href="https://github.com/ethereum/btcrelay/tree/master#verifytxrawtransaction-transactionindex-merklesibling-blockhash">verifyTx</a>
</p>
<h2>1.</h2>
<div class="row">
<form class="form">
<div class="form-group">
<label for="transHex">Bitcoin Transaction Hash</label>
<input type="text" class="form-control" id="transHex" size="64" maxlength="64"></input>
</div>
<button type="button" class="btn btn-default" id="btn-get-tx">Lookup</button>
</form>
</div>
<h2>2.</h2>
<div class="row">
<form>
<div class="form-group">
<label for="txHexText">Raw Transaction</label>
<textarea readonly class="form-control" name="textarea" id="txHexText" rows="6" cols="50"></textarea>
</div>
<div class="form-group">
<label for="mProof">Merkle Proof</label>
<textarea readonly class="form-control" name="textarea" id="mProof" rows="6" cols="50"></textarea>
</div>
<div class="form-group">
<label class="col-md-1 control-label">Block Hash</label>
<p class="form-control-static" id="txBlockHash">0</p>
</div>
<div class="form-group">
<label class="col-md-2 control-label">Verification fee milli-ether</label>
<p class="form-control-static" id="feeVerifyTx">?</p>
</div>
<button type="button" class="btn btn-default" onclick="callContract();">Call verifyTx</button>
</form>
</div>
<div class="row">
<div>BTC Relay verifyTx returned: <span id="result"></span></div>
</div>
<hr>
<div class="row">
<div>View page source to see the main steps:</div>
<ul>
<li>Getting a raw transaction and its index from a hash</li>
<li>Using <a href="https://www.npmjs.com/package/bitcoin-proof">bitcoin-proof </a>to get a Merkle proof</li>
<li>Calling <a href="https://github.com/ethereum/btcrelay#verifytxrawtransaction-transactionindex-merklesibling-blockhash">verifyTx</a></li>
</ul>
</div>
<div class="footer-logo">
<img src="./images/ethereum-logo-small.png"/>
</div>
</div>
</body>
</html>