-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_web.html
More file actions
169 lines (148 loc) · 9.52 KB
/
example_web.html
File metadata and controls
169 lines (148 loc) · 9.52 KB
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
<html>
<body>
<script src="dist/ethlightjs.min.js"></script>
<script type="text/javascript" src="node_modules/hooked-web3-provider/build/hooked-web3-provider.js"></script>
<script type="text/javascript" src="node_modules/web3/dist/web3.js"></script>
<script>
var password = 'mypassword'
var addresses = undefined
var contract = undefined
console.log(web3.version)
function setWeb3Provider(keystore) {
var web3Provider = new HookedWeb3Provider({
host: "http://104.236.65.136:8545",
//host: "http://localhost:8545",
transaction_signer: keystore
});
web3.setProvider(web3Provider);
}
function checkNonce(){
var addr = document.getElementById('sendFrom').value
var nonce = web3.eth.getTransactionCount(addr, function(err, nonce){
document.getElementById('nonce').value = nonce
})
}
function getBalances() {
web3.eth.getBalance(addresses[0], function(err, bal){
web3.eth.getTransactionCount(addresses[0], function(err, nonce) {
document.getElementById('addr0').innerHTML = addresses[0] + ' (Bal: ' + (bal / 1.0e18) + ' ETH, Nonce: ' + nonce + ')'
})
})
web3.eth.getBalance(addresses[1], function(err, bal){
web3.eth.getTransactionCount(addresses[1], function(err, nonce) {
document.getElementById('addr1').innerHTML = addresses[1] + ' (Bal: ' + (bal / 1.0e18) + ' ETH, Nonce: ' + nonce + ')'
})
})
web3.eth.getBalance(addresses[2], function(err, bal){
web3.eth.getTransactionCount(addresses[2], function(err, nonce) {
document.getElementById('addr2').innerHTML = addresses[2] + ' (Bal: ' + (bal / 1.0e18) + ' ETH, Nonce: ' + nonce + ')'
})
})
}
function setSeed() {
var seed = document.getElementById('seed').value
var keystore = new ethlightjs.keystore(seed, password)
var numAddr = 3
keystore.generateNewAddress(password, numAddr)
addresses = keystore.getAddresses()
keystore.passwordProvider = function (callback) {callback(null, password)}
setWeb3Provider(keystore);
getBalances();
}
function randomSeed() {
var randomSeed = ethlightjs.keystore.generateRandomSeed()
document.getElementById('seed').value = randomSeed
}
function sendEth() {
var fromAddr = '0x' + document.getElementById('sendFrom').value
var toAddr = '0x' + document.getElementById('sendTo').value
var valueEth = document.getElementById('sendValueAmount').value
var value = parseFloat(valueEth)*1.0e18
var accNonce = parseInt(document.getElementById('nonce').value)
var gasPrice = 1000000000000
var gas = 50000
web3.eth.sendTransaction({from: fromAddr, to: toAddr, value: value, gasPrice: gasPrice, gas: gas, nonce: accNonce}, function (err, txhash) {
console.log('error: ' + err)
console.log('txhash: ' + txhash)
})
}
function createContract() {
var fromAddr = '0x' + document.getElementById('sendFrom').value
var bytecode = '0x' + document.getElementById('bytecode').value
var accNonce = parseInt(document.getElementById('nonce').value)
var abi = JSON.parse(document.getElementById('abi').value)
var gasPrice = 1000000000000
var gas = 300000
web3.eth.contract(abi).new({from: fromAddr, data: bytecode, gas: gas, gasPrice: gasPrice}, function (err, c) {
if(err) {
console.error(err);
return;
// callback fires twice, we only want the second call when the contract is deployed
} else if(c.address){
console.log(c)
contract = c;
document.getElementById('contractAddr').value = contract.address;
}
});
}
function registerKey() {
var key = parseInt(document.getElementById('keyToRegister').value)
var fromAddr = '0x' + document.getElementById('sendFrom').value
var accNonce = parseInt(document.getElementById('nonce').value)
contract.register(key, {from: fromAddr, gas: 300000, gasPrice: 1000000000000, nonce: accNonce}, function (err, txhash) {
console.log(txhash);
})
}
function setValue() {
var key = parseInt(document.getElementById('keyToSet').value)
var val = parseInt(document.getElementById('valueToSet').value)
var fromAddr = '0x' + document.getElementById('sendFrom').value
var accNonce = parseInt(document.getElementById('nonce').value)
contract.setValue(key, val, {from: fromAddr, gas: 300000, gasPrice: 1000000000000, nonce: accNonce}, function (err, txhash) {
console.log(txhash);
})
}
function getContractData() {
var key = parseInt(document.getElementById('keyToQuery').value)
contract.getOwner(key, function (err, owner) {
contract.getValue(key, function (err, val) {
document.getElementById('keyOwner').innerHTML = 'Key Owner: ' + (owner.slice(2))
document.getElementById('keyValue').innerHTML = 'Key Value: ' + val
})
})
}
</script>
<h1>LightWallet</h1>
<h2>Seed</h2>
<div><input type="text" id="seed" value="unhappy nerve cancel reject october fix vital pulse cash behind curious bicycle" size="80"></input><button onclick="setSeed()">Set Seed</button><button onclick="randomSeed()">Random Seed</button></div>
<h2>Addresses</h2>
<button onclick="getBalances()">Refresh</button>
<div id="addr0"></div>
<div id="addr1"></div>
<div id="addr2"></div>
<div id="signedTx"></div>
<h2>Sender address</h2>
<div>From: <input type="text" id="sendFrom"></input></div>
<div><button onclick="checkNonce()">Check nonce</button></div>
<div>Nonce: <input type="number" id="nonce" value=0></div>
<h2>Transfer ether</h2>
<div>To: <input type="text" id="sendTo"></input></div>
<div>Ether: <input type="text" id="sendValueAmount"></div>
<div><button onclick="sendEth()">Send Ether</button></div>
<h2>Create Contract</h2>
<div>Bytecode: <input id="bytecode" value="6060604052610381806100136000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900480630ff4c9161461006557806329507f731461008c5780637b8d56e3146100a5578063c41a360a146100be578063f207564e146100fb57610063565b005b610076600480359060200150610308565b6040518082815260200191505060405180910390f35b6100a36004803590602001803590602001506101b3565b005b6100bc60048035906020018035906020015061026e565b005b6100cf600480359060200150610336565b604051808273ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b61010c60048035906020015061010e565b005b60006000600050600083815260200190815260200160002060005060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1614156101af57336000600050600083815260200190815260200160002060005060000160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908302179055505b5b50565b3373ffffffffffffffffffffffffffffffffffffffff166000600050600084815260200190815260200160002060005060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16141561026957806000600050600084815260200190815260200160002060005060000160006101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908302179055505b5b5050565b3373ffffffffffffffffffffffffffffffffffffffff166000600050600084815260200190815260200160002060005060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff161415610303578060006000506000848152602001908152602001600020600050600101600050819055505b5b5050565b600060006000506000838152602001908152602001600020600050600101600050549050610331565b919050565b60006000600050600083815260200190815260200160002060005060000160009054906101000a900473ffffffffffffffffffffffffffffffffffffffff16905061037c565b91905056"></input></div>
<div>ABI: <input id="abi" value='[{"constant":true,"inputs":[{"name":"key","type":"uint256"}],"name":"getValue","outputs":[{"name":"value","type":"uint256"}],"type":"function"},{"constant":false,"inputs":[{"name":"key","type":"uint256"},{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"type":"function"},{"constant":false,"inputs":[{"name":"key","type":"uint256"},{"name":"newValue","type":"uint256"}],"name":"setValue","outputs":[],"type":"function"},{"constant":true,"inputs":[{"name":"key","type":"uint256"}],"name":"getOwner","outputs":[{"name":"owner","type":"address"}],"type":"function"},{"constant":false,"inputs":[{"name":"key","type":"uint256"}],"name":"register","outputs":[],"type":"function"}]'></input></div>
<div><button onclick="createContract()">Create contract</button></div>
<div>Contract address: <input id="contractAddr"></input></div>
<h2>Execute functions</h2>
<div>Key to Register: <input id="keyToRegister" value="123"></input></div>
<div><button onclick="registerKey()">Register Key</button></div>
<div>Set value: Key: <input id="keyToSet" value="123"></input> Value: <input id="valueToSet" value="456"></input></div>
<div><button onclick="setValue()">Set Value</button></div>
<h3>Get Contract Data</h3>
<div>Get owner and value from key: <input id="keyToQuery" value="123"></input></div>
<div><button onclick="getContractData()">Get Data</button></div>
<div id="keyOwner"></div>
<div id="keyValue"></div>
</body>
</html>