From b64fb35301060c00acaf25beec37971d1877d0cd Mon Sep 17 00:00:00 2001 From: bguiz Date: Tue, 3 Mar 2020 14:55:53 +0800 Subject: fix: define own libs.bitcoin.networks.rsk --- src/js/bitcoinjs-extensions.js | 25 +++++++++++++++++++++++++ src/js/index.js | 8 ++++---- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/src/js/bitcoinjs-extensions.js b/src/js/bitcoinjs-extensions.js index 17475b1..85c39d1 100644 --- a/src/js/bitcoinjs-extensions.js +++ b/src/js/bitcoinjs-extensions.js @@ -1175,6 +1175,31 @@ libs.bitcoin.networks.revolutionvr = { wif: 0xc6, }; +libs.bitcoin.networks.rsk = { + messagePrefix: '\x18RSK Signed Message:\n', + bip32: { + public: 0x0488b21e, + private: 0x0488ade4 + }, + // TODO defaulting to Bitcoin value, check this + pubKeyHash: 0x00, + // TODO defaulting to Bitcoin value, check this + scriptHash: 0x05, + // TODO defaulting to Bitcoin value, check this + wif: 0x80 +}; + +libs.bitcoin.networks.rsktestnet = { + messagePrefix: '\x18RSK Testnet Signed Message:\n', + bip32: { + public: 0x043587cf, + private: 0x04358394 + }, + pubKeyHash: 0x6f, + scriptHash: 0xc4, + wif: 0xef +}; + libs.bitcoin.networks.rubycoin = { messagePrefix: '\x18Rubycoin Signed Message:\n', bip32: { diff --git a/src/js/index.js b/src/js/index.js index c2d34a7..ad2b859 100644 --- a/src/js/index.js +++ b/src/js/index.js @@ -2973,16 +2973,16 @@ }, }, { - name: "RBTC - RSK", + name: "R-BTC - RSK", onSelect: function() { - network = libs.bitcoin.networks.rubycoin; + network = libs.bitcoin.networks.rsk; setHdCoin(137); }, }, { - name: "tRBTC - RSK Testnet", + name: "tR-BTC - RSK Testnet", onSelect: function() { - network = libs.bitcoin.networks.rubycoin; + network = libs.bitcoin.networks.rsktestnet; setHdCoin(37310); }, }, -- cgit v1.2.3 From a823e5266c95f54dbf7b672f3d0ba2bac779f85e Mon Sep 17 00:00:00 2001 From: bguiz Date: Mon, 9 Mar 2020 14:12:22 +0800 Subject: fix: calculate RSK addresses with EIP-155 compliant checksums --- src/js/index.js | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) 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 @@ } } + // RSK values are different + if (networkIsRsk()) { + var pubkeyBuffer = keyPair.getPublicKeyBuffer(); + var ethPubkey = libs.ethUtil.importPublic(pubkeyBuffer); + var addressBuffer = libs.ethUtil.publicToAddress(ethPubkey); + var hexAddress = addressBuffer.toString('hex'); + // Use chainId based on selected network + // Ref: https://developers.rsk.co/rsk/architecture/account-based/#chainid + var chainId; + var rskNetworkName = networks[DOM.network.val()].name; + switch (rskNetworkName) { + case "R-BTC - RSK": + chainId = 30; + break; + case "tR-BTC - RSK Testnet": + chainId = 31; + break; + default: + chainId = null; + } + var checksumAddress = toChecksumAddressForRsk(hexAddress, chainId); + address = libs.ethUtil.addHexPrefix(checksumAddress); + pubkey = libs.ethUtil.addHexPrefix(pubkey); + if (hasPrivkey) { + privkey = libs.ethUtil.bufferToHex(keyPair.d.toBuffer()); + } + } + // Stellar is different if (networks[DOM.network.val()].name == "XLM - Stellar") { var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44); @@ -1849,6 +1877,12 @@ || (name == "ERE - EtherCore") } + function networkIsRsk() { + var name = networks[DOM.network.val()].name; + return (name == "R-BTC - RSK") + || (name == "tR-BTC - RSK Testnet"); + } + function networkHasSegwit() { var n = network; if ("baseNetwork" in network) { @@ -3349,6 +3383,46 @@ } ] + // RSK - RSK functions - begin + function stripHexPrefix(address) { + if (typeof address !== "string") { + throw new Error("address parameter should be a string."); + } + + var hasPrefix = (address.substring(0, 2) === "0x" || + address.substring(0, 2) === "0X"); + + return hasPrefix ? address.slice(2) : address; + }; + + function toChecksumAddressForRsk(address, chainId = null) { + if (typeof address !== "string") { + throw new Error("address parameter should be a string."); + } + + if (!/^(0x)?[0-9a-f]{40}$/i.test(address)) { + throw new Error("Given address is not a valid RSK address: " + address); + } + + var stripAddress = stripHexPrefix(address).toLowerCase(); + var prefix = chainId != null ? chainId.toString() + "0x" : ""; + var keccakHash = libs.ethUtil.keccak256(prefix + stripAddress) + .toString("hex") + .replace(/^0x/i, ""); + var checksumAddress = "0x"; + + for (var i = 0; i < stripAddress.length; i++) { + checksumAddress += + parseInt(keccakHash[i], 16) >= 8 ? + stripAddress[i].toUpperCase() : + stripAddress[i]; + } + + return checksumAddress; + } + + // RSK - RSK functions - end + // ELA - Elastos functions - begin function displayBip44InfoForELA() { if (!isELA()) { -- cgit v1.2.3