From: iancoleman <1281387+iancoleman@users.noreply.github.com> Date: Sun, 10 May 2020 23:05:32 +0000 (+1000) Subject: Merge branch 'master' into feat/rsk-network X-Git-Url: https://git.immae.eu/?p=perso%2FImmae%2FProjets%2FCryptomonnaies%2FBIP39.git;a=commitdiff_plain;h=bf528f65f411daec8735d25ca30e01fc1dd33e6e;hp=-c Merge branch 'master' into feat/rsk-network --- bf528f65f411daec8735d25ca30e01fc1dd33e6e diff --combined src/js/bitcoinjs-extensions.js index 85c39d1,2792e13..e67996c --- a/src/js/bitcoinjs-extensions.js +++ b/src/js/bitcoinjs-extensions.js @@@ -1175,31 -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: { @@@ -1764,6 -1739,28 +1764,28 @@@ libs.bitcoin.networks.elastos = wif: 0xef // TODO set this correctly, same as BTC for now }; + libs.bitcoin.networks.sugarchain = { + messagePrefix: '\x18Sugarchain Signed Message:\n', + bip32: { + public: 0x0488B21E, + private: 0x0488ADE4, + }, + pubKeyHash: 0x3f, + scriptHash: 0x7d, + wif: 0x80 + }; + + libs.bitcoin.networks.sugarchaintestnet = { + messagePrefix: '\x18Sugarchain Signed Message:\n', + bip32: { + public: 0x045f1cf6, + private: 0x045f18bc, + }, + pubKeyHash: 0x42, + scriptHash: 0x80, + wif: 0xef + }; + // https://github.com/libs.bitcoinjs-lib/blob/3f6f5ef97a1ee1b8337865209282c0095e22b2e7/src/networks.js libs.bitcoin.networks.regtest = { messagePrefix: '\x18Bitcoin Signed Message:\n', @@@ -1776,3 -1773,14 +1798,14 @@@ scriptHash: 0xc4, wif: 0xef, }; + + libs.bitcoin.networks.argoneum = { + messagePrefix: 'unused', + bip32: { + public: 0x0488b21e, + private: 0x0488ade4 + }, + pubKeyHash: 0x32, + scriptHash: 0x61, + wif: 0xbf + }; diff --combined src/js/index.js index fa9836a,e8072e4..43c6955 --- a/src/js/index.js +++ b/src/js/index.js @@@ -1140,34 -1140,11 +1140,40 @@@ } } + // 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()); + } + } + + // Handshake values are different + if (networks[DOM.network.val()].name == "HNS - Handshake") { + var ring = libs.handshake.KeyRing.fromPublic(keyPair.getPublicKeyBuffer()) + address = ring.getAddress().toString(); + } ++ // Stellar is different if (networks[DOM.network.val()].name == "XLM - Stellar") { var purpose = parseIntNoNaN(DOM.bip44purpose.val(), 44); @@@ -1264,6 -1241,12 +1270,12 @@@ privkey = eosUtil.bufferToPrivate(keyPair.d.toBuffer(32)); } + if (networks[DOM.network.val()].name == "FIO - Foundation for Interwallet Operability") { + address = "" + pubkey = FIObufferToPublic(keyPair.getPublicKeyBuffer()); + privkey = FIObufferToPrivate(keyPair.d.toBuffer(32)); + } + //Groestlcoin Addresses are different if(isGRS()) { @@@ -1877,12 -1860,6 +1889,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) { @@@ -2064,6 -2041,13 +2076,13 @@@ setHdCoin(161); }, }, + { + name: "AGM - Argoneum", + onSelect: function() { + network = libs.bitcoin.networks.argoneum; + setHdCoin(421); + }, + }, { name: "ARYA - Aryacoin", onSelect: function() { @@@ -2520,6 -2504,13 +2539,13 @@@ setHdCoin(40); }, }, + { + name: "FIO - Foundation for Interwallet Operability", + onSelect: function() { + network = libs.bitcoin.networks.bitcoin; + setHdCoin(235); + }, + }, { name: "FIX - FIX", onSelect: function() { @@@ -2604,6 -2595,12 +2630,12 @@@ setHdCoin(1); }, }, + { + name: "HNS - Handshake", + onSelect: function() { + setHdCoin(5353); + }, + }, { name: "HNC - Helleniccoin", onSelect: function() { @@@ -3007,16 -3004,16 +3039,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); }, }, @@@ -3125,6 -3122,20 +3157,20 @@@ setHdCoin(105); }, }, + { + name: "SUGAR - Sugarchain", + onSelect: function() { + network = libs.bitcoin.networks.sugarchain; + setHdCoin(408); + }, + }, + { + name: "TUGAR - Sugarchain Testnet", + onSelect: function() { + network = libs.bitcoin.networks.sugarchaintestnet; + setHdCoin(408); + }, + }, { name: "SWTC - Jingtum", onSelect: function() { @@@ -3383,46 -3394,6 +3429,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()) {