PageRenderTime 1513ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/bitcoin-networks.js

https://gitlab.com/YukiteruAmano/simulador-criptografico
JavaScript | 169 lines | 162 code | 7 blank | 0 comment | 3 complexity | 70524881401d37dd30a9103e699fb339 MD5 | raw file
  1. window.allNetworks = [{
  2. label: 'BTC (Bitcoin Regtest, legacy, BIP32/44)',
  3. config: {
  4. messagePrefix: '\u0018Bitcoin Signed Message:\n',
  5. bech32: 'bcrt',
  6. bip32: {public: 0x043587cf, private: 0x04358394},
  7. pubKeyHash: 111,
  8. scriptHash: 196,
  9. wif: 239,
  10. bip44: 0x01
  11. }
  12. }, {
  13. label: 'BTC (Bitcoin Regtest, SegWit, BIP49)',
  14. config: {
  15. messagePrefix: '\u0018Bitcoin Signed Message:\n',
  16. bech32: 'bcrt',
  17. bip32: {public: 0x044a5262, private: 0x044a4e28},
  18. pubKeyHash: 111,
  19. scriptHash: 196,
  20. wif: 239,
  21. bip44: 0x01
  22. }
  23. }, {
  24. label: 'BTC (Bitcoin Regtest, Native SegWit, BIP84)',
  25. config: {
  26. messagePrefix: '\u0018Bitcoin Signed Message:\n',
  27. bech32: 'bcrt',
  28. bip32: {public: 0x045f1cf6, private: 0x045f18bc},
  29. pubKeyHash: 111,
  30. scriptHash: 196,
  31. wif: 239,
  32. bip44: 0x01
  33. }
  34. }, {
  35. label: 'BTC (Bitcoin Signet, legacy, BIP32/44)',
  36. config: {
  37. messagePrefix: '\u0018Bitcoin Signed Message:\n',
  38. bech32: 'tb',
  39. bip32: {public: 0x043587cf, private: 0x04358394},
  40. pubKeyHash: 111,
  41. scriptHash: 196,
  42. wif: 239,
  43. bip44: 0x01
  44. }
  45. }, {
  46. label: 'BTC (Bitcoin Signet, SegWit, BIP49)',
  47. config: {
  48. messagePrefix: '\u0018Bitcoin Signed Message:\n',
  49. bech32: 'tb',
  50. bip32: {public: 0x044a5262, private: 0x044a4e28},
  51. pubKeyHash: 111,
  52. scriptHash: 196,
  53. wif: 239,
  54. bip44: 0x01
  55. }
  56. }, {
  57. label: 'BTC (Bitcoin Signet, Native SegWit, BIP84)',
  58. config: {
  59. messagePrefix: '\u0018Bitcoin Signed Message:\n',
  60. bech32: 'tb',
  61. bip32: {public: 0x045f1cf6, private: 0x045f18bc},
  62. pubKeyHash: 111,
  63. scriptHash: 196,
  64. wif: 239,
  65. bip44: 0x01
  66. }
  67. }, {
  68. label: 'BTC (Bitcoin Testnet, legacy, BIP32/44)',
  69. config: {
  70. messagePrefix: '\u0018Bitcoin Signed Message:\n',
  71. bech32: 'tb',
  72. bip32: {public: 0x043587cf, private: 0x04358394},
  73. pubKeyHash: 111,
  74. scriptHash: 196,
  75. wif: 239,
  76. bip44: 0x01
  77. }
  78. }, {
  79. label: 'BTC (Bitcoin Testnet, SegWit, BIP49)',
  80. config: {
  81. messagePrefix: '\u0018Bitcoin Signed Message:\n',
  82. bech32: 'tb',
  83. bip32: {public: 0x044a5262, private: 0x044a4e28},
  84. pubKeyHash: 111,
  85. scriptHash: 196,
  86. wif: 239,
  87. bip44: 0x01
  88. }
  89. }, {
  90. label: 'BTC (Bitcoin Testnet, Native SegWit, BIP84)',
  91. config: {
  92. messagePrefix: '\u0018Bitcoin Signed Message:\n',
  93. bech32: 'tb',
  94. bip32: {public: 0x045f1cf6, private: 0x045f18bc},
  95. pubKeyHash: 111,
  96. scriptHash: 196,
  97. wif: 239,
  98. bip44: 0x01
  99. }
  100. }, {
  101. label: 'BTC (Bitcoin, legacy, BIP32/44)',
  102. config: {
  103. messagePrefix: '\u0018Bitcoin Signed Message:\n',
  104. bech32: 'bc',
  105. bip32: {public: 0x0488b21e, private: 0x0488ade4},
  106. pubKeyHash: 0,
  107. scriptHash: 5,
  108. wif: 128,
  109. bip44: 0x00
  110. }
  111. }, {
  112. label: 'BTC (Bitcoin, SegWit, BIP49)',
  113. config: {
  114. messagePrefix: '\u0018Bitcoin Signed Message:\n',
  115. bech32: 'bc',
  116. bip32: {public: 0x049d7cb2, private: 0x049d7878},
  117. pubKeyHash: 0,
  118. scriptHash: 5,
  119. wif: 128,
  120. bip44: 0x00
  121. }
  122. }, {
  123. label: 'BTC (Bitcoin, Native SegWit, BIP84)',
  124. config: {
  125. messagePrefix: '\u0018Bitcoin Signed Message:\n',
  126. bech32: 'bc',
  127. bip32: {public: 0x04b24746, private: 0x04b2430c},
  128. pubKeyHash: 0,
  129. scriptHash: 5,
  130. wif: 128,
  131. bip44: 0x00
  132. }
  133. }];
  134. window.bitcoinNetworks = _.filter(allNetworks, n => n.config.bech32 && (n.config.bip44 === 0x00 || n.config.bip44 === 0x01));
  135. function customToWIF(keyPair, network) {
  136. return keyPair.toWIF();
  137. }
  138. function getP2PKHAddress(keyPair, network) {
  139. return bitcoin.payments.p2pkh({ pubkey: keyPair.publicKey, network: network }).address;
  140. }
  141. function getP2WPKHAddress(keyPair, network) {
  142. return bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey, network: network }).address;
  143. }
  144. function getNestedP2WPKHAddress(keyPair, network) {
  145. const p2wpkh = bitcoin.payments.p2wpkh({ pubkey: keyPair.publicKey, network: network });
  146. return bitcoin.payments.p2sh({ redeem: p2wpkh }).address;
  147. }
  148. function getP2TRAddress(keyPair, network) {
  149. const pubKey = bitcoin.ecurve.Point.decodeFrom(bitcoin.secp256k1, keyPair.publicKey);
  150. const taprootPubkey = bitcoin.schnorr.taproot.taprootConstruct(pubKey);
  151. const words = bitcoin.bech32.toWords(taprootPubkey);
  152. words.unshift(1);
  153. return bitcoin.bech32m.encode(network.bech32, words);
  154. }
  155. function calculateAddresses(keyPair, network) {
  156. keyPair.address = getP2PKHAddress(keyPair, network);
  157. if (network.bech32) {
  158. keyPair.nestedP2WPKHAddress = getNestedP2WPKHAddress(keyPair, network);
  159. keyPair.P2WPKHAddress = getP2WPKHAddress(keyPair, network);
  160. keyPair.P2TRAddress = getP2TRAddress(keyPair, network);
  161. }
  162. }