@ocap/client 1.30.3 → 1.30.4

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/lib/extension.js CHANGED
@@ -1,41 +1,57 @@
1
1
  'use strict';
2
2
 
3
- const get = require('lodash/get');
4
- const set = require('lodash/set');
5
- const omit = require('lodash/omit');
6
- const cloneDeep = require('lodash/cloneDeep');
7
- const camelCase = require('lodash/camelCase');
3
+ const { get, set, omit, camelCase } = require('./helpers/lite');
8
4
  const { isValid } = require('@arcblock/did');
9
- const ocapProto = require('@ocap/proto');
5
+ const ocapProto = require('@ocap/proto/schema');
10
6
  const { toTxHash } = require('@ocap/mcrypto');
11
- const { createMessage, getMessageType, toTypeUrl } = require('@ocap/message');
7
+ const { encodeTx: encodeTxCbor, decodeTx: decodeTxCbor } = require('@ocap/message/cbor');
12
8
  const { bytesToHex, toBase58, toBase64, toHex, toBuffer, fromTokenToUnit, fromUnitToToken } = require('@ocap/util');
13
9
  const { DEFAULT_TOKEN_DECIMAL } = require('@ocap/util/lib/constant');
14
- const { formatFactoryState, preMintFromFactory, isValidFactory } = require('@ocap/asset');
10
+ const { formatFactoryState, preMintFromFactory: preMintFromFactoryCbor } = require('@ocap/asset/mint/client');
15
11
  const {
16
12
  toDelegateAddress,
17
- toAssetAddress,
18
- toFactoryAddress,
19
- toTokenAddress,
20
- toTokenFactoryAddress,
13
+ toAssetAddress: toAssetAddressCbor,
14
+ toFactoryAddress: toFactoryAddressCbor,
15
+ toTokenAddress: toTokenAddressCbor,
16
+ toTokenFactoryAddress: toTokenFactoryAddressCbor,
21
17
  toStakeAddress,
22
- toRollupAddress
23
- } = require('@arcblock/did-util');
18
+ toRollupAddress: toRollupAddressCbor
19
+ } = require('@arcblock/did-util/cbor');
24
20
 
25
21
  const debug = require('debug')(require('../package.json').name);
26
22
 
27
- const { transactions, typeUrls, multiSignTxs, multiSignV2Txs } = ocapProto;
23
+ const { transactions, typeUrls, toTypeUrl, multiSignTxs, multiSignV2Txs } = ocapProto;
24
+
25
+ // Default CBOR-only dependency bundle. `@ocap/client/legacy` re-invokes
26
+ // `createExtensionMethods` with a protobuf-wired bundle so the main entry
27
+ // stays free of `google-protobuf` / `*_pb.js`. See README §4.7 / §4.10.
28
+ const defaultDeps = {
29
+ encodeTx: encodeTxCbor,
30
+ decodeTx: decodeTxCbor,
31
+ preMintFromFactory: preMintFromFactoryCbor,
32
+ toAssetAddress: toAssetAddressCbor,
33
+ toFactoryAddress: toFactoryAddressCbor,
34
+ toTokenAddress: toTokenAddressCbor,
35
+ toTokenFactoryAddress: toTokenFactoryAddressCbor,
36
+ toRollupAddress: toRollupAddressCbor
37
+ };
28
38
 
29
39
  // FIXME: delete this after @ocap/proto updated
30
40
  const filteredTransactions = transactions.filter(x => !['DeclareTx'].includes(x));
31
41
 
32
42
  /**
33
- * Generate extension methods on the fly
43
+ * Generate extension methods on the fly.
44
+ *
45
+ * The `deps` parameter lets the `@ocap/client/legacy` subpath re-invoke this
46
+ * factory with protobuf-wired encoders and address helpers. Callers of the
47
+ * main entry use the default CBOR bundle and never drag `google-protobuf`
48
+ * or any `*_pb.js` module into the graph.
34
49
  *
35
50
  * @public
36
51
  * @param {GraphQLClient} client
52
+ * @param {object} [deps=defaultDeps]
37
53
  */
38
- const createExtensionMethods = client => {
54
+ const createExtensionMethods = (client, deps = defaultDeps) => {
39
55
  Object.defineProperty(client, 'ctx', {
40
56
  value: {
41
57
  loading: false,
@@ -44,6 +60,17 @@ const createExtensionMethods = client => {
44
60
  }
45
61
  });
46
62
 
63
+ const {
64
+ encodeTx,
65
+ decodeTx,
66
+ preMintFromFactory,
67
+ toAssetAddress,
68
+ toFactoryAddress,
69
+ toTokenAddress,
70
+ toTokenFactoryAddress,
71
+ toRollupAddress
72
+ } = deps;
73
+
47
74
  /**
48
75
  * Format big number presentation amount to token number
49
76
  *
@@ -113,17 +140,6 @@ const createExtensionMethods = client => {
113
140
  */
114
141
  client.getTxMultiSignMethods = () => multiSignTxs.concat(multiSignV2Txs).map(x => camelCase(`multi_sign_${x}`));
115
142
 
116
- /**
117
- * Get protobuf message class by name, only supports forge-built-in types
118
- *
119
- * @memberof GraphQLClient
120
- * @function
121
- * @name GraphQLClient#getType
122
- * @param {string} x
123
- * @returns {class|null} message type
124
- */
125
- client.getType = x => getMessageType(x).fn;
126
-
127
143
  /**
128
144
  * Decode transaction buffer/base64/base58 to an object
129
145
  *
@@ -133,10 +149,7 @@ const createExtensionMethods = client => {
133
149
  * @param {buffer|hex|base48|base64} input
134
150
  * @returns {object} transaction object
135
151
  */
136
- client.decodeTx = input => {
137
- const Transaction = client.getType('Transaction');
138
- return Transaction.deserializeBinary(toBuffer(input)).toObject();
139
- };
152
+ client.decodeTx = input => decodeTx(toBuffer(input));
140
153
 
141
154
  /**
142
155
  * Ensure a connection is bootstrapped with some meta info fetched from chain node
@@ -341,10 +354,14 @@ const createExtensionMethods = client => {
341
354
  signatures = tx.signaturesList;
342
355
  }
343
356
 
344
- // Determine itx
357
+ // Determine itx. If the caller already wrapped tx.itx as {type, value}
358
+ // via a prior sign call (e.g. signExchangeTx → multiSignExchangeV2Tx),
359
+ // unwrap and re-tag with the current outer type to avoid double-nesting.
345
360
  let itx = null;
346
361
  if (tx.itx.typeUrl && tx.itx.value) {
347
362
  itx = tx.itx;
363
+ } else if (tx.itx.type && tx.itx.value && !Array.isArray(tx.itx.value)) {
364
+ itx = { type: x, value: tx.itx.value };
348
365
  } else {
349
366
  itx = { type: x, value: tx.itx };
350
367
  }
@@ -352,22 +369,22 @@ const createExtensionMethods = client => {
352
369
  const type = itx.typeUrl || toTypeUrl(itx.type);
353
370
  const feeConfig = get(context, 'txConfig.txFee', []).find(t => t.typeUrl === type);
354
371
 
355
- const txObj = createMessage('Transaction', {
372
+ const txParams = {
356
373
  from: tx.delegator ? address : delegator || address,
357
374
  nonce,
358
375
  pk,
359
376
  chainId,
360
- signature: tx.signature || Buffer.from([]),
377
+ signature: tx.signature || new Uint8Array(0),
361
378
  signatures,
362
379
  delegator: tx.delegator || (delegator ? address : ''),
363
380
  itx,
364
381
  serviceFee: feeConfig ? feeConfig.fee : '0'
365
- });
366
- const txToSignBytes = txObj.serializeBinary();
382
+ };
383
+ const txToSignBytes = encodeTx(txParams);
367
384
 
368
- debug(`encodeTx.${x}.txObj`, txObj.toObject());
385
+ debug(`encodeTx.${x}.txParams`, txParams);
369
386
 
370
- return { object: txObj.toObject(), buffer: Buffer.from(txToSignBytes) };
387
+ return { object: txParams, buffer: Buffer.from(txToSignBytes) };
371
388
  };
372
389
 
373
390
  const encodeMethod = camelCase(`encode_${x}`);
@@ -416,11 +433,10 @@ const createExtensionMethods = client => {
416
433
  return new Promise(async (resolve, reject) => {
417
434
  let txHash = null;
418
435
  try {
419
- const txMsg = createMessage('Transaction', encoded);
420
- const txBytes = txMsg.serializeBinary();
436
+ const txBytes = encodeTx(encoded);
421
437
  const txStr = toBase64(txBytes);
422
438
  txHash = toTxHash(txBytes);
423
- debug(`sendTx.${x}.txObj`, txMsg.toObject());
439
+ debug(`sendTx.${x}.txParams`, encoded);
424
440
  const { hash } = await client.sendTx({ tx: txStr, commit, extra: typeof txExtra === 'string' ? txExtra : JSON.stringify(txExtra) }, extra);
425
441
  resolve(hash);
426
442
  } catch (err) {
@@ -474,16 +490,16 @@ const createExtensionMethods = client => {
474
490
  if (typeof wallet.toAddress !== 'function') {
475
491
  throw new Error('Multisig requires a valid wallet');
476
492
  }
477
- tx.signaturesList = tx.signatures || tx.signaturesList || [];
493
+ tx.signatures = tx.signatures || tx.signaturesList || [];
478
494
  if (delegator) {
479
- tx.signaturesList.unshift({
495
+ tx.signatures.unshift({
480
496
  pk: wallet.publicKey,
481
497
  signer: delegator,
482
498
  delegator: wallet.address,
483
499
  data
484
500
  });
485
501
  } else {
486
- tx.signaturesList.unshift({
502
+ tx.signatures.unshift({
487
503
  pk: wallet.publicKey,
488
504
  signer: wallet.address,
489
505
  delegator: '',
@@ -492,7 +508,7 @@ const createExtensionMethods = client => {
492
508
  }
493
509
 
494
510
  const { object, buffer } = await txEncodeFn({ tx, wallet });
495
- object.signaturesList[0].signature = await wallet.sign(bytesToHex(buffer));
511
+ object.signatures[0].signature = await wallet.sign(bytesToHex(buffer));
496
512
  return _formatEncodedTx(object, encoding);
497
513
  };
498
514
  const multiSignMethod = camelCase(`multi_sign_${x}`);
@@ -507,17 +523,17 @@ const createExtensionMethods = client => {
507
523
  }
508
524
 
509
525
  const signer = wallet.address;
510
- const signatures = cloneDeep(tx.signatures || tx.signaturesList || []);
526
+ const signatures = structuredClone(tx.signatures || tx.signaturesList || []);
511
527
  const item = signatures.find(s => s.signer === signer || s.delegator === signer);
512
528
  if (!item) {
513
529
  throw new Error('Signer not in the list');
514
530
  }
515
531
 
516
- tx.signaturesList = signatures.map(s => omit(s, ['signature']));
532
+ tx.signatures = signatures.map(s => omit(s, ['signature']));
517
533
  const { object, buffer } = await txEncodeFn({ tx, wallet });
518
534
 
519
535
  item.signature = await wallet.sign(bytesToHex(buffer));
520
- object.signaturesList = signatures;
536
+ object.signatures = signatures;
521
537
  return _formatEncodedTx(object, encoding);
522
538
  };
523
539
  const multiSignMethod = camelCase(`multi_sign_${x}`);
@@ -715,8 +731,11 @@ const createExtensionMethods = client => {
715
731
 
716
732
  itx.address = toFactoryAddress(itx);
717
733
 
718
- isValidFactory(itx, true);
719
-
734
+ // Client-side factory schema validation (isValidFactory → valibot +
735
+ // @ocap/validator) removed in Phase 5: it pulled ~92 KB into every bundle
736
+ // but duplicated validation the server pipeline already performs on
737
+ // sendCreateFactoryTx. Invalid factories now surface as the chain's
738
+ // `factory validation failed: ...` GraphQL error.
720
739
  const hash = await client.sendCreateFactoryTx({ tx: { itx }, wallet }, extra);
721
740
  return [hash, itx.address];
722
741
  };
@@ -1434,5 +1453,5 @@ const createExtensionMethods = client => {
1434
1453
  };
1435
1454
  };
1436
1455
 
1437
- module.exports = { createExtensionMethods };
1456
+ module.exports = { createExtensionMethods, defaultDeps };
1438
1457
  //# sourceMappingURL=extension.js.map