@cheny56/node-client 1.0.11 → 1.0.12

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cheny56/node-client",
3
- "version": "1.0.11",
3
+ "version": "1.0.12",
4
4
  "description": "Client library for Quorum blockchain with Post-Quantum Cryptography (PQC) and Zero-Knowledge Proof (ZK) support",
5
5
  "main": "./src/index.js",
6
6
  "types": "src/index.d.ts",
package/src/contract.js CHANGED
@@ -161,9 +161,7 @@ class ERC20Token extends Contract {
161
161
  async transferPQC(wallet, provider, to, amount, txOptions = {}) {
162
162
  const data = this.encodeFunctionData('transfer', [to, amount]);
163
163
  // Ensure wallet is initialized
164
- if (!wallet._initialized) {
165
- await wallet._ensureKeyPair();
166
- }
164
+ await wallet._initPromise;
167
165
  const nonce = await provider.getTransactionCount(wallet.address, 'pending');
168
166
 
169
167
  const tx = new PQCLegacyTransaction({
@@ -122,9 +122,7 @@ class PQCLegacyTransaction extends BaseTransaction {
122
122
  */
123
123
  async sign(wallet) {
124
124
  // Ensure wallet is initialized
125
- if (!wallet._initialized) {
126
- await wallet._ensureKeyPair();
127
- }
125
+ await wallet._initPromise;
128
126
 
129
127
  const hash = ethers.getBytes(this.getSigningHash());
130
128
  this.pqcPubKey = wallet.publicKey;
@@ -237,9 +235,7 @@ class HybridLegacyTransaction extends BaseTransaction {
237
235
  */
238
236
  async sign(ecdsaWallet, pqcWallet) {
239
237
  // Ensure PQC wallet is initialized
240
- if (!pqcWallet._initialized) {
241
- await pqcWallet._ensureKeyPair();
242
- }
238
+ await pqcWallet._initPromise;
243
239
 
244
240
  const hash = this.getSigningHash();
245
241
  const hashBytes = ethers.getBytes(hash);
@@ -389,9 +385,7 @@ class PQCAccessListTransaction extends BaseTransaction {
389
385
 
390
386
  async sign(wallet) {
391
387
  // Ensure wallet is initialized
392
- if (!wallet._initialized) {
393
- await wallet._ensureKeyPair();
394
- }
388
+ await wallet._initPromise;
395
389
 
396
390
  const hash = ethers.getBytes(this.getSigningHash());
397
391
  this.pqcPubKey = wallet.publicKey;
@@ -546,9 +540,7 @@ class PQCDynamicFeeTransaction extends BaseTransaction {
546
540
 
547
541
  async sign(wallet) {
548
542
  // Ensure wallet is initialized
549
- if (!wallet._initialized) {
550
- await wallet._ensureKeyPair();
551
- }
543
+ await wallet._initPromise;
552
544
 
553
545
  const hash = ethers.getBytes(this.getSigningHash());
554
546
  this.pqcPubKey = wallet.publicKey;
package/src/wallet.js CHANGED
@@ -68,6 +68,7 @@ class PQCWallet {
68
68
  this.publicKey = publicKey instanceof Uint8Array ? publicKey : new Uint8Array(publicKey);
69
69
  this.address = derivePQCAddress(this.publicKey);
70
70
  this._initialized = true;
71
+ this._initPromise = Promise.resolve();
71
72
  } else {
72
73
  // Generate new key pair using dilithium-crystals
73
74
  // dilithium-crystals uses keyPair() which is async and returns { publicKey, secretKey }
@@ -75,9 +76,21 @@ class PQCWallet {
75
76
  this.publicKey = null;
76
77
  this.address = null;
77
78
  this._initialized = false;
79
+ // Start initialization immediately
80
+ this._initPromise = this._ensureKeyPair();
78
81
  }
79
82
  }
80
83
 
84
+ /**
85
+ * Create a new PQC wallet with initialized keys (static factory method)
86
+ * @returns {Promise<PQCWallet>} Initialized wallet
87
+ */
88
+ static async create() {
89
+ const wallet = new PQCWallet();
90
+ await wallet._initPromise;
91
+ return wallet;
92
+ }
93
+
81
94
  /**
82
95
  * Initialize key pair if not already done (async)
83
96
  * @private
@@ -98,6 +111,15 @@ class PQCWallet {
98
111
  this._initialized = true;
99
112
  }
100
113
 
114
+ /**
115
+ * Get address (will initialize keys if needed)
116
+ * @returns {Promise<string>} Wallet address
117
+ */
118
+ async getAddress() {
119
+ await this._initPromise;
120
+ return this.address;
121
+ }
122
+
101
123
  /**
102
124
  * Create wallet from secret key
103
125
  * Note: In practice, you would derive the public key from the secret key.
@@ -122,9 +144,7 @@ class PQCWallet {
122
144
  */
123
145
  async sign(hash) {
124
146
  // Ensure key pair is initialized
125
- if (!this._initialized) {
126
- await this._ensureKeyPair();
127
- }
147
+ await this._initPromise;
128
148
 
129
149
  // dilithium-crystals API: sign(secretKey, message)
130
150
  // Some implementations may require context, but we'll try without first
@@ -146,9 +166,7 @@ class PQCWallet {
146
166
  */
147
167
  async verify(hash, signature) {
148
168
  // Ensure key pair is initialized
149
- if (!this._initialized) {
150
- await this._ensureKeyPair();
151
- }
169
+ await this._initPromise;
152
170
 
153
171
  // dilithium-crystals API: verify(publicKey, message, signature)
154
172
  if (dilithium.verify) {
@@ -166,9 +184,7 @@ class PQCWallet {
166
184
  * @returns {Promise<string>} Public key (hex)
167
185
  */
168
186
  async getPublicKeyHex() {
169
- if (!this._initialized) {
170
- await this._ensureKeyPair();
171
- }
187
+ await this._initPromise;
172
188
  return ethers.hexlify(this.publicKey);
173
189
  }
174
190
 
@@ -177,9 +193,7 @@ class PQCWallet {
177
193
  * @returns {Promise<string>} Secret key (hex)
178
194
  */
179
195
  async getSecretKeyHex() {
180
- if (!this._initialized) {
181
- await this._ensureKeyPair();
182
- }
196
+ await this._initPromise;
183
197
  return ethers.hexlify(this.secretKey);
184
198
  }
185
199
  }
@@ -198,6 +212,24 @@ class HybridWallet {
198
212
  this._initialized = false;
199
213
  }
200
214
 
215
+ /**
216
+ * Create a new Hybrid wallet with initialized keys (static factory method)
217
+ * @param {ECDSAWallet|string} ecdsaWallet - ECDSA wallet or private key
218
+ * @returns {Promise<HybridWallet>} Initialized hybrid wallet
219
+ */
220
+ static async create(ecdsaWallet) {
221
+ const ecdsa = ecdsaWallet instanceof ECDSAWallet ? ecdsaWallet : new ECDSAWallet(ecdsaWallet);
222
+ const pqc = new PQCWallet();
223
+ await pqc._initPromise; // Wait for PQC keys to be generated
224
+ const wallet = new HybridWallet(ecdsa, pqc);
225
+ wallet.address = deriveHybridAddress(
226
+ ethers.getBytes(ecdsa.getPublicKey()),
227
+ pqc.publicKey
228
+ );
229
+ wallet._initialized = true;
230
+ return wallet;
231
+ }
232
+
201
233
  /**
202
234
  * Get the hybrid address (computed from ECDSA + PQC public keys)
203
235
  * @returns {Promise<string>} Hybrid address
@@ -208,9 +240,7 @@ class HybridWallet {
208
240
  }
209
241
 
210
242
  // Ensure PQC wallet has keys
211
- if (!this.pqcWallet._initialized) {
212
- await this.pqcWallet._ensureKeyPair();
213
- }
243
+ await this.pqcWallet._initPromise;
214
244
 
215
245
  this.address = deriveHybridAddress(
216
246
  ethers.getBytes(this.ecdsaWallet.getPublicKey()),