hd-wallet-wasm 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +275 -0
- package/dist/hd-wallet.js +0 -0
- package/dist/hd-wallet.wasm +0 -0
- package/dist/index.d.ts +609 -0
- package/package.json +76 -0
- package/src/index.d.ts +609 -0
- package/src/index.mjs +2479 -0
package/README.md
ADDED
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
# hd-wallet-wasm
|
|
2
|
+
|
|
3
|
+
A comprehensive HD (Hierarchical Deterministic) wallet implementation compiled to WebAssembly. Implements BIP-32, BIP-39, and BIP-44 standards with multi-curve cryptography and multi-chain support.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **BIP-32/39/44/49/84** - Complete HD wallet derivation standards
|
|
8
|
+
- **Multi-curve support** - secp256k1, Ed25519, P-256, P-384, X25519
|
|
9
|
+
- **Multi-chain** - Bitcoin, Ethereum, Solana, Cosmos, Polkadot
|
|
10
|
+
- **Hardware wallet ready** - Trezor, Ledger, KeepKey abstraction layer
|
|
11
|
+
- **Secure** - Crypto++ backend, secure memory handling
|
|
12
|
+
- **Fast** - WebAssembly performance
|
|
13
|
+
- **TypeScript** - Full type definitions included
|
|
14
|
+
|
|
15
|
+
## Installation
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install hd-wallet-wasm
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## Quick Start
|
|
22
|
+
|
|
23
|
+
```javascript
|
|
24
|
+
import init from 'hd-wallet-wasm';
|
|
25
|
+
|
|
26
|
+
// Initialize the WASM module
|
|
27
|
+
const wallet = await init();
|
|
28
|
+
|
|
29
|
+
// Inject entropy (required in WASI environments)
|
|
30
|
+
const entropy = crypto.getRandomValues(new Uint8Array(32));
|
|
31
|
+
wallet.injectEntropy(entropy);
|
|
32
|
+
|
|
33
|
+
// Generate a 24-word mnemonic
|
|
34
|
+
const mnemonic = wallet.mnemonic.generate(24);
|
|
35
|
+
console.log('Mnemonic:', mnemonic);
|
|
36
|
+
|
|
37
|
+
// Derive seed from mnemonic
|
|
38
|
+
const seed = wallet.mnemonic.toSeed(mnemonic, 'optional passphrase');
|
|
39
|
+
|
|
40
|
+
// Create master key
|
|
41
|
+
const master = wallet.hdkey.fromSeed(seed);
|
|
42
|
+
|
|
43
|
+
// Derive Bitcoin key (BIP-44: m/44'/0'/0'/0/0)
|
|
44
|
+
const btcKey = master.derivePath("m/44'/0'/0'/0/0");
|
|
45
|
+
console.log('Bitcoin public key:', wallet.utils.encodeHex(btcKey.publicKey()));
|
|
46
|
+
|
|
47
|
+
// Get Bitcoin address
|
|
48
|
+
const btcAddress = wallet.bitcoin.getAddress(btcKey.publicKey(), 0); // P2PKH
|
|
49
|
+
console.log('Bitcoin address:', btcAddress);
|
|
50
|
+
|
|
51
|
+
// Derive Ethereum key (BIP-44: m/44'/60'/0'/0/0)
|
|
52
|
+
const ethKey = master.derivePath("m/44'/60'/0'/0/0");
|
|
53
|
+
const ethAddress = wallet.ethereum.getAddress(ethKey.publicKey());
|
|
54
|
+
console.log('Ethereum address:', ethAddress);
|
|
55
|
+
|
|
56
|
+
// Sign a message
|
|
57
|
+
const signature = wallet.curves.secp256k1.sign(
|
|
58
|
+
wallet.utils.sha256(new TextEncoder().encode('Hello, World!')),
|
|
59
|
+
ethKey.privateKey()
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
// Clean up
|
|
63
|
+
btcKey.wipe();
|
|
64
|
+
ethKey.wipe();
|
|
65
|
+
master.wipe();
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## API Overview
|
|
69
|
+
|
|
70
|
+
### Mnemonic (BIP-39)
|
|
71
|
+
|
|
72
|
+
```javascript
|
|
73
|
+
// Generate mnemonic (12, 15, 18, 21, or 24 words)
|
|
74
|
+
const mnemonic = wallet.mnemonic.generate(24);
|
|
75
|
+
|
|
76
|
+
// Validate mnemonic
|
|
77
|
+
const isValid = wallet.mnemonic.validate(mnemonic);
|
|
78
|
+
|
|
79
|
+
// Convert to seed
|
|
80
|
+
const seed = wallet.mnemonic.toSeed(mnemonic, 'passphrase');
|
|
81
|
+
|
|
82
|
+
// Convert to/from entropy
|
|
83
|
+
const entropy = wallet.mnemonic.toEntropy(mnemonic);
|
|
84
|
+
const recovered = wallet.mnemonic.fromEntropy(entropy);
|
|
85
|
+
|
|
86
|
+
// Multiple languages supported
|
|
87
|
+
import { Language } from 'hd-wallet-wasm';
|
|
88
|
+
const japanese = wallet.mnemonic.generate(24, Language.JAPANESE);
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### HD Keys (BIP-32)
|
|
92
|
+
|
|
93
|
+
```javascript
|
|
94
|
+
// From seed
|
|
95
|
+
const master = wallet.hdkey.fromSeed(seed);
|
|
96
|
+
|
|
97
|
+
// From extended key
|
|
98
|
+
const restored = wallet.hdkey.fromXprv('xprv...');
|
|
99
|
+
const watchOnly = wallet.hdkey.fromXpub('xpub...');
|
|
100
|
+
|
|
101
|
+
// Derivation
|
|
102
|
+
const child = master.deriveChild(0);
|
|
103
|
+
const hardened = master.deriveHardened(0);
|
|
104
|
+
const path = master.derivePath("m/44'/0'/0'/0/0");
|
|
105
|
+
|
|
106
|
+
// Serialization
|
|
107
|
+
const xprv = master.toXprv();
|
|
108
|
+
const xpub = master.toXpub();
|
|
109
|
+
|
|
110
|
+
// Get neutered (public only) version
|
|
111
|
+
const pubOnly = master.neutered();
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Multi-Curve Cryptography
|
|
115
|
+
|
|
116
|
+
```javascript
|
|
117
|
+
import { Curve } from 'hd-wallet-wasm';
|
|
118
|
+
|
|
119
|
+
// secp256k1 (Bitcoin, Ethereum)
|
|
120
|
+
const sig = wallet.curves.secp256k1.sign(message, privateKey);
|
|
121
|
+
const valid = wallet.curves.secp256k1.verify(message, sig, publicKey);
|
|
122
|
+
const shared = wallet.curves.secp256k1.ecdh(myPrivate, theirPublic);
|
|
123
|
+
|
|
124
|
+
// Ed25519 (Solana)
|
|
125
|
+
const edSig = wallet.curves.ed25519.sign(message, privateKey);
|
|
126
|
+
const edValid = wallet.curves.ed25519.verify(message, edSig, publicKey);
|
|
127
|
+
|
|
128
|
+
// P-256, P-384 (FIPS compliant)
|
|
129
|
+
const p256Sig = wallet.curves.p256.sign(message, privateKey);
|
|
130
|
+
const p384Sig = wallet.curves.p384.sign(message, privateKey);
|
|
131
|
+
|
|
132
|
+
// X25519 (key exchange only)
|
|
133
|
+
const x25519Shared = wallet.curves.x25519.ecdh(myPrivate, theirPublic);
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
### Bitcoin
|
|
137
|
+
|
|
138
|
+
```javascript
|
|
139
|
+
import { BitcoinAddressType, Network } from 'hd-wallet-wasm';
|
|
140
|
+
|
|
141
|
+
// Address generation
|
|
142
|
+
const p2pkh = wallet.bitcoin.getAddress(pubKey, BitcoinAddressType.P2PKH);
|
|
143
|
+
const p2wpkh = wallet.bitcoin.getAddress(pubKey, BitcoinAddressType.P2WPKH);
|
|
144
|
+
const p2tr = wallet.bitcoin.getAddress(pubKey, BitcoinAddressType.P2TR);
|
|
145
|
+
|
|
146
|
+
// Testnet
|
|
147
|
+
const testAddr = wallet.bitcoin.getAddress(pubKey, BitcoinAddressType.P2WPKH, Network.TESTNET);
|
|
148
|
+
|
|
149
|
+
// Message signing (Bitcoin Signed Message format)
|
|
150
|
+
const sig = wallet.bitcoin.signMessage('Hello', privateKey);
|
|
151
|
+
const valid = wallet.bitcoin.verifyMessage('Hello', sig, address);
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
### Ethereum
|
|
155
|
+
|
|
156
|
+
```javascript
|
|
157
|
+
// Address (EIP-55 checksummed)
|
|
158
|
+
const address = wallet.ethereum.getAddress(publicKey);
|
|
159
|
+
|
|
160
|
+
// Message signing (EIP-191)
|
|
161
|
+
const sig = wallet.ethereum.signMessage('Hello', privateKey);
|
|
162
|
+
|
|
163
|
+
// Typed data signing (EIP-712)
|
|
164
|
+
const typedSig = wallet.ethereum.signTypedData(typedData, privateKey);
|
|
165
|
+
|
|
166
|
+
// Verify and recover signer
|
|
167
|
+
const signer = wallet.ethereum.verifyMessage('Hello', sig);
|
|
168
|
+
```
|
|
169
|
+
|
|
170
|
+
### Solana
|
|
171
|
+
|
|
172
|
+
```javascript
|
|
173
|
+
// Address (Base58)
|
|
174
|
+
const address = wallet.solana.getAddress(publicKey);
|
|
175
|
+
|
|
176
|
+
// Message signing
|
|
177
|
+
const sig = wallet.solana.signMessage(message, privateKey);
|
|
178
|
+
const valid = wallet.solana.verifyMessage(message, sig, publicKey);
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Cosmos
|
|
182
|
+
|
|
183
|
+
```javascript
|
|
184
|
+
// Address with custom prefix
|
|
185
|
+
const cosmosAddr = wallet.cosmos.getAddress(publicKey, 'cosmos');
|
|
186
|
+
const osmoAddr = wallet.cosmos.getAddress(publicKey, 'osmo');
|
|
187
|
+
|
|
188
|
+
// Amino signing (legacy)
|
|
189
|
+
const aminoSig = wallet.cosmos.signAmino(aminoDoc, privateKey);
|
|
190
|
+
|
|
191
|
+
// Direct signing (protobuf)
|
|
192
|
+
const directSig = wallet.cosmos.signDirect(bodyBytes, authInfoBytes, chainId, accountNumber, privateKey);
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### Polkadot
|
|
196
|
+
|
|
197
|
+
```javascript
|
|
198
|
+
// SS58 address
|
|
199
|
+
const dotAddr = wallet.polkadot.getAddress(publicKey, 0); // Polkadot
|
|
200
|
+
const ksmAddr = wallet.polkadot.getAddress(publicKey, 2); // Kusama
|
|
201
|
+
|
|
202
|
+
// Message signing
|
|
203
|
+
const sig = wallet.polkadot.signMessage(message, privateKey);
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Utilities
|
|
207
|
+
|
|
208
|
+
```javascript
|
|
209
|
+
// Hashing
|
|
210
|
+
const sha256 = wallet.utils.sha256(data);
|
|
211
|
+
const keccak = wallet.utils.keccak256(data);
|
|
212
|
+
const blake2b = wallet.utils.blake2b(data, 32);
|
|
213
|
+
|
|
214
|
+
// Encoding
|
|
215
|
+
const hex = wallet.utils.encodeHex(data);
|
|
216
|
+
const base58 = wallet.utils.encodeBase58(data);
|
|
217
|
+
const bech32 = wallet.utils.encodeBech32('bc', data);
|
|
218
|
+
|
|
219
|
+
// Key derivation
|
|
220
|
+
const derived = wallet.utils.hkdf(ikm, salt, info, 32);
|
|
221
|
+
const pbkdf2 = wallet.utils.pbkdf2(password, salt, 100000, 32);
|
|
222
|
+
|
|
223
|
+
// Secure wipe
|
|
224
|
+
wallet.utils.secureWipe(sensitiveData);
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## Coin Types (SLIP-44)
|
|
228
|
+
|
|
229
|
+
```javascript
|
|
230
|
+
import { CoinType } from 'hd-wallet-wasm';
|
|
231
|
+
|
|
232
|
+
CoinType.BITCOIN // 0
|
|
233
|
+
CoinType.BITCOIN_TESTNET // 1
|
|
234
|
+
CoinType.LITECOIN // 2
|
|
235
|
+
CoinType.ETHEREUM // 60
|
|
236
|
+
CoinType.COSMOS // 118
|
|
237
|
+
CoinType.POLKADOT // 354
|
|
238
|
+
CoinType.SOLANA // 501
|
|
239
|
+
// ... and 50+ more
|
|
240
|
+
```
|
|
241
|
+
|
|
242
|
+
## Browser Usage
|
|
243
|
+
|
|
244
|
+
```html
|
|
245
|
+
<script type="module">
|
|
246
|
+
import init from 'https://unpkg.com/hd-wallet-wasm/src/index.mjs';
|
|
247
|
+
|
|
248
|
+
const wallet = await init();
|
|
249
|
+
// Use wallet...
|
|
250
|
+
</script>
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
## Security Notes
|
|
254
|
+
|
|
255
|
+
- Always inject entropy from a cryptographically secure source before generating mnemonics
|
|
256
|
+
- Use `wipe()` to securely clear sensitive key material when done
|
|
257
|
+
- Never log or expose private keys or mnemonics
|
|
258
|
+
- Consider using hardware wallets for high-value operations
|
|
259
|
+
- The library enforces key separation: external chain (0) for signing, internal chain (1) for encryption
|
|
260
|
+
|
|
261
|
+
## FIPS Mode
|
|
262
|
+
|
|
263
|
+
When built with FIPS mode enabled, only FIPS-approved algorithms are available:
|
|
264
|
+
- P-256 and P-384 ECDSA (not secp256k1 or Ed25519)
|
|
265
|
+
- FIPS-approved random number generation
|
|
266
|
+
|
|
267
|
+
## License
|
|
268
|
+
|
|
269
|
+
Apache-2.0
|
|
270
|
+
|
|
271
|
+
## Links
|
|
272
|
+
|
|
273
|
+
- [Documentation](https://digitalarsenal.github.io/hd-wallet-wasm/)
|
|
274
|
+
- [GitHub](https://github.com/DigitalArsenal/hd-wallet-wasm)
|
|
275
|
+
- [API Reference](https://digitalarsenal.github.io/hd-wallet-wasm/api/)
|
|
Binary file
|
|
Binary file
|