@beclab/olaresid 0.1.3 ā 0.1.5
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/dist/business/index.d.ts +3 -3
- package/dist/business/index.d.ts.map +1 -1
- package/dist/business/index.js +49 -63
- package/dist/business/index.js.map +1 -1
- package/dist/cli.js +3 -3
- package/dist/cli.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -1
- package/dist/index.js.map +1 -1
- package/dist/utils/crypto-utils.d.ts +71 -4
- package/dist/utils/crypto-utils.d.ts.map +1 -1
- package/dist/utils/crypto-utils.js +163 -51
- package/dist/utils/crypto-utils.js.map +1 -1
- package/examples/crypto-utilities.ts +3 -3
- package/examples/ed25519-jwk.ts +73 -0
- package/examples/encoding-utils.ts +96 -0
- package/examples/generate-mnemonic.ts +3 -3
- package/examples/register-subdomain.ts +4 -3
- package/examples/transfer-domain.ts +1 -1
- package/examples/wallet-management.ts +8 -8
- package/package.json +2 -3
- package/src/business/index.ts +47 -58
- package/src/cli.ts +3 -3
- package/src/index.ts +8 -23
- package/src/utils/crypto-utils.ts +198 -64
|
@@ -40,40 +40,70 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
40
40
|
};
|
|
41
41
|
})();
|
|
42
42
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
43
|
+
exports.base64ToUint8Array = base64ToUint8Array;
|
|
44
|
+
exports.uint8ArrayToHex = uint8ArrayToHex;
|
|
45
|
+
exports.hexToUint8Array = hexToUint8Array;
|
|
46
|
+
exports.uint8ArrayToBase64 = uint8ArrayToBase64;
|
|
43
47
|
exports.generateMnemonic = generateMnemonic;
|
|
44
48
|
exports.getEthereumAddressFromMnemonic = getEthereumAddressFromMnemonic;
|
|
45
49
|
exports.getEVMPrivateKeyFromMnemonic = getEVMPrivateKeyFromMnemonic;
|
|
46
50
|
exports.getDIDFromMnemonic = getDIDFromMnemonic;
|
|
51
|
+
exports.getEd25519JwkFromMnemonic = getEd25519JwkFromMnemonic;
|
|
47
52
|
exports.deriveDIDFromMnemonic = deriveDIDFromMnemonic;
|
|
48
53
|
exports.generateDIDKeyData = generateDIDKeyData;
|
|
49
54
|
exports.createRsaKeyPair = createRsaKeyPair;
|
|
50
|
-
const bip39 = __importStar(require("bip39"));
|
|
51
55
|
const varint = __importStar(require("varint"));
|
|
52
|
-
|
|
53
|
-
const
|
|
56
|
+
const base58_1 = require("multiformats/bases/base58");
|
|
57
|
+
const base64_1 = require("multiformats/bases/base64");
|
|
58
|
+
// ============================================================================
|
|
59
|
+
// Cross-platform encoding utilities (Browser + Node.js compatible)
|
|
60
|
+
// ============================================================================
|
|
54
61
|
/**
|
|
55
|
-
*
|
|
56
|
-
*
|
|
62
|
+
* Convert base64 string to Uint8Array (cross-platform)
|
|
63
|
+
* Works in both browser and Node.js environments
|
|
64
|
+
* @param base64 base64 encoded string
|
|
65
|
+
* @returns Uint8Array
|
|
57
66
|
*/
|
|
58
|
-
function
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
for (let i = 0; i <
|
|
62
|
-
|
|
63
|
-
}
|
|
64
|
-
// Convert to base58
|
|
65
|
-
let encoded = '';
|
|
66
|
-
while (num > 0n) {
|
|
67
|
-
const remainder = Number(num % 58n);
|
|
68
|
-
encoded = BASE58_ALPHABET[remainder] + encoded;
|
|
69
|
-
num = num / 58n;
|
|
67
|
+
function base64ToUint8Array(base64) {
|
|
68
|
+
const binaryString = atob(base64);
|
|
69
|
+
const bytes = new Uint8Array(binaryString.length);
|
|
70
|
+
for (let i = 0; i < binaryString.length; i++) {
|
|
71
|
+
bytes[i] = binaryString.charCodeAt(i);
|
|
70
72
|
}
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
73
|
+
return bytes;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Convert Uint8Array to hex string (cross-platform)
|
|
77
|
+
* @param bytes Uint8Array to convert
|
|
78
|
+
* @returns hex string without '0x' prefix
|
|
79
|
+
*/
|
|
80
|
+
function uint8ArrayToHex(bytes) {
|
|
81
|
+
return Array.from(bytes)
|
|
82
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
83
|
+
.join('');
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Convert hex string to Uint8Array (cross-platform)
|
|
87
|
+
* @param hex hex string (with or without '0x' prefix)
|
|
88
|
+
* @returns Uint8Array
|
|
89
|
+
*/
|
|
90
|
+
function hexToUint8Array(hex) {
|
|
91
|
+
const cleanHex = hex.startsWith('0x') ? hex.slice(2) : hex;
|
|
92
|
+
const bytes = new Uint8Array(cleanHex.length / 2);
|
|
93
|
+
for (let i = 0; i < cleanHex.length; i += 2) {
|
|
94
|
+
bytes[i / 2] = parseInt(cleanHex.slice(i, i + 2), 16);
|
|
74
95
|
}
|
|
75
|
-
|
|
76
|
-
|
|
96
|
+
return bytes;
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Convert Uint8Array to base64 string (cross-platform)
|
|
100
|
+
* Works in both browser and Node.js environments
|
|
101
|
+
* @param bytes Uint8Array to convert
|
|
102
|
+
* @returns base64 encoded string
|
|
103
|
+
*/
|
|
104
|
+
function uint8ArrayToBase64(bytes) {
|
|
105
|
+
const binaryString = String.fromCharCode(...bytes);
|
|
106
|
+
return btoa(binaryString);
|
|
77
107
|
}
|
|
78
108
|
// ============================================================================
|
|
79
109
|
// Trust Wallet Core Management
|
|
@@ -97,18 +127,9 @@ async function loadWalletCore() {
|
|
|
97
127
|
// Start loading
|
|
98
128
|
loadingPromise = (async () => {
|
|
99
129
|
try {
|
|
100
|
-
//
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
// Browser environment with ES modules
|
|
104
|
-
const { initWasm } = await Promise.resolve().then(() => __importStar(require('@trustwallet/wallet-core')));
|
|
105
|
-
walletCore = await initWasm();
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
// Node.js environment
|
|
109
|
-
const { initWasm } = require('@trustwallet/wallet-core');
|
|
110
|
-
walletCore = await initWasm();
|
|
111
|
-
}
|
|
130
|
+
// Dynamic import works in both Node.js ESM and browser ESM
|
|
131
|
+
const { initWasm } = await Promise.resolve().then(() => __importStar(require('@trustwallet/wallet-core')));
|
|
132
|
+
walletCore = await initWasm();
|
|
112
133
|
walletCoreLoaded = true;
|
|
113
134
|
return walletCore;
|
|
114
135
|
}
|
|
@@ -121,22 +142,34 @@ async function loadWalletCore() {
|
|
|
121
142
|
}
|
|
122
143
|
// multicodec code for Ed25519 keys (0xed)
|
|
123
144
|
const ED25519_CODEC_ID = varint.encode(parseInt('0xed', 16));
|
|
145
|
+
/**
|
|
146
|
+
* Simple mnemonic validation (checks word count)
|
|
147
|
+
* @param mnemonic BIP39 mnemonic phrase
|
|
148
|
+
* @returns true if mnemonic has valid word count (12, 15, 18, 21, or 24 words)
|
|
149
|
+
*/
|
|
150
|
+
function validateMnemonic(mnemonic) {
|
|
151
|
+
const words = mnemonic.trim().split(/\s+/);
|
|
152
|
+
const validWordCounts = [12, 15, 18, 21, 24];
|
|
153
|
+
return validWordCounts.includes(words.length);
|
|
154
|
+
}
|
|
124
155
|
// ============================================================================
|
|
125
156
|
// Mnemonic and Key Derivation Functions
|
|
126
157
|
// ============================================================================
|
|
127
158
|
/**
|
|
128
|
-
* Generate a random BIP39 mnemonic phrase
|
|
159
|
+
* Generate a random BIP39 mnemonic phrase using Trust Wallet Core
|
|
160
|
+
* Works in both Node.js and browser environments
|
|
161
|
+
*
|
|
129
162
|
* @param wordCount Number of words (12, 15, 18, 21, or 24), default is 12
|
|
130
|
-
* @returns A mnemonic phrase string
|
|
163
|
+
* @returns A promise that resolves to a mnemonic phrase string
|
|
131
164
|
*
|
|
132
165
|
* @example
|
|
133
166
|
* ```typescript
|
|
134
|
-
* const mnemonic = generateMnemonic(12);
|
|
167
|
+
* const mnemonic = await generateMnemonic(12);
|
|
135
168
|
* console.log(mnemonic);
|
|
136
169
|
* // Output: "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about"
|
|
137
170
|
* ```
|
|
138
171
|
*/
|
|
139
|
-
function generateMnemonic(wordCount = 12) {
|
|
172
|
+
async function generateMnemonic(wordCount = 12) {
|
|
140
173
|
// Convert word count to entropy bits
|
|
141
174
|
// 12 words = 128 bits, 15 words = 160 bits, etc.
|
|
142
175
|
const strengthMap = {
|
|
@@ -150,7 +183,12 @@ function generateMnemonic(wordCount = 12) {
|
|
|
150
183
|
if (!strength) {
|
|
151
184
|
throw new Error('Invalid word count. Must be one of: 12, 15, 18, 21, 24');
|
|
152
185
|
}
|
|
153
|
-
|
|
186
|
+
// Ensure Wallet Core is loaded
|
|
187
|
+
const core = await loadWalletCore();
|
|
188
|
+
const { HDWallet } = core;
|
|
189
|
+
const wallet = HDWallet.create(strength, '');
|
|
190
|
+
const mnemonic = wallet.mnemonic();
|
|
191
|
+
return mnemonic;
|
|
154
192
|
}
|
|
155
193
|
/**
|
|
156
194
|
* Derive Ethereum address from mnemonic using Trust Wallet Core
|
|
@@ -167,8 +205,8 @@ function generateMnemonic(wordCount = 12) {
|
|
|
167
205
|
*/
|
|
168
206
|
async function getEthereumAddressFromMnemonic(mnemonic) {
|
|
169
207
|
// Validate mnemonic
|
|
170
|
-
if (!
|
|
171
|
-
throw new Error('Invalid mnemonic phrase');
|
|
208
|
+
if (!validateMnemonic(mnemonic)) {
|
|
209
|
+
throw new Error('Invalid mnemonic phrase: must have 12, 15, 18, 21, or 24 words');
|
|
172
210
|
}
|
|
173
211
|
const core = await loadWalletCore();
|
|
174
212
|
const { HDWallet, CoinType } = core;
|
|
@@ -191,16 +229,17 @@ async function getEthereumAddressFromMnemonic(mnemonic) {
|
|
|
191
229
|
*/
|
|
192
230
|
async function getEVMPrivateKeyFromMnemonic(mnemonic) {
|
|
193
231
|
// Validate mnemonic
|
|
194
|
-
if (!
|
|
195
|
-
throw new Error('Invalid mnemonic phrase');
|
|
232
|
+
if (!validateMnemonic(mnemonic)) {
|
|
233
|
+
throw new Error('Invalid mnemonic phrase: must have 12, 15, 18, 21, or 24 words');
|
|
196
234
|
}
|
|
197
235
|
const core = await loadWalletCore();
|
|
198
236
|
const { HDWallet, CoinType } = core;
|
|
199
237
|
const wallet = HDWallet.createWithMnemonic(mnemonic, '');
|
|
200
238
|
// Get private key for Ethereum
|
|
201
239
|
const privateKeyData = wallet.getKeyForCoin(CoinType.ethereum);
|
|
202
|
-
// Convert to hex string with 0x prefix
|
|
203
|
-
const
|
|
240
|
+
// Convert to hex string with 0x prefix (cross-platform)
|
|
241
|
+
const privateKeyBytes = new Uint8Array(privateKeyData.data());
|
|
242
|
+
const privateKeyHex = '0x' + uint8ArrayToHex(privateKeyBytes);
|
|
204
243
|
return privateKeyHex;
|
|
205
244
|
}
|
|
206
245
|
/**
|
|
@@ -221,7 +260,7 @@ async function getID(mnemonic) {
|
|
|
221
260
|
idBytes.set(ED25519_CODEC_ID, 0);
|
|
222
261
|
idBytes.set(publicKey.data(), ED25519_CODEC_ID.length);
|
|
223
262
|
// Encode to base58btc
|
|
224
|
-
const id =
|
|
263
|
+
const id = base58_1.base58btc.encode(idBytes);
|
|
225
264
|
return id;
|
|
226
265
|
}
|
|
227
266
|
/**
|
|
@@ -241,12 +280,85 @@ async function getID(mnemonic) {
|
|
|
241
280
|
*/
|
|
242
281
|
async function getDIDFromMnemonic(mnemonic) {
|
|
243
282
|
// Validate mnemonic
|
|
244
|
-
if (!
|
|
245
|
-
throw new Error('Invalid mnemonic phrase');
|
|
283
|
+
if (!validateMnemonic(mnemonic)) {
|
|
284
|
+
throw new Error('Invalid mnemonic phrase: must have 12, 15, 18, 21, or 24 words');
|
|
246
285
|
}
|
|
247
286
|
const id = await getID(mnemonic);
|
|
248
287
|
return `did:key:${id}`;
|
|
249
288
|
}
|
|
289
|
+
/**
|
|
290
|
+
* Generate Ed25519 JWK (JSON Web Key) from BIP39 mnemonic phrase
|
|
291
|
+
* Uses Trust Wallet Core for key derivation, ensuring compatibility with TermiPass
|
|
292
|
+
*
|
|
293
|
+
* @param mnemonic BIP39 mnemonic phrase (12, 15, 18, 21, or 24 words)
|
|
294
|
+
* @returns Object containing publicJwk and privateJwk in RFC 7517 format
|
|
295
|
+
*
|
|
296
|
+
* @example
|
|
297
|
+
* ```typescript
|
|
298
|
+
* const { publicJwk, privateJwk } = await getEd25519JwkFromMnemonic(mnemonic);
|
|
299
|
+
*
|
|
300
|
+
* // Public JWK (safe to share)
|
|
301
|
+
* console.log(publicJwk);
|
|
302
|
+
* // {
|
|
303
|
+
* // "kty": "OKP",
|
|
304
|
+
* // "crv": "Ed25519",
|
|
305
|
+
* // "alg": "EdDSA",
|
|
306
|
+
* // "use": "sig",
|
|
307
|
+
* // "kid": "did:key:z6Mk...#z6Mk...",
|
|
308
|
+
* // "x": "base64url-encoded-public-key"
|
|
309
|
+
* // }
|
|
310
|
+
*
|
|
311
|
+
* // Private JWK (keep secure!)
|
|
312
|
+
* console.log(privateJwk);
|
|
313
|
+
* // {
|
|
314
|
+
* // "kty": "OKP",
|
|
315
|
+
* // "crv": "Ed25519",
|
|
316
|
+
* // "alg": "EdDSA",
|
|
317
|
+
* // "use": "sig",
|
|
318
|
+
* // "kid": "did:key:z6Mk...#z6Mk...",
|
|
319
|
+
* // "x": "base64url-encoded-public-key",
|
|
320
|
+
* // "d": "base64url-encoded-private-key"
|
|
321
|
+
* // }
|
|
322
|
+
* ```
|
|
323
|
+
*/
|
|
324
|
+
async function getEd25519JwkFromMnemonic(mnemonic) {
|
|
325
|
+
// Validate mnemonic
|
|
326
|
+
if (!validateMnemonic(mnemonic)) {
|
|
327
|
+
throw new Error('Invalid mnemonic phrase: must have 12, 15, 18, 21, or 24 words');
|
|
328
|
+
}
|
|
329
|
+
const core = await loadWalletCore();
|
|
330
|
+
const { HDWallet, Curve } = core;
|
|
331
|
+
const wallet = HDWallet.createWithMnemonic(mnemonic, '');
|
|
332
|
+
const privateKey = wallet.getMasterKey(Curve.ed25519);
|
|
333
|
+
const publicKey = privateKey.getPublicKeyEd25519();
|
|
334
|
+
// Get key data
|
|
335
|
+
const publicKeyBytes = publicKey.data();
|
|
336
|
+
const privateKeyBytes = privateKey.data();
|
|
337
|
+
const idBytes = new Uint8Array(publicKeyBytes.length + ED25519_CODEC_ID.length);
|
|
338
|
+
idBytes.set(ED25519_CODEC_ID, 0);
|
|
339
|
+
idBytes.set(publicKeyBytes, ED25519_CODEC_ID.length);
|
|
340
|
+
const id = base58_1.base58btc.encode(idBytes);
|
|
341
|
+
const did = `did:key:${id}`;
|
|
342
|
+
const keyId = `${did}#${id}`;
|
|
343
|
+
// Base64url encode the keys
|
|
344
|
+
const x = base64_1.base64url.baseEncode(publicKeyBytes);
|
|
345
|
+
const d = base64_1.base64url.baseEncode(privateKeyBytes);
|
|
346
|
+
// Public JWK (contains only public key material)
|
|
347
|
+
const publicJwk = {
|
|
348
|
+
kty: 'OKP', // Key Type: Octet Key Pair
|
|
349
|
+
crv: 'Ed25519', // Curve: Ed25519
|
|
350
|
+
alg: 'EdDSA', // Algorithm: EdDSA
|
|
351
|
+
use: 'sig', // Use: signature
|
|
352
|
+
kid: keyId, // Key ID
|
|
353
|
+
x: x // Public key parameter
|
|
354
|
+
};
|
|
355
|
+
// Private JWK (contains both public and private key material)
|
|
356
|
+
const privateJwk = {
|
|
357
|
+
...publicJwk,
|
|
358
|
+
d: d // Private key parameter
|
|
359
|
+
};
|
|
360
|
+
return { publicJwk, privateJwk };
|
|
361
|
+
}
|
|
250
362
|
/**
|
|
251
363
|
* Derive both owner (Ethereum address) and DID from existing mnemonic
|
|
252
364
|
*
|
|
@@ -262,8 +374,8 @@ async function getDIDFromMnemonic(mnemonic) {
|
|
|
262
374
|
*/
|
|
263
375
|
async function deriveDIDFromMnemonic(mnemonic) {
|
|
264
376
|
// Validate mnemonic once upfront
|
|
265
|
-
if (!
|
|
266
|
-
throw new Error('Invalid mnemonic phrase');
|
|
377
|
+
if (!validateMnemonic(mnemonic)) {
|
|
378
|
+
throw new Error('Invalid mnemonic phrase: must have 12, 15, 18, 21, or 24 words');
|
|
267
379
|
}
|
|
268
380
|
// Derive both in parallel for better performance
|
|
269
381
|
const [owner, did] = await Promise.all([
|
|
@@ -287,7 +399,7 @@ async function deriveDIDFromMnemonic(mnemonic) {
|
|
|
287
399
|
* ```
|
|
288
400
|
*/
|
|
289
401
|
async function generateDIDKeyData(wordCount = 12) {
|
|
290
|
-
const mnemonic = generateMnemonic(wordCount);
|
|
402
|
+
const mnemonic = await generateMnemonic(wordCount);
|
|
291
403
|
const { owner, did } = await deriveDIDFromMnemonic(mnemonic);
|
|
292
404
|
return {
|
|
293
405
|
mnemonic,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"crypto-utils.js","sourceRoot":"","sources":["../../src/utils/crypto-utils.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"crypto-utils.js","sourceRoot":"","sources":["../../src/utils/crypto-utils.ts"],"names":[],"mappings":";AAAA;;;;;;GAMG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgCH,gDAOC;AAOD,0CAIC;AAOD,0CAOC;AAQD,gDAGC;AA+ED,4CA4BC;AAeD,wEAeC;AAgBD,oEAuBC;AA4CD,gDAUC;AAqCD,8DAoDC;AAeD,sDAkBC;AAgBD,gDAWC;AAkHD,4CAkBC;AAxkBD,+CAAiC;AACjC,sDAAsD;AACtD,sDAAsD;AAkBtD,+EAA+E;AAC/E,mEAAmE;AACnE,+EAA+E;AAE/E;;;;;GAKG;AACH,SAAgB,kBAAkB,CAAC,MAAc;IAChD,MAAM,YAAY,GAAG,IAAI,CAAC,MAAM,CAAC,CAAC;IAClC,MAAM,KAAK,GAAG,IAAI,UAAU,CAAC,YAAY,CAAC,MAAM,CAAC,CAAC;IAClD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,YAAY,CAAC,MAAM,EAA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AAI,EAAE,OAAO;YACb,MAAM,EAAE,KAAK;SACb;KACD,CAAC,CAAC;IAEH,OAAO;QACN,YAAY,EAAE,SAAS;QACvB,aAAa,EAAE,UAAU;KACzB,CAAC;AACH,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,SAAgB,gBAAgB,CAC/B,SAAiB,IAAI;IAErB,sBAAsB;IACtB,IAAI,MAAM,GAAG,GAAG,EAAE,CAAC;QAClB,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;IAC7D,CAAC;IACD,IAAI,MAAM,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC;QACtB,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;IAC3D,CAAC;IAED,IAAI,MAAM,EAAE,EAAE,CAAC;QACd,4EAA4E;QAC5E,OAAO,OAAO,CAAC,OAAO,CAAC,mBAAmB,CAAC,MAAM,CAAC,CAAC,CAAC;IACrD,CAAC;SAAM,CAAC;QACP,yDAAyD;QACzD,OAAO,sBAAsB,CAAC,MAAM,CAAC,CAAC;IACvC,CAAC;AACF,CAAC"}
|
|
@@ -29,10 +29,10 @@ async function main() {
|
|
|
29
29
|
console.log('\nš Example 1: Generate Mnemonic Phrases');
|
|
30
30
|
console.log('-'.repeat(60));
|
|
31
31
|
|
|
32
|
-
const mnemonic12 = generateMnemonic(12);
|
|
32
|
+
const mnemonic12 = await generateMnemonic(12);
|
|
33
33
|
console.log('12-word mnemonic:', mnemonic12);
|
|
34
34
|
|
|
35
|
-
const mnemonic24 = generateMnemonic(24);
|
|
35
|
+
const mnemonic24 = await generateMnemonic(24);
|
|
36
36
|
console.log('24-word mnemonic:', mnemonic24);
|
|
37
37
|
|
|
38
38
|
// =========================================================================
|
|
@@ -105,7 +105,7 @@ async function main() {
|
|
|
105
105
|
console.log('\nā
Example 7: Verify Consistency');
|
|
106
106
|
console.log('-'.repeat(60));
|
|
107
107
|
|
|
108
|
-
const verifyMnemonic = generateMnemonic(12);
|
|
108
|
+
const verifyMnemonic = await generateMnemonic(12);
|
|
109
109
|
console.log('Testing mnemonic:', verifyMnemonic);
|
|
110
110
|
|
|
111
111
|
const addr1 = await getEthereumAddressFromMnemonic(verifyMnemonic);
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Example: Generate Ed25519 JWK using mnemonic and key pair derivation
|
|
3
|
+
*
|
|
4
|
+
* This example demonstrates how to:
|
|
5
|
+
* 1. Generate a BIP39 mnemonic phrase
|
|
6
|
+
* 2. Derive Ed25519 key pair from the mnemonic
|
|
7
|
+
* 3. Convert the key pair to JSON Web Key (JWK) format
|
|
8
|
+
*
|
|
9
|
+
* The JWK format is commonly used for cryptographic keys in web applications
|
|
10
|
+
* and follows the RFC 7517 specification.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { generateMnemonic, getEd25519JwkFromMnemonic } from '../src/index';
|
|
14
|
+
|
|
15
|
+
async function example_generateEd25519JWK() {
|
|
16
|
+
console.log('='.repeat(70));
|
|
17
|
+
console.log('Example: Generate Ed25519 JWK from Mnemonic');
|
|
18
|
+
console.log('='.repeat(70));
|
|
19
|
+
|
|
20
|
+
try {
|
|
21
|
+
// Step 1: Generate a new mnemonic phrase
|
|
22
|
+
console.log('\nš Step 1: Generating BIP39 mnemonic...');
|
|
23
|
+
const mnemonic = await generateMnemonic(12);
|
|
24
|
+
console.log(`Mnemonic: ${mnemonic}`);
|
|
25
|
+
|
|
26
|
+
// Step 2: Derive Ed25519 key pair and DID from mnemonic
|
|
27
|
+
console.log('\nš Step 2: Deriving Ed25519 key pair from mnemonic...');
|
|
28
|
+
const { publicJwk, privateJwk } = await getEd25519JwkFromMnemonic(
|
|
29
|
+
mnemonic
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
console.log(`Public JWK: ${JSON.stringify(publicJwk, null, 2)}`);
|
|
33
|
+
console.log(`Private JWK: ${JSON.stringify(privateJwk, null, 2)}`);
|
|
34
|
+
} catch (error: any) {
|
|
35
|
+
console.error('\nā Error:', error.message);
|
|
36
|
+
if (error.stack) {
|
|
37
|
+
console.error('\nStack trace:');
|
|
38
|
+
console.error(error.stack);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async function main() {
|
|
44
|
+
console.log('š Ed25519 JWK Generation Example\n');
|
|
45
|
+
console.log(
|
|
46
|
+
'This example shows how to generate Ed25519 keys in JWK format'
|
|
47
|
+
);
|
|
48
|
+
console.log("using olaresid's cryptographic utilities.\n");
|
|
49
|
+
|
|
50
|
+
try {
|
|
51
|
+
await example_generateEd25519JWK();
|
|
52
|
+
|
|
53
|
+
console.log('\n\n' + '='.repeat(70));
|
|
54
|
+
console.log('ā
All examples completed successfully!');
|
|
55
|
+
console.log('='.repeat(70));
|
|
56
|
+
console.log('\nā ļø SECURITY WARNING:');
|
|
57
|
+
console.log(' - Never share your mnemonic phrase or private JWK!');
|
|
58
|
+
console.log(
|
|
59
|
+
' - Store private keys securely (encrypted, hardware security modules)'
|
|
60
|
+
);
|
|
61
|
+
console.log(' - Use public JWKs for verification purposes only\n');
|
|
62
|
+
} catch (error: any) {
|
|
63
|
+
console.error('\nā Error:', error.message || error);
|
|
64
|
+
if (error.stack) {
|
|
65
|
+
console.error('\nStack trace:');
|
|
66
|
+
console.error(error.stack);
|
|
67
|
+
}
|
|
68
|
+
process.exit(1);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// Run the examples
|
|
73
|
+
main();
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Test encoding utilities exported from olaresid
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
base64ToUint8Array,
|
|
7
|
+
uint8ArrayToHex,
|
|
8
|
+
hexToUint8Array,
|
|
9
|
+
uint8ArrayToBase64
|
|
10
|
+
} from '../src/index';
|
|
11
|
+
|
|
12
|
+
async function main() {
|
|
13
|
+
console.log('============================================================');
|
|
14
|
+
console.log('Testing Cross-platform Encoding Utilities');
|
|
15
|
+
console.log(
|
|
16
|
+
'============================================================\n'
|
|
17
|
+
);
|
|
18
|
+
|
|
19
|
+
// Test data
|
|
20
|
+
const testString = 'Hello, Olares!';
|
|
21
|
+
const testBytes = new TextEncoder().encode(testString);
|
|
22
|
+
|
|
23
|
+
console.log('š Original text:', testString);
|
|
24
|
+
console.log('š¦ Original bytes:', testBytes);
|
|
25
|
+
console.log('');
|
|
26
|
+
|
|
27
|
+
// Test 1: Uint8Array to Hex
|
|
28
|
+
console.log('š Test 1: uint8ArrayToHex()');
|
|
29
|
+
console.log('------------------------------------------------------------');
|
|
30
|
+
const hexString = uint8ArrayToHex(testBytes);
|
|
31
|
+
console.log('ā
Hex string:', hexString);
|
|
32
|
+
console.log('');
|
|
33
|
+
|
|
34
|
+
// Test 2: Hex to Uint8Array
|
|
35
|
+
console.log('š Test 2: hexToUint8Array()');
|
|
36
|
+
console.log('------------------------------------------------------------');
|
|
37
|
+
const bytesFromHex = hexToUint8Array(hexString);
|
|
38
|
+
console.log('ā
Bytes from hex:', bytesFromHex);
|
|
39
|
+
console.log(
|
|
40
|
+
'ā
Matches original:',
|
|
41
|
+
JSON.stringify(bytesFromHex) === JSON.stringify(testBytes)
|
|
42
|
+
);
|
|
43
|
+
console.log('');
|
|
44
|
+
|
|
45
|
+
// Test 3: Uint8Array to Base64
|
|
46
|
+
console.log('š Test 3: uint8ArrayToBase64()');
|
|
47
|
+
console.log('------------------------------------------------------------');
|
|
48
|
+
const base64String = uint8ArrayToBase64(testBytes);
|
|
49
|
+
console.log('ā
Base64 string:', base64String);
|
|
50
|
+
console.log('');
|
|
51
|
+
|
|
52
|
+
// Test 4: Base64 to Uint8Array
|
|
53
|
+
console.log('š Test 4: base64ToUint8Array()');
|
|
54
|
+
console.log('------------------------------------------------------------');
|
|
55
|
+
const bytesFromBase64 = base64ToUint8Array(base64String);
|
|
56
|
+
console.log('ā
Bytes from base64:', bytesFromBase64);
|
|
57
|
+
console.log(
|
|
58
|
+
'ā
Matches original:',
|
|
59
|
+
JSON.stringify(bytesFromBase64) === JSON.stringify(testBytes)
|
|
60
|
+
);
|
|
61
|
+
console.log('');
|
|
62
|
+
|
|
63
|
+
// Test 5: Round-trip conversion
|
|
64
|
+
console.log('š Test 5: Round-trip conversion (hex)');
|
|
65
|
+
console.log('------------------------------------------------------------');
|
|
66
|
+
const hex1 = uint8ArrayToHex(testBytes);
|
|
67
|
+
const bytes1 = hexToUint8Array(hex1);
|
|
68
|
+
const hex2 = uint8ArrayToHex(bytes1);
|
|
69
|
+
console.log('ā
Round-trip hex successful:', hex1 === hex2);
|
|
70
|
+
console.log('');
|
|
71
|
+
|
|
72
|
+
console.log('š Test 6: Round-trip conversion (base64)');
|
|
73
|
+
console.log('------------------------------------------------------------');
|
|
74
|
+
const b64_1 = uint8ArrayToBase64(testBytes);
|
|
75
|
+
const bytes2 = base64ToUint8Array(b64_1);
|
|
76
|
+
const b64_2 = uint8ArrayToBase64(bytes2);
|
|
77
|
+
console.log('ā
Round-trip base64 successful:', b64_1 === b64_2);
|
|
78
|
+
console.log('');
|
|
79
|
+
|
|
80
|
+
// Test 7: Hex with 0x prefix
|
|
81
|
+
console.log('š Test 7: Hex with 0x prefix');
|
|
82
|
+
console.log('------------------------------------------------------------');
|
|
83
|
+
const hexWithPrefix = '0x' + hexString;
|
|
84
|
+
const bytesFromPrefixedHex = hexToUint8Array(hexWithPrefix);
|
|
85
|
+
console.log(
|
|
86
|
+
'ā
Can handle 0x prefix:',
|
|
87
|
+
JSON.stringify(bytesFromPrefixedHex) === JSON.stringify(testBytes)
|
|
88
|
+
);
|
|
89
|
+
console.log('');
|
|
90
|
+
|
|
91
|
+
console.log('============================================================');
|
|
92
|
+
console.log('ā
All encoding utility tests passed!');
|
|
93
|
+
console.log('============================================================');
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
main().catch(console.error);
|
|
@@ -21,7 +21,7 @@ async function example1_generateNew() {
|
|
|
21
21
|
|
|
22
22
|
// Generate a 12-word mnemonic
|
|
23
23
|
console.log('\nš Generating 12-word mnemonic...');
|
|
24
|
-
const mnemonic12 = generateMnemonic(12);
|
|
24
|
+
const mnemonic12 = await generateMnemonic(12);
|
|
25
25
|
console.log(`Mnemonic: ${mnemonic12}`);
|
|
26
26
|
|
|
27
27
|
console.log('\nā³ Deriving keys using Trust Wallet Core...');
|
|
@@ -35,7 +35,7 @@ async function example1_generateNew() {
|
|
|
35
35
|
// Generate a 24-word mnemonic
|
|
36
36
|
console.log('\n' + '-'.repeat(60));
|
|
37
37
|
console.log('\nš Generating 24-word mnemonic...');
|
|
38
|
-
const mnemonic24 = generateMnemonic(24);
|
|
38
|
+
const mnemonic24 = await generateMnemonic(24);
|
|
39
39
|
console.log(`Mnemonic: ${mnemonic24}`);
|
|
40
40
|
|
|
41
41
|
console.log('\nā³ Deriving keys...');
|
|
@@ -95,7 +95,7 @@ async function example4_parallelDerivation() {
|
|
|
95
95
|
console.log('Example 4: Parallel Key Derivation (Performance Test)');
|
|
96
96
|
console.log('='.repeat(60));
|
|
97
97
|
|
|
98
|
-
const mnemonic = generateMnemonic(12);
|
|
98
|
+
const mnemonic = await generateMnemonic(12);
|
|
99
99
|
console.log(`\nš Mnemonic: ${mnemonic}`);
|
|
100
100
|
|
|
101
101
|
console.log('\nā³ Deriving owner and DID in parallel...');
|
|
@@ -53,7 +53,7 @@ async function main() {
|
|
|
53
53
|
console.log('-'.repeat(60));
|
|
54
54
|
|
|
55
55
|
// Replace with your actual parent domain
|
|
56
|
-
const PARENT_DOMAIN = process.env.PARENT_DOMAIN || '
|
|
56
|
+
const PARENT_DOMAIN = process.env.PARENT_DOMAIN || '1.com';
|
|
57
57
|
const parentDomain = olaresId.domain(PARENT_DOMAIN);
|
|
58
58
|
|
|
59
59
|
console.log(`š Parent domain: ${PARENT_DOMAIN}`);
|
|
@@ -79,7 +79,7 @@ async function main() {
|
|
|
79
79
|
console.log('\nš Step 4: Generate Mnemonic for Subdomain');
|
|
80
80
|
console.log('-'.repeat(60));
|
|
81
81
|
|
|
82
|
-
const mnemonic = generateMnemonic(12);
|
|
82
|
+
const mnemonic = await generateMnemonic(12);
|
|
83
83
|
console.log('ā
Generated 12-word mnemonic:');
|
|
84
84
|
console.log(` ${mnemonic}`);
|
|
85
85
|
console.log(
|
|
@@ -92,7 +92,8 @@ async function main() {
|
|
|
92
92
|
console.log('\nš Step 5: Register Subdomain');
|
|
93
93
|
console.log('-'.repeat(60));
|
|
94
94
|
|
|
95
|
-
const SUBDOMAIN_LABEL =
|
|
95
|
+
const SUBDOMAIN_LABEL =
|
|
96
|
+
process.env.SUBDOMAIN || `example-child-${Date.now()}`;
|
|
96
97
|
console.log(`š Subdomain label: ${SUBDOMAIN_LABEL}`);
|
|
97
98
|
console.log(`š Full domain will be: ${SUBDOMAIN_LABEL}.${PARENT_DOMAIN}`);
|
|
98
99
|
|
|
@@ -98,7 +98,7 @@ async function main() {
|
|
|
98
98
|
mnemonic = process.env.NEW_OWNER_MNEMONIC;
|
|
99
99
|
console.log('š Using provided mnemonic');
|
|
100
100
|
} else {
|
|
101
|
-
mnemonic = generateMnemonic(12);
|
|
101
|
+
mnemonic = await generateMnemonic(12);
|
|
102
102
|
console.log('š Generated 12-word mnemonic:');
|
|
103
103
|
console.log(` ${mnemonic}`);
|
|
104
104
|
console.log(
|