PageRenderTime 1296ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/pages/hd-wallet/hd-wallet.js

https://gitlab.com/YukiteruAmano/simulador-criptografico
JavaScript | 167 lines | 150 code | 17 blank | 0 comment | 7 complexity | c0f5f7720aaf46ca57a8c92d675040fe MD5 | raw file
  1. angular
  2. .module('app')
  3. .component('hdWalletPage', {
  4. templateUrl: 'pages/hd-wallet/hd-wallet.html',
  5. controller: HdWalletPageController,
  6. controllerAs: 'vm',
  7. bindings: {}
  8. });
  9. const calculatePath = function (bip, coinType, account, change, index) {
  10. let path = bip.path;
  11. if (bip.hasCoinType) {
  12. path = path.replace(/coin/g, coinType.config.bip44);
  13. }
  14. path = path.replace(/account/g, account);
  15. path = path.replace(/change/g, change);
  16. path = path.replace(/index/g, index);
  17. return path;
  18. };
  19. function HdWalletPageController(lodash, allNetworks) {
  20. const vm = this;
  21. const PBKDF2_SALT = 'Digital Bitbox',
  22. PBKDF2_HMACLEN = 64,
  23. PBKDF2_ROUNDS_APP = 20480;
  24. const METHOD_NONE = 0,
  25. METHOD_PBKDF2 = 1,
  26. METHOD_COINOMI = 2;
  27. const BITCOIN = lodash.find(allNetworks, ['label', 'BTC (Bitcoin, legacy, BIP32/44)']);
  28. vm.coinTypes = allNetworks;
  29. vm.coinType = BITCOIN;
  30. vm.networks = allNetworks;
  31. vm.network = BITCOIN;
  32. vm.mnemonic = null;
  33. vm.asPassword = true;
  34. vm.passphrase = null;
  35. vm.seed = null;
  36. vm.seedHex = null;
  37. vm.node = null;
  38. vm.nodeBase58 = null;
  39. vm.privKeyWif = null;
  40. vm.publicKeyWif = null;
  41. vm.address = null;
  42. vm.account = 0;
  43. vm.change = 0;
  44. vm.index = 0;
  45. vm.bips = [
  46. {id: 0, label: 'BIP32 (Bitcoin Core)', bip: '32', hasCoinType: false, path: 'm/account\'/change\'/index'},
  47. {id: 1, label: 'BIP44 (Legacy wallets, multi coin wallets)', bip: '44', hasCoinType: true, path: 'm/44\'/coin\'/account\'/change/index'},
  48. {id: 2, label: 'BIP49 (SegWit P2SH-P2WPKH)', bip: '49', hasCoinType: true, path: 'm/49\'/coin\'/account\'/change/index'},
  49. {id: 3, label: 'BIP84 (Native SegWit bech32 P2WPKH)', bip: '84', hasCoinType: true, path: 'm/84\'/coin\'/account\'/change/index'},
  50. {id: 4, label: 'BIP86 (Native SegWit v1 bech32m P2TR)', bip: '86', hasCoinType: true, path: 'm/86\'/coin\'/account\'/change/index'},
  51. ];
  52. vm.selectedBip = vm.bips[1];
  53. vm.path = calculatePath(vm.selectedBip, vm.coinType, vm.account, vm.change, vm.index);
  54. vm.customPath = '0/0';
  55. vm.strenghteningMethods = [
  56. { label: 'BIP39 default (like Coinomi)', id: METHOD_COINOMI },
  57. { label: 'BIP39 custom (passhprase to hex)', id: METHOD_NONE },
  58. { label: 'PBKDF2 (Digital Bitbox)', id: METHOD_PBKDF2 }
  59. ];
  60. vm.strenghtening = vm.strenghteningMethods[0];
  61. vm.seedLengths = [
  62. { label: '128bit / 12 words', id: 128 },
  63. { label: '160bit / 15 words', id: 160 },
  64. { label: '192bit / 18 words', id: 192 },
  65. { label: '224bit / 21 words', id: 224 },
  66. { label: '256bit / 24 words', id: 256 }
  67. ];
  68. vm.mnemonicLength = vm.seedLengths[0];
  69. vm.$onInit = function () {
  70. vm.newSeed();
  71. };
  72. vm.newSeed = function () {
  73. vm.mnemonic = bitcoin.bip39.generateMnemonic(vm.mnemonicLength.id);
  74. vm.fromMnemonic();
  75. };
  76. vm.fromMnemonic = function () {
  77. let pw = null;
  78. if (vm.passphrase) {
  79. if (vm.strenghtening.id === METHOD_PBKDF2) {
  80. pw = bitcoin.pbkdf2.pbkdf2Sync(
  81. bitcoin.Buffer.from(vm.passphrase, 'utf8'),
  82. PBKDF2_SALT,
  83. PBKDF2_ROUNDS_APP,
  84. PBKDF2_HMACLEN,
  85. 'sha512'
  86. ).toString('hex');
  87. } else if (vm.strenghtening.id === METHOD_COINOMI) {
  88. pw = vm.passphrase;
  89. } else {
  90. pw = bitcoin.Buffer.from(vm.passphrase, 'utf8').toString('hex');
  91. }
  92. }
  93. vm.seed = bitcoin.bip39.mnemonicToSeed(vm.mnemonic, pw);
  94. vm.fromSeed();
  95. };
  96. vm.fromSeed = function () {
  97. if (vm.seed) {
  98. vm.seedHex = vm.seed.toString('hex');
  99. vm.node = bitcoin.bip32.fromSeed(vm.seed, vm.network.config);
  100. vm.nodeBase58 = vm.node.toBase58();
  101. vm.customParentBase58 = vm.node.toBase58();
  102. vm.fromNode();
  103. vm.fromCustomParent();
  104. }
  105. };
  106. vm.fromHexSeed = function () {
  107. vm.seed = bitcoin.Buffer.from(vm.seedHex, 'hex');
  108. vm.mnemonic = 'Cannot be reversed! Mnemonic to seed is a one way street...';
  109. vm.fromSeed();
  110. };
  111. vm.fromBase58Seed = function () {
  112. vm.error = null;
  113. try {
  114. vm.node = bitcoin.bip32.fromBase58(vm.nodeBase58, vm.network.config);
  115. vm.seed = null;
  116. vm.seedHex = 'Cannot be reversed! Seed is hashed to create HD node';
  117. vm.mnemonic = 'Cannot be reversed! Mnemonic to seed is a one way street...';
  118. vm.fromNode();
  119. } catch (e) {
  120. vm.error = e;
  121. }
  122. };
  123. vm.fromNode = function () {
  124. vm.publicKeyWif = vm.node.neutered().toBase58();
  125. vm.address = getP2PKHAddress(vm.node, vm.network.config);
  126. vm.calculatePath();
  127. };
  128. vm.calculatePath = function () {
  129. vm.path = calculatePath(vm.selectedBip, vm.coinType, vm.account, vm.change, vm.index);
  130. vm.fromPath();
  131. };
  132. vm.fromPath = function () {
  133. vm.derivedKey = vm.node.derivePath(vm.path);
  134. calculateAddresses(vm.derivedKey, vm.network.config);
  135. };
  136. vm.fromCustomParent = function () {
  137. vm.customParentError = null;
  138. try {
  139. vm.customParent = bitcoin.bip32.fromBase58(vm.customParentBase58, vm.network.config);
  140. vm.customPath = '0/0';
  141. vm.fromCustomPath();
  142. } catch (e) {
  143. vm.customParentError = e;
  144. }
  145. };
  146. vm.fromCustomPath = function () {
  147. vm.customDerivedKey = vm.customParent.derivePath(vm.customPath, vm.network.config);
  148. calculateAddresses(vm.customDerivedKey, vm.network.config);
  149. };
  150. }