|
| 1 | + |
| 2 | +//eth |
| 3 | +function init() { |
| 4 | + web3 = new Web3(); |
| 5 | + web3.setProvider(new web3.providers.HttpProvider("https://api.myetherapi.com/eth")); |
| 6 | +} |
| 7 | +function version() { |
| 8 | + alert(web3.version); |
| 9 | +} |
| 10 | + |
| 11 | + |
| 12 | +//wallet |
| 13 | +function createWallet(params) { |
| 14 | + var paramsjson = JSON.parse(params); |
| 15 | + var callid = paramsjson.callid; |
| 16 | + var numberOfAccounts = paramsjson.numberOfAccounts; |
| 17 | + var entropy = paramsjson.entropy; |
| 18 | + var wallet = web3.eth.accounts.wallet.create(numberOfAccounts, entropy); |
| 19 | + var accountInfo = new Object(); |
| 20 | + accountInfo.privatekey = wallet[0].privateKey; |
| 21 | + accountInfo.address = wallet[0].address; |
| 22 | + notifyClient(callid, 0, accountInfo); |
| 23 | +} |
| 24 | + |
| 25 | +function createWalletWithWord(params) { |
| 26 | + var paramsjson = JSON.parse(params); |
| 27 | + var callid = paramsjson.callid; |
| 28 | + var mnemonic = bip39.generateMnemonic(); |
| 29 | + var seed = bip39.mnemonicToSeed(mnemonic); |
| 30 | + var hdWallet = hdkey.fromMasterSeed(seed); |
| 31 | + var key1 = hdWallet.derivePath("m/44'/60'/0'/0/0"); |
| 32 | + var accountInfo = new Object(); |
| 33 | + accountInfo.type = 1; |
| 34 | + accountInfo.privatekey = key1.getWallet().getPrivateKeyString(); |
| 35 | + accountInfo.address = key1.getWallet().getAddressString(); |
| 36 | + accountInfo.words = mnemonic; |
| 37 | + notifyClient(callid, 0, accountInfo); |
| 38 | +} |
| 39 | + |
| 40 | +function importWalletWithPK(params) { |
| 41 | + var paramsjson = JSON.parse(params); |
| 42 | + var callid = paramsjson.callid; |
| 43 | + var privatekey = paramsjson.privateKey; |
| 44 | + var account = web3.eth.accounts.privateKeyToAccount(privatekey); |
| 45 | + account.type = 1; |
| 46 | + notifyClient(callid, 0, account); |
| 47 | +} |
| 48 | + |
| 49 | +function importWalletWithWords(params) { |
| 50 | + var paramsjson = JSON.parse(params); |
| 51 | + var callid = paramsjson.callid; |
| 52 | + var words = paramsjson.words; |
| 53 | + var seed = bip39.mnemonicToSeed(words); |
| 54 | + var hdWallet = hdkey.fromMasterSeed(seed); |
| 55 | + var key1 = hdWallet.derivePath("m/44'/60'/0'/0/0"); |
| 56 | + var accountInfo = new Object(); |
| 57 | + accountInfo.type = 1; |
| 58 | + accountInfo.privateKey = key1.getWallet().getPrivateKeyString(); |
| 59 | + accountInfo.address = key1.getWallet().getAddressString(); |
| 60 | + notifyClient(callid, 0, accountInfo); |
| 61 | +} |
| 62 | + |
| 63 | +function accountSignTransaction(params) { |
| 64 | + client.log(params); |
| 65 | + var paramsjson = JSON.parse(params); |
| 66 | + var callid = paramsjson.callid; |
| 67 | + var privateKey = paramsjson.privateKey; |
| 68 | + var transactionToSign = paramsjson.transactionToSign; |
| 69 | + if (transactionToSign.abi == null || transactionToSign.abi == undefined) { |
| 70 | + web3.eth.accounts.signTransaction(transactionToSign, privateKey).then(function (result) { |
| 71 | + var callbackData = new Object(); |
| 72 | + callbackData.signedTransaction = result; |
| 73 | + notifyClient(callid, 0, callbackData) |
| 74 | + }); |
| 75 | + |
| 76 | + } else { |
| 77 | + client.log("abi"); |
| 78 | + var abi = transactionToSign.abi; |
| 79 | + var contractAddress = transactionToSign.to; |
| 80 | + var from = transactionToSign.from; |
| 81 | + var value = transactionToSign.value; |
| 82 | + var toAddress = transactionToSign.toAddress; |
| 83 | + var gasLimit = transactionToSign.gas; |
| 84 | + var gasPrice = transactionToSign.gasPrice; |
| 85 | + |
| 86 | + var contract = new web3.eth.Contract(abi, contractAddress, {from: from}); |
| 87 | + web3.eth.getTransactionCount(from).then(function (result) { |
| 88 | + client.log("sign"); |
| 89 | + var rawTransaction = { |
| 90 | + "from": from, |
| 91 | + "gasPrice": web3.utils.toHex(gasPrice), |
| 92 | + "gasLimit": web3.utils.toHex(gasLimit), |
| 93 | + "to": contractAddress, |
| 94 | + "data": contract.methods.transfer(toAddress, value).encodeABI(), |
| 95 | + "nonce":web3.utils.toHex(result) |
| 96 | + }; |
| 97 | + web3.eth.accounts.signTransaction(rawTransaction, privateKey).then(function (result) { |
| 98 | + var callbackData = new Object(); |
| 99 | + callbackData.signedTransaction = result; |
| 100 | + notifyClient(callid, 0, callbackData); |
| 101 | + }); |
| 102 | + }); |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +function sendTransaction(params) { |
| 107 | + var paramsjson = JSON.parse(params); |
| 108 | + var callid = paramsjson.callid; |
| 109 | + |
| 110 | + var rawTransaction = paramsjson.rawTransaction; //由signedTransaction产生 |
| 111 | + |
| 112 | + web3.eth.sendSignedTransaction(rawTransaction) |
| 113 | + .on('receipt', function (receipt) { |
| 114 | + var callbackData = new Object(); |
| 115 | + callbackData.receipt = receipt; |
| 116 | + notifyClient(callid, 0, callbackData) |
| 117 | + }) |
| 118 | + .on('error', function (error) { |
| 119 | + var callbackData = new Object(); |
| 120 | + callbackData.error = error; |
| 121 | + notifyClient(callid, -1, callbackData) |
| 122 | + }); |
| 123 | +} |
| 124 | + |
| 125 | +function toIbanAddress(params) { |
| 126 | + var paramsjson = JSON.parse(params); |
| 127 | + var callid = paramsjson.callid; |
| 128 | + var ethAddress = paramsjson.ethAddress; |
| 129 | + var ibanAddress = web3.eth.Iban.toIban(ethAddress); |
| 130 | + var callbackData = new Object(); |
| 131 | + callbackData.ibanAddress = ibanAddress; |
| 132 | + notifyClient(callid, 0, callbackData); |
| 133 | +} |
| 134 | +function toEthAddress(params) { |
| 135 | + var paramsjson = JSON.parse(params); |
| 136 | + var callid = paramsjson.callid; |
| 137 | + var ibanAddress = paramsjson.ibanAddress; |
| 138 | + var ethAddress = web3.eth.Iban.toAddress(ibanAddress); |
| 139 | + client.log(ethAddress); |
| 140 | + var callbackData = new Object(); |
| 141 | + callbackData.ethAddress = ethAddress; |
| 142 | + notifyClient(callid, 0, callbackData); |
| 143 | +} |
| 144 | +function getGasPrice(params) { |
| 145 | + var paramsjson = JSON.parse(params); |
| 146 | + var callid = paramsjson.callid; |
| 147 | + web3.eth.getGasPrice().then(function(gasPrice) { |
| 148 | + notifyClient(callid, 0, gasPrice) |
| 149 | + }); |
| 150 | +} |
| 151 | + |
| 152 | +//end eth |
| 153 | + |
| 154 | +//jt |
| 155 | +function createJtWallet() { |
| 156 | + var paramsjson = JSON.parse(params); |
| 157 | + var callid = paramsjson.callid; |
| 158 | + var wallet = jingtum.Wallet.generate(); |
| 159 | + var accountInfo = new Object(); |
| 160 | + accountInfo.type = 2; //jt 类型 |
| 161 | + accountInfo.privatekey = wallet.secret; |
| 162 | + accountInfo.address = wallet.address; |
| 163 | + notifyClient(callid, 0, accountInfo); |
| 164 | +} |
| 165 | + |
| 166 | +function retrieveWalletFromPk(params) { |
| 167 | + var paramsjson = JSON.parse(params); |
| 168 | + var callid = paramsjson.callid; |
| 169 | + var privatekey = paramsjson.privateKey; |
| 170 | + var wallet = jingtum.Wallet.fromSecret(privateKey); |
| 171 | + var accountInfo = new Object(); |
| 172 | + accountInfo.type = 2; //jt 类型 |
| 173 | + accountInfo.address = wallet.address; |
| 174 | + notifyClient(callid, 0, accountInfo); |
| 175 | +} |
| 176 | + |
| 177 | +function isValidAddress(params) { |
| 178 | + var paramsjson = JSON.parse(params); |
| 179 | + var callid = paramsjson.callid; |
| 180 | + var address = paramsjson.address; |
| 181 | + var wallet = jingtum.Wallet.isValidAddress(address); |
| 182 | + notifyClient(callid, 0, wallet); |
| 183 | +} |
| 184 | + |
| 185 | +function signJTTransaction() { |
| 186 | + var paramsjson = JSON.parse(params); |
| 187 | + var callid = paramsjson.callid; |
| 188 | + var tx = {}; |
| 189 | + tx.Fee = paramsjson.fee; |
| 190 | + tx.Flags = 0; |
| 191 | + tx.Sequence = paramsjson.sequence;//通过balance接口拿到这个值 |
| 192 | + tx.TransactionType = 'Payment'; |
| 193 | + tx.Account = paramsjson.account; |
| 194 | + tx.Amount = {"value": paramsjson.value, "currency": paramsjson.currency, "issuer": paramsjson.issuer}; |
| 195 | + tx.Destination = paramsjson.destination; |
| 196 | + var sign = jingtumLib.jingtum_sign_tx(tx, {"seed": paramsjson.seed}); |
| 197 | + var callbackData = new Object(); |
| 198 | + callbackData.signedTransaction = result; |
| 199 | + notifyClient(callid, 0, callbackData); |
| 200 | +} |
| 201 | + |
| 202 | + |
| 203 | +function notifyClient(callid, ret, extra) { |
| 204 | + var result = new Object(); |
| 205 | + result.ret = ret; |
| 206 | + result.callid = callid; |
| 207 | + result.extra = extra; |
| 208 | + var resultStr = toJsonString(result); |
| 209 | + window.client.notifyWeb3Result(resultStr); |
| 210 | +} |
| 211 | + |
| 212 | + |
| 213 | +function toJsonString(obj) { |
| 214 | + if (obj == undefined) { |
| 215 | + return "{}"; |
| 216 | + } else { |
| 217 | + return JSON.stringify(obj); |
| 218 | + } |
| 219 | +} |
| 220 | + |
| 221 | +function outputObj(obj) { |
| 222 | + var description = ""; |
| 223 | + for (var i in obj) { |
| 224 | + description += i + " = " + (obj[i]) + "\n"; |
| 225 | + } |
| 226 | + console.log(description); |
| 227 | +} |
| 228 | +function printAccount(account) { |
| 229 | + var description = ""; |
| 230 | + for (var i in account) { |
| 231 | + description += i + " = " + account[i] + "\n"; |
| 232 | + } |
| 233 | + console.log(description); |
| 234 | +} |
| 235 | + |
| 236 | +function testJson() { |
| 237 | + var result = new Object(); |
| 238 | + result.ret = -1; |
| 239 | + alert(toJsonString(result)); |
| 240 | +} |
| 241 | + |
| 242 | + |
0 commit comments