Browse Source

initial push

master
peter 2 years ago
parent
commit
e766735b67
2 changed files with 0 additions and 107 deletions
  1. 0
    27
      index.d.ts
  2. 0
    80
      index.js

+ 0
- 27
index.d.ts View File

@@ -1,27 +0,0 @@
1
-export declare type network = 'testnet' | 'mainnet';
2
-/**
3
- * Generate a BIP39 menemonic
4
- */
5
-export declare function randomMenemonic(): string;
6
-/**
7
- * Generate a pseudo-random seed from BIP39 menemonic
8
- * @param menemonic
9
- */
10
-export declare function seedFromMenemonic(menemonic: string): string;
11
-/**
12
- * Derive an extended keypair from seed
13
- * @param seed The seed-string to use for generating the HDKey
14
- * @param net Network to use ("testnet", "mainnet")
15
- * @param n n-th derived keypair default:0
16
- */
17
-export declare function deriveAccount(seed: string, net?: network, n?: number): {
18
-    publicExtendedKey: string;
19
-    privateExtendedKey: string;
20
-};
21
-/**
22
- *
23
- * @param extendedKey Extended key to generate address from
24
- * @param networkName Network to use ("testnet", "mainnet")
25
- * @param n n-th derived address default:0
26
- */
27
-export declare function generateAddress(extendedKey: string, networkName?: network, n?: number): string;

+ 0
- 80
index.js View File

@@ -1,80 +0,0 @@
1
-"use strict";
2
-Object.defineProperty(exports, "__esModule", { value: true });
3
-const HDKey = require("hdkey");
4
-const bitcoin = require("bitcoinjs-lib");
5
-const bip39 = require('bip39');
6
-const assert = require('bsert');
7
-/**
8
- * Generate a BIP39 menemonic
9
- */
10
-function randomMenemonic() {
11
-    return bip39.generateMnemonic();
12
-}
13
-exports.randomMenemonic = randomMenemonic;
14
-/**
15
- * Generate a pseudo-random seed from BIP39 menemonic
16
- * @param menemonic
17
- */
18
-function seedFromMenemonic(menemonic) {
19
-    assert(bip39.validateMnemonic(menemonic));
20
-    return bip39.mnemonicToSeedSync(menemonic).toString('hex');
21
-}
22
-exports.seedFromMenemonic = seedFromMenemonic;
23
-/**
24
- * Derive an extended keypair from seed
25
- * @param seed The seed-string to use for generating the HDKey
26
- * @param net Network to use ("testnet", "mainnet")
27
- * @param n n-th derived keypair default:0
28
- */
29
-function deriveAccount(seed, net = "mainnet", n = 0) {
30
-    assert(n >= 0);
31
-    let version;
32
-    if (net == "testnet") {
33
-        version = { private: 0x04358394, public: 0x043587CF }; //HDKey does not support testnet keys out of the box
34
-    }
35
-    var rootKey = HDKey.fromMasterSeed(seed, version);
36
-    let childKey = rootKey.derive("m/" + n + "'");
37
-    return childKey;
38
-}
39
-exports.deriveAccount = deriveAccount;
40
-function checkBTCHDKey(key, nw) {
41
-    switch (nw) {
42
-        case "mainnet":
43
-            assert(key.startsWith('x'));
44
-            if (key.startsWith('xprv'))
45
-                console.log("WARNING Use of extended private keys as generator is discouraged");
46
-            break;
47
-        case "testnet":
48
-            assert(key.startsWith('t'));
49
-            if (key.startsWith('tprv'))
50
-                console.log("WARNING Use of extended private keys as generator is discouraged");
51
-            break;
52
-    }
53
-}
54
-//doc https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki
55
-//lib https://github.com/cryptocoinjs/hdkey
56
-//gen http://bip32.org/
57
-// enter bip32  key of the following formats:
58
-// - extended public mainnet key  (xpub prefix) 
59
-// - extended private mainnet key (xprv prefix) (not reccomended)
60
-// - extended public testnet key  (tpub prefix) 
61
-// - extended private testnet key (tprv prefix) (not reccomended)
62
-/**
63
- *
64
- * @param extendedKey Extended key to generate address from
65
- * @param networkName Network to use ("testnet", "mainnet")
66
- * @param n n-th derived address default:0
67
- */
68
-function generateAddress(extendedKey, networkName = "mainnet", n = 0) {
69
-    assert(n >= 0);
70
-    let version;
71
-    checkBTCHDKey(extendedKey, networkName);
72
-    if (networkName == "testnet") {
73
-        version = { private: 0x04358394, public: 0x043587CF }; //HDKey does not support testnet keys out of the box
74
-    }
75
-    const hdkey = HDKey.fromExtendedKey(extendedKey, version);
76
-    const childkey = hdkey.derive('m/0/' + n);
77
-    const address = bitcoin.payments.p2pkh({ pubkey: childkey._publicKey, network: bitcoin.networks[networkName] }).address;
78
-    return address;
79
-}
80
-exports.generateAddress = generateAddress;

Loading…
Cancel
Save