diff options
author | bguiz <bguiz@users.noreply.github.com> | 2020-03-09 14:12:22 +0800 |
---|---|---|
committer | bguiz <bguiz@users.noreply.github.com> | 2020-03-10 17:08:16 +0800 |
commit | a823e5266c95f54dbf7b672f3d0ba2bac779f85e (patch) | |
tree | 49577b30f36c843844abce0f108eb9fade1af684 | |
parent | b64fb35301060c00acaf25beec37971d1877d0cd (diff) | |
download | BIP39-a823e5266c95f54dbf7b672f3d0ba2bac779f85e.tar.gz BIP39-a823e5266c95f54dbf7b672f3d0ba2bac779f85e.tar.zst BIP39-a823e5266c95f54dbf7b672f3d0ba2bac779f85e.zip |
fix: calculate RSK addresses with EIP-155 compliant checksums
-rw-r--r-- | src/js/index.js | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/src/js/index.js b/src/js/index.js index ad2b859..fa9836a 100644 --- a/src/js/index.js +++ b/src/js/index.js | |||
@@ -1140,6 +1140,34 @@ | |||
1140 | } | 1140 | } |
1141 | } | 1141 | } |
1142 | 1142 | ||
1143 | // RSK values are different | ||
1144 | if (networkIsRsk()) { | ||
1145 | var pubkeyBuffer = keyPair.getPublicKeyBuffer(); | ||
1146 | var ethPubkey = libs.ethUtil.importPublic(pubkeyBuffer); | ||
1147 | var addressBuffer = libs.ethUtil.publicToAddress(ethPubkey); | ||
1148 | var hexAddress = addressBuffer.toString('hex'); | ||
1149 | // Use chainId based on selected network | ||
1150 | // Ref: https://developers.rsk.co/rsk/architecture/account-based/#chainid | ||
1151 | var chainId; | ||
1152 | var rskNetworkName = networks[DOM.network.val()].name; | ||
1153 | switch (rskNetworkName) { | ||
1154 | case "R-BTC - RSK": | ||
1155 | chainId = 30; | ||
1156 | break; | ||
1157 | case "tR-BTC - RSK Testnet": | ||
1158 | chainId = 31; | ||
1159 | break; | ||
1160 | default: | ||
1161 | chainId = null; | ||
1162 | } | ||
1163 | var checksumAddress = toChecksumAddressForRsk(hexAddress, chainId); | ||
1164 | address = libs.ethUtil.addHexPrefix(checksumAddress); | ||
1165 | pubkey = libs.ethUtil.addHexPrefix(pubkey); | ||
1166 | if (hasPrivkey) { | ||
1167 | privkey = libs.ethUtil.bufferToHex(keyPair.d.toBuffer()); | ||
1168 | } | ||
1169 | } | ||
1170 | |||
1143 | // Stellar is different | 1171 | // Stellar is different |
1144 | if (networks[DOM.network.val()].name == "XLM - Stellar") { | 1172 | if (networks[DOM.network.val()].name == "XLM - Stellar") { |
1145 | var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44); | 1173 | var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44); |
@@ -1849,6 +1877,12 @@ | |||
1849 | || (name == "ERE - EtherCore") | 1877 | || (name == "ERE - EtherCore") |
1850 | } | 1878 | } |
1851 | 1879 | ||
1880 | function networkIsRsk() { | ||
1881 | var name = networks[DOM.network.val()].name; | ||
1882 | return (name == "R-BTC - RSK") | ||
1883 | || (name == "tR-BTC - RSK Testnet"); | ||
1884 | } | ||
1885 | |||
1852 | function networkHasSegwit() { | 1886 | function networkHasSegwit() { |
1853 | var n = network; | 1887 | var n = network; |
1854 | if ("baseNetwork" in network) { | 1888 | if ("baseNetwork" in network) { |
@@ -3349,6 +3383,46 @@ | |||
3349 | } | 3383 | } |
3350 | ] | 3384 | ] |
3351 | 3385 | ||
3386 | // RSK - RSK functions - begin | ||
3387 | function stripHexPrefix(address) { | ||
3388 | if (typeof address !== "string") { | ||
3389 | throw new Error("address parameter should be a string."); | ||
3390 | } | ||
3391 | |||
3392 | var hasPrefix = (address.substring(0, 2) === "0x" || | ||
3393 | address.substring(0, 2) === "0X"); | ||
3394 | |||
3395 | return hasPrefix ? address.slice(2) : address; | ||
3396 | }; | ||
3397 | |||
3398 | function toChecksumAddressForRsk(address, chainId = null) { | ||
3399 | if (typeof address !== "string") { | ||
3400 | throw new Error("address parameter should be a string."); | ||
3401 | } | ||
3402 | |||
3403 | if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) { | ||
3404 | throw new Error("Given address is not a valid RSK address: " + address); | ||
3405 | } | ||
3406 | |||
3407 | var stripAddress = stripHexPrefix(address).toLowerCase(); | ||
3408 | var prefix = chainId != null ? chainId.toString() + "0x" : ""; | ||
3409 | var keccakHash = libs.ethUtil.keccak256(prefix + stripAddress) | ||
3410 | .toString("hex") | ||
3411 | .replace(/^0x/i, ""); | ||
3412 | var checksumAddress = "0x"; | ||
3413 | |||
3414 | for (var i = 0; i < stripAddress.length; i++) { | ||
3415 | checksumAddress += | ||
3416 | parseInt(keccakHash[i], 16) >= 8 ? | ||
3417 | stripAddress[i].toUpperCase() : | ||
3418 | stripAddress[i]; | ||
3419 | } | ||
3420 | |||
3421 | return checksumAddress; | ||
3422 | } | ||
3423 | |||
3424 | // RSK - RSK functions - end | ||
3425 | |||
3352 | // ELA - Elastos functions - begin | 3426 | // ELA - Elastos functions - begin |
3353 | function displayBip44InfoForELA() { | 3427 | function displayBip44InfoForELA() { |
3354 | if (!isELA()) { | 3428 | if (!isELA()) { |