forked from ethereum/btcrelay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sampleCall.html
144 lines (118 loc) · 4.51 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
<!DOCTYPE html>
<html lang="en">
<head>
<title>Ethereum: get ether with bitcoin</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">
var web3 = require('web3');
web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
var btcproof = require('bitcoin-proof');
var gRelayAddr = web3.eth.namereg.addr('btcrelay');
var gMerkleProof;
var gBlockHashOfTx;
var gRawTx;
// shows how to use web3 to make an eth_call to the relay contract
// verifyTx returns 1 (success) or 0 (verify failed)
function callVerifyTx(txHash, txIndex, merkleSibling, txBlockHash) {
var RelayContract = web3.eth.contract(btcRelayAbi); // see ./js/btcRelayAbi.js
var contract = RelayContract.at(gRelayAddr);
var objParam = { from: web3.eth.coinbase, gas: 3000000 };
var res = contract.verifyTx.call(txHash, txIndex, merkleSibling, txBlockHash, objParam);
document.getElementById('result').innerText = 'btcrelay verifyTx returned: ' + res.toString(10);
}
function callContract() {
var transHex = $('#transHex').val();
var txHash = '0x' + transHex;
var txBlockHash = '0x' + gBlockHashOfTx;
// web3.js wants 0x prepended
var merkleSibling = gMerkleProof.sibling.map(function(sib) {
return '0x' + sib;
});
callVerifyTx(txHash, 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) {
gRawTx = data.data.tx.hex;
$('#txHexText').val(gRawTx);
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));
})
})
}
$(function() {
// tx[1] of block 360276
$('#transHex').val('be44a9201e8016b1772e7331f3c3c8a4820060307df4603033dc5350a01ac279');
$('#btn-get-tx').click(getTxInfo);
$('#relayAddr').text(gRelayAddr);
});
</script>
</head>
<body>
<div class="container">
<div class="logo">
<img src="./images/ethereum-logo-small.png"/>
</div>
<p class="lead">btcrelay at <span id="relayAddr"></span></p>
<p>This is a sample of calling the verifyTx method on the btcrelay contract
</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>
<div class="col-md-9">
<p class="form-control-static" id="txBlockHash">0</p>
</div>
</div>
<br><br><br>
<button type="button" class="btn btn-default" onclick="callContract();">Call verifyTx</button>
</form>
</div>
<div id="result"></div>
<div class="footer-logo">
<img src="./images/ETH_DEV_LOGO_LARGE.svg"/>
</div>
</div>
</body>
</html>