forked from ThreeDotsTech/nyzo-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
140 lines (124 loc) · 4.69 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import AddressGenerator from './lib/address-generator'
import AddressImporter, { Account, Wallet } from './lib/address-importer'
import NyzoString, { NyzoStringPublicIdentifier, NyzoStringPrivateSeed, NyzoStringPrefilledData } from './lib/nyzoStrings'
const generator = new AddressGenerator()
const importer = new AddressImporter()
const nyzoStrings = new NyzoString()
const wallet = {
/**
* Generate a new Nyzo cryptocurrency wallet
*
* This function generates a wallet from random entropy. Wallet includes
* a BIP39 mnemonic phrase in line with the NYZO Ledger implementation and
* a seed, the account is derived using BIP32 deterministic hierarchial algorithm
* with input parameters 44'/380' and index 0.
*
* The NYZO address is encoded from the public key using standard NyzoString.
*
* Generation uses CryptoJS to generate random entropy by default. You can give your own entropy
* as a parameter and it will be used instead.
*
* An optional seed password can be used to encrypt the mnemonic phrase so the seed
* cannot be derived correctly without the password. Recovering the password is not possible.
*
* @param {string} [entropy] - (Optional) 64 byte hexadecimal string entropy to be used instead of the default
* @param {string} [seedPassword] - (Optional) seed password
* @returns the generated mnemonic, seed and account
*/
generate: (entropy?: string, seedPassword?: string): Wallet => {
return generator.generateWallet(entropy, seedPassword)
},
/**
* Import a Nyzo cryptocurrency wallet from a mnemonic phrase
*
* This function imports a wallet from a mnemonic phrase. Wallet includes the mnemonic phrase,
* a seed derived with BIP39 standard and an account derived using BIP32 deterministic hierarchial
* algorithm with input parameters 44'/380' and index 0.
*
* The Nyzo address is derived from the public key using standard NyzoStrings encoding.
*
* @param {string} mnemonic - The mnemonic phrase. Words are separated with a space
* @param {string} [seedPassword] - (Optional) seed password
* @throws Throws an error if the mnemonic phrase doesn't pass validations
* @returns the wallet derived from the mnemonic (mnemonic, seed, account)
*/
fromMnemonic: (mnemonic: string, seedPassword?: string): Wallet => {
return importer.fromMnemonic(mnemonic, seedPassword)
},
/**
* Import a Nyzo cryptocurrency wallet from a seed
*
* This function imports a wallet from a seed. Wallet includes the seed and an account derived using
* BIP39 standard and an account derived using BIP32 deterministic hierarchial algorithm with input
* parameters 44'/380' and index 0.
*
* The Nyzo address is derived from the public key using standard NyzoStrings encoding.
*
* @param {string} seed - The seed
* @returns {Wallet} the wallet derived from the seed (seed, account)
*/
fromSeed: (seed: string): Wallet => {
return importer.fromSeed(seed)
},
/**
* Derive accounts for the seed
*
* This function derives Nyzo accounts with the BIP32 deterministic hierarchial algorithm
* from the given seed with input parameters 44'/380' and indexes based on the from and to
* parameters.
*
* @param {string} seed - The seed
* @param {number} from - The start index
* @param {number} to - The end index
*/
accounts: (seed: string, from: number, to: number): Account[] => {
return importer.fromSeed(seed, from, to).accounts
},
}
const tools = {
/**
* Validate mnemonic words
*
* @param {string} input The address to validate
*/
validateMnemonic: (input: string): boolean => {
return importer.validateMnemonic(input);
},
/**
* Encode an array of bytes as a Private Key Nyzostring
*
* @param {Uint8Array} input The array to encode.
*/
nyzoStringFromPrivateKey: (input: Uint8Array): string => {
return nyzoStrings.nyzoStringFromPrivateKey(input);
},
/**
* Encode an array of bytes as a Public Identifier Nyzostring
*
* @param {Uint8Array} input The array to encode.
*/
nyzoStringFromPublicIdentifier: (input: Uint8Array): string => {
return nyzoStrings.nyzoStringFromPublicIdentifier(input);
},
/**
* Encode an array of bytes as a Nyzostring given a prefix
*
* @param {string} prefix The prefix to use in the encoded string.
* @param {Uint8Array} contentBytes The array to encode.
*/
encodeNyzoString: (prefix: string, contentBytes: Uint8Array): string => {
return nyzoStrings.encodeNyzoString(prefix, contentBytes);
},
/**
* Decodes a string to a NyzoString Object
*
* @param {string} string The string to decode.
*/
decodeNyzoString: (string: string): NyzoStringPublicIdentifier | NyzoStringPrefilledData | NyzoStringPrivateSeed => {
return nyzoStrings.decodeNyzoString(string);
},
}
export {
wallet,
tools,
}