|
| 1 | +ethers-wallet |
| 2 | +============= |
| 3 | + |
| 4 | +Complete Ethereum wallet implementation in JavaScript. |
| 5 | + |
| 6 | +Features |
| 7 | +- Keep your private keys in the browser |
| 8 | +- Small (~155kb compressed; hopefully under 100kb soon) |
| 9 | +- MIT licensed (with a few exceptions, which we are migrating off of; see below) |
| 10 | + |
| 11 | +*NOTE: This is still very beta; please only use it on the testnet for now, or with VERY small amounts of ether on the livenet that you are willing to lose due to bugs.* |
| 12 | + |
| 13 | + |
| 14 | +Wallet API |
| 15 | +---------- |
| 16 | + |
| 17 | +```javascript |
| 18 | +// A private key can be specified as a 32 byte buffer or hexidecimal string |
| 19 | +var privateKey = new Wallet.utils.Buffer([ |
| 20 | + 0x31, 0x41, 0x59, 0x26, 0x53, 0x58, 0x97, 0x93, |
| 21 | + 0x23, 0x84, 0x62, 0x64, 0x33, 0x83, 0x27, 0x95, |
| 22 | + 0x02, 0x88, 0x41, 0x97, 0x16, 0x93, 0x99, 0x37, |
| 23 | + 0x51, 0x05, 0x82, 0x09, 0x74, 0x94, 0x45, 0x92 |
| 24 | +]) |
| 25 | + |
| 26 | +// or equivalently: |
| 27 | +var privateKey = '0x3141592653589793238462643383279502884197169399375105820974944592' |
| 28 | + |
| 29 | +// Create a wallet object |
| 30 | +var wallet = new Wallet(privateKey) |
| 31 | + |
| 32 | +// Wallet address |
| 33 | +console.log(wallet.address) |
| 34 | +/// "0x7357589f8e367c2C31F51242fB77B350A11830F3" |
| 35 | + |
| 36 | +// ICAP Addresses |
| 37 | +Wallet.getIcapAddress(wallet.address) |
| 38 | +/// "XE39DH16QOXYG5JY9BYY6JGZW8ORUPBX71V" |
| 39 | + |
| 40 | +Wallet.getIcapAddress("XE39DH16QOXYG5JY9BYY6JGZW8ORUPBX71V") |
| 41 | +/// "XE39DH16QOXYG5JY9BYY6JGZW8ORUPBX71V" |
| 42 | + |
| 43 | +// Get checksummed address (from ICAP) |
| 44 | +Wallet.getAddress("XE39DH16QOXYG5JY9BYY6JGZW8ORUPBX71V") |
| 45 | +/// "0x7357589f8e367c2C31F51242fB77B350A11830F3" |
| 46 | + |
| 47 | +// Get checksummed addresses (from unchecksumed) |
| 48 | +Wallet.getAddress("0x7357589f8e367c2c31f51242fb77b350a11830f3") |
| 49 | +/// "0x7357589f8e367c2C31F51242fB77B350A11830F3" |
| 50 | + |
| 51 | +// Detect address checksum errors (notice the last "f" should be lowercase) |
| 52 | +Wallet.getAddress('0x7357589f8e367c2c31f51242fb77b350a11830F3') |
| 53 | +/// Error: invalid checksum address |
| 54 | + |
| 55 | +// Sign transactions |
| 56 | +wallet.sign({ |
| 57 | + to: "0x06B5955A67D827CDF91823E3bB8F069e6c89c1D6", |
| 58 | + gasLimit: 3000000, |
| 59 | + gasPrice: "0x1000", |
| 60 | + value: "0x1000" |
| 61 | +}) |
| 62 | + |
| 63 | +``` |
| 64 | + |
| 65 | +Contract API |
| 66 | +------------ |
| 67 | + |
| 68 | +```javascript |
| 69 | +// Load a normal web3 object (you need a local RPC-enabled ethereum node running) |
| 70 | +var web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545')) |
| 71 | + |
| 72 | +// Create your wallet |
| 73 | +var wallet = new Wallet('0x3141592653589793238462643383279502884197169399375105820974944592') |
| 74 | + |
| 75 | +console.log(wallet.address); |
| 76 | +/// "0x7357589f8e367c2C31F51242fB77B350A11830F3" |
| 77 | + |
| 78 | +// Find an existing contract address and ABI |
| 79 | +// See: https://gist.github.com/ricmoo/e78709e075ff8082a86c875ac062c3c3 |
| 80 | +var simpleStorageAddress = '0xdfaf84077cF4bCECA4F79d167F47041Ed3006D5b' |
| 81 | +var simpleStorageAbi = [ |
| 82 | + { |
| 83 | + "constant":true, |
| 84 | + "inputs":[], |
| 85 | + "name":"getValue", |
| 86 | + "outputs":[{"name":"","type":"string"}], |
| 87 | + "type":"function" |
| 88 | + }, { |
| 89 | + "constant":false, |
| 90 | + "inputs":[{"name":"value","type":"string"}], |
| 91 | + "name":"setValue", |
| 92 | + "outputs":[], |
| 93 | + "type":"function" |
| 94 | + }, { |
| 95 | + "anonymous":false, |
| 96 | + "inputs":[ |
| 97 | + {"indexed":false,"name":"oldValue","type":"string"}, |
| 98 | + {"indexed":false,"name":"newValue","type":"string"} |
| 99 | + ], |
| 100 | + "name":"valueChanged", |
| 101 | + "type":"event" |
| 102 | + } |
| 103 | +]; |
| 104 | + |
| 105 | +// Get the contract |
| 106 | +var contract = wallet.getContract(web3, simpleStorageAddress, simpleStorageAbi) |
| 107 | + |
| 108 | +// Set up events |
| 109 | +contract.onvaluechanged = function(oldValue, newValue) { |
| 110 | + console.log('Value Changed from "' + oldValue + '" to "' + newValue + '".') |
| 111 | +} |
| 112 | + |
| 113 | +// Call constant methods, which don't alter state (free). |
| 114 | +// Returns a promise. |
| 115 | +contract.getValue().then(function(value) { |
| 116 | + console.log('Value is "' + value + '".') |
| 117 | +}) |
| 118 | + |
| 119 | +// Call state-changing methods (which will cost you ether, so use testnet to test!) |
| 120 | +// Returns a promise. |
| 121 | +contract.setValue("Hello World").then(function(txid) { |
| 122 | + console.log('txid: ' + txid); |
| 123 | +}); |
| 124 | + |
| 125 | +// Include ether with a state-changing call, or custom gasLimit or gasPrice |
| 126 | +var options = { |
| 127 | + gasPrice: 1000 // in wei (default: from network) |
| 128 | + gasLimit: 3000000, // is gas (default: 3000000) |
| 129 | + value: 1000 // in wei (default: 0) |
| 130 | +} |
| 131 | +contract.setValue("Hello World", options).then(function(txid) { |
| 132 | + console.log('txid: ' + txid); |
| 133 | +}); |
| 134 | + |
| 135 | +``` |
| 136 | + |
| 137 | + |
| 138 | +License |
| 139 | +------- |
| 140 | + |
| 141 | +MIT Licensed, with the exceptions: |
| 142 | +- The Solidity encoder/decoder (LGPL) |
| 143 | +- RLP (MPL-2.0) |
| 144 | + |
| 145 | +We are working on our own implementations so we can move off of them and have a completely MIT licensed implementation in the near future. |
| 146 | + |
| 147 | +Stay tuned! |
0 commit comments