@ocap/tx-protocols 1.30.2 → 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.
@@ -31,7 +31,7 @@ runner.use(pipes.VerifyInfo([{
31
31
  itx.address = "";
32
32
  context.assetData = decodedData;
33
33
  context.signers = [itx.issuer].filter(Boolean);
34
- return toAssetAddress(itx) === context.itx.address;
34
+ return toAssetAddress(itx, context.txEncoding) === context.itx.address;
35
35
  }
36
36
  }]));
37
37
  runner.use(pipes.VerifyMultiSigV2({ signersKey: "signers" }));
@@ -30,7 +30,8 @@ const verify_itx_address = (mode) => (context, next) => {
30
30
  factory,
31
31
  inputs,
32
32
  owner: owner.address,
33
- issuer
33
+ issuer,
34
+ encoding: context.txEncoding
34
35
  });
35
36
  if (minted.address !== itx.address) {
36
37
  debug("asset address mismatch", {
@@ -28,7 +28,7 @@ runner.use((context, next) => {
28
28
  if (factoryProps.name.length < 2 || factoryProps.name.length > 255) return next(new CustomError("INVALID_FACTORY_PROPS", "Length of factory name should between 2 and 255 characters"));
29
29
  const props = cloneDeep(factoryProps);
30
30
  props.address = "";
31
- if (toFactoryAddress(props) !== factoryProps.address) return next(new CustomError("INVALID_FACTORY_PROPS", "Factory address is not valid"));
31
+ if (toFactoryAddress(props, context.txEncoding) !== factoryProps.address) return next(new CustomError("INVALID_FACTORY_PROPS", "Factory address is not valid"));
32
32
  const factoryTokenAddresses = (factoryProps.input.tokens || []).map((x) => x.address);
33
33
  const maxListSize = get(context, "config.transaction.maxListSize");
34
34
  if (factoryTokenAddresses.length > maxListSize) return next(new CustomError("INVALID_FACTORY_INPUT", `input.tokens exceeded max allowed length: ${maxListSize}`));
@@ -59,7 +59,7 @@ runner.use(pipes.VerifyInfo([
59
59
  itx.address = "";
60
60
  context.rollupData = itx.data;
61
61
  context.seedValidators = getListField(itx, "seedValidators").map((x) => x.address);
62
- return toRollupAddress(itx) === context.itx.address;
62
+ return toRollupAddress(itx, context.txEncoding) === context.itx.address;
63
63
  }
64
64
  }
65
65
  ]));
@@ -7,10 +7,10 @@ function CreateVerifySignersPipe({ signersKey, allowSender = true, allowShrink =
7
7
  const { tx, rollupState } = context;
8
8
  const rollup = rollupState;
9
9
  const { minSignerCount, maxSignerCount } = rollup;
10
- const signatures = get(context, signersKey);
10
+ const signatures = get(context, signersKey) ?? [];
11
11
  const signerCount = signatures.length;
12
- const seedValidators = rollup.seedValidators.map((x) => x.address);
13
- const validators = rollup.validators.map((x) => x.address);
12
+ const seedValidators = (rollup.seedValidators ?? []).map((x) => x.address);
13
+ const validators = (rollup.validators ?? []).map((x) => x.address);
14
14
  if (!allowSender && signatures.some((x) => x.signer === tx.from)) return next(new CustomError("INVALID_SIGNATURE", `Invalid: ${signersKey}, sender not allowed to sign`));
15
15
  if (validators.length > minSignerCount) {
16
16
  if (signerCount < minSignerCount) return next(new CustomError("INVALID_SIGNATURE", `Invalid: ${signersKey}, expect at least ${minSignerCount} signatures, got ${signerCount}`));
@@ -55,7 +55,7 @@ runner.use(pipes.VerifyInfo([{
55
55
  const itx = cloneDeep(context.itx);
56
56
  itx.data = decodeAnySafe(itx.data ?? null);
57
57
  itx.address = "";
58
- return toTokenAddress(itx) === context.itx.address;
58
+ return toTokenAddress(itx, context.txEncoding) === context.itx.address;
59
59
  }
60
60
  }]));
61
61
  runner.use(pipes.ExtractState({
@@ -88,11 +88,12 @@ runner.use(pipes.VerifyInfo([
88
88
  error: "INVALID_TOKEN",
89
89
  message: "Token address is not valid",
90
90
  fn: (ctx) => {
91
- const itxToken = ctx.itx.token;
91
+ const context = ctx;
92
+ const itxToken = context.itx.token;
92
93
  return toTokenAddress({
93
94
  ...itxToken,
94
95
  address: ""
95
- }) === itxToken.address;
96
+ }, context.txEncoding) === itxToken.address;
96
97
  }
97
98
  },
98
99
  {
@@ -103,7 +104,7 @@ runner.use(pipes.VerifyInfo([
103
104
  const itx = cloneDeep(context.itx);
104
105
  itx.data = decodeAnySafe(itx.data ?? null);
105
106
  itx.address = "";
106
- return toTokenFactoryAddress(itx) === context.itx.address;
107
+ return toTokenFactoryAddress(itx, context.txEncoding) === context.itx.address;
107
108
  }
108
109
  },
109
110
  {
@@ -35,7 +35,7 @@ runner.use(_ocap_tx_pipeline.pipes.VerifyInfo([{
35
35
  itx.address = "";
36
36
  context.assetData = decodedData;
37
37
  context.signers = [itx.issuer].filter(Boolean);
38
- return (0, _arcblock_did_util.toAssetAddress)(itx) === context.itx.address;
38
+ return (0, _arcblock_did_util.toAssetAddress)(itx, context.txEncoding) === context.itx.address;
39
39
  }
40
40
  }]));
41
41
  runner.use(_ocap_tx_pipeline.pipes.VerifyMultiSigV2({ signersKey: "signers" }));
@@ -33,7 +33,8 @@ const verify_itx_address = (mode) => (context, next) => {
33
33
  factory,
34
34
  inputs,
35
35
  owner: owner.address,
36
- issuer
36
+ issuer,
37
+ encoding: context.txEncoding
37
38
  });
38
39
  if (minted.address !== itx.address) {
39
40
  debug$1("asset address mismatch", {
@@ -34,7 +34,7 @@ runner.use((context, next) => {
34
34
  if (factoryProps.name.length < 2 || factoryProps.name.length > 255) return next(new _ocap_util_lib_error.CustomError("INVALID_FACTORY_PROPS", "Length of factory name should between 2 and 255 characters"));
35
35
  const props = (0, lodash_cloneDeep.default)(factoryProps);
36
36
  props.address = "";
37
- if ((0, _arcblock_did_util.toFactoryAddress)(props) !== factoryProps.address) return next(new _ocap_util_lib_error.CustomError("INVALID_FACTORY_PROPS", "Factory address is not valid"));
37
+ if ((0, _arcblock_did_util.toFactoryAddress)(props, context.txEncoding) !== factoryProps.address) return next(new _ocap_util_lib_error.CustomError("INVALID_FACTORY_PROPS", "Factory address is not valid"));
38
38
  const factoryTokenAddresses = (factoryProps.input.tokens || []).map((x) => x.address);
39
39
  const maxListSize = (0, lodash_get.default)(context, "config.transaction.maxListSize");
40
40
  if (factoryTokenAddresses.length > maxListSize) return next(new _ocap_util_lib_error.CustomError("INVALID_FACTORY_INPUT", `input.tokens exceeded max allowed length: ${maxListSize}`));
@@ -64,7 +64,7 @@ runner.use(_ocap_tx_pipeline.pipes.VerifyInfo([
64
64
  itx.address = "";
65
65
  context.rollupData = itx.data;
66
66
  context.seedValidators = (0, _ocap_util_lib_get_list_field.getListField)(itx, "seedValidators").map((x) => x.address);
67
- return (0, _arcblock_did_util.toRollupAddress)(itx) === context.itx.address;
67
+ return (0, _arcblock_did_util.toRollupAddress)(itx, context.txEncoding) === context.itx.address;
68
68
  }
69
69
  }
70
70
  ]));
@@ -10,10 +10,10 @@ function CreateVerifySignersPipe({ signersKey, allowSender = true, allowShrink =
10
10
  const { tx, rollupState } = context;
11
11
  const rollup = rollupState;
12
12
  const { minSignerCount, maxSignerCount } = rollup;
13
- const signatures = (0, lodash_get.default)(context, signersKey);
13
+ const signatures = (0, lodash_get.default)(context, signersKey) ?? [];
14
14
  const signerCount = signatures.length;
15
- const seedValidators = rollup.seedValidators.map((x) => x.address);
16
- const validators = rollup.validators.map((x) => x.address);
15
+ const seedValidators = (rollup.seedValidators ?? []).map((x) => x.address);
16
+ const validators = (rollup.validators ?? []).map((x) => x.address);
17
17
  if (!allowSender && signatures.some((x) => x.signer === tx.from)) return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", `Invalid: ${signersKey}, sender not allowed to sign`));
18
18
  if (validators.length > minSignerCount) {
19
19
  if (signerCount < minSignerCount) return next(new _ocap_util_lib_error.CustomError("INVALID_SIGNATURE", `Invalid: ${signersKey}, expect at least ${minSignerCount} signatures, got ${signerCount}`));
@@ -61,7 +61,7 @@ runner.use(_ocap_tx_pipeline.pipes.VerifyInfo([{
61
61
  const itx = (0, lodash_cloneDeep.default)(context.itx);
62
62
  itx.data = require_util.decodeAnySafe(itx.data ?? null);
63
63
  itx.address = "";
64
- return (0, _arcblock_did_util.toTokenAddress)(itx) === context.itx.address;
64
+ return (0, _arcblock_did_util.toTokenAddress)(itx, context.txEncoding) === context.itx.address;
65
65
  }
66
66
  }]));
67
67
  runner.use(_ocap_tx_pipeline.pipes.ExtractState({
@@ -94,11 +94,12 @@ runner.use(_ocap_tx_pipeline.pipes.VerifyInfo([
94
94
  error: "INVALID_TOKEN",
95
95
  message: "Token address is not valid",
96
96
  fn: (ctx) => {
97
- const itxToken = ctx.itx.token;
97
+ const context = ctx;
98
+ const itxToken = context.itx.token;
98
99
  return (0, _arcblock_did_util.toTokenAddress)({
99
100
  ...itxToken,
100
101
  address: ""
101
- }) === itxToken.address;
102
+ }, context.txEncoding) === itxToken.address;
102
103
  }
103
104
  },
104
105
  {
@@ -109,7 +110,7 @@ runner.use(_ocap_tx_pipeline.pipes.VerifyInfo([
109
110
  const itx = (0, lodash_cloneDeep.default)(context.itx);
110
111
  itx.data = require_util.decodeAnySafe(itx.data ?? null);
111
112
  itx.address = "";
112
- return (0, _arcblock_did_util.toTokenFactoryAddress)(itx) === context.itx.address;
113
+ return (0, _arcblock_did_util.toTokenFactoryAddress)(itx, context.txEncoding) === context.itx.address;
113
114
  }
114
115
  },
115
116
  {
package/package.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "publishConfig": {
4
4
  "access": "public"
5
5
  },
6
- "version": "1.30.2",
6
+ "version": "1.30.4",
7
7
  "description": "Predefined tx pipeline sets to execute certain type of transactions",
8
8
  "type": "module",
9
9
  "main": "./lib/index.cjs",
@@ -46,22 +46,22 @@
46
46
  "author": "wangshijun <wangshijun2010@gmail.com> (http://github.com/wangshijun)",
47
47
  "license": "MIT",
48
48
  "dependencies": {
49
- "@arcblock/did": "1.30.2",
50
- "@arcblock/did-util": "1.30.2",
51
- "@arcblock/jwt": "1.30.2",
52
- "@arcblock/validator": "1.30.2",
53
- "@arcblock/vc": "1.30.2",
49
+ "@arcblock/did": "1.30.4",
50
+ "@arcblock/did-util": "1.30.4",
51
+ "@arcblock/jwt": "1.30.4",
52
+ "@arcblock/validator": "1.30.4",
53
+ "@arcblock/vc": "1.30.4",
54
54
  "@blocklet/xss": "^0.3.7",
55
- "@ocap/asset": "1.30.2",
56
- "@ocap/client": "1.30.2",
57
- "@ocap/mcrypto": "1.30.2",
58
- "@ocap/merkle-tree": "1.30.2",
59
- "@ocap/message": "1.30.2",
60
- "@ocap/state": "1.30.2",
61
- "@ocap/tx-pipeline": "1.30.2",
62
- "@ocap/types": "1.30.2",
63
- "@ocap/util": "1.30.2",
64
- "@ocap/wallet": "1.30.2",
55
+ "@ocap/asset": "1.30.4",
56
+ "@ocap/client": "1.30.4",
57
+ "@ocap/mcrypto": "1.30.4",
58
+ "@ocap/merkle-tree": "1.30.4",
59
+ "@ocap/message": "1.30.4",
60
+ "@ocap/state": "1.30.4",
61
+ "@ocap/tx-pipeline": "1.30.4",
62
+ "@ocap/types": "1.30.4",
63
+ "@ocap/util": "1.30.4",
64
+ "@ocap/wallet": "1.30.4",
65
65
  "debug": "^4.4.3",
66
66
  "deep-diff": "^1.0.2",
67
67
  "lodash": "^4.17.23",
package/tools/fixtures.ts CHANGED
@@ -5,8 +5,10 @@ import crypto from 'node:crypto';
5
5
  import cloneDeep from 'lodash/cloneDeep';
6
6
  import { types, getRandomBytes, toTxHash } from '@ocap/mcrypto';
7
7
  import { fromRandom } from '@ocap/wallet';
8
- import { createMessage, fromTypeUrl } from '@ocap/message';
9
- import { toBase64, toBN, fromUnitToToken, fromTokenToUnit, toBase58 } from '@ocap/util';
8
+ import { fromTypeUrl } from '@ocap/message';
9
+ import { encodeTx as encodeTxCbor } from '@ocap/message/cbor';
10
+ import { encodeTx as encodeTxProto } from '@ocap/message/protobuf';
11
+ import { toBase64, toBN, fromUnitToToken, fromTokenToUnit, toBase58, toHex } from '@ocap/util';
10
12
  import * as states from '@ocap/state';
11
13
  import * as JWT from '@arcblock/jwt';
12
14
  import { toStakeAddress, toTokenAddress, toTokenFactoryAddress } from '@arcblock/did-util';
@@ -63,14 +65,41 @@ export const client: any = new Proxy({}, {
63
65
 
64
66
  export const getTxHash = () => getRandomBytes(32);
65
67
 
66
- export const getTxBase64 = (tx: any) => {
67
- const txObj = createMessage('Transaction', tx);
68
- if (!txObj) {
69
- throw new Error('Failed to create Transaction message');
70
- }
71
- const txBytes = txObj.serializeBinary();
72
- const txStr = toBase64(txBytes);
73
- return txStr;
68
+ export type TxEncoding = 'cbor' | 'protobuf';
69
+
70
+ export const encodeTxBytes = (tx: any, encoding: TxEncoding): Uint8Array =>
71
+ encoding === 'cbor' ? encodeTxCbor(tx) : encodeTxProto(tx);
72
+
73
+ // Phase 3.5 parameterised fixture: Phase 2 rewired `@ocap/client` to sign
74
+ // canonical-CBOR bytes, so the test-suite default is `'cbor'` (matches the
75
+ // signature produced by `client.sign*Tx`). Pass `'protobuf'` only when the
76
+ // caller has explicitly re-signed the tx via `signTxForEncoding` (below);
77
+ // otherwise chain-side `verify-signature` rejects the tx because the bytes
78
+ // under signature differ. README §5.1 shows the default as `'protobuf'` for
79
+ // historical back-compat, but that pre-dates the Phase 2 client migration —
80
+ // keeping `'cbor'` is the only default that keeps 62 existing call sites
81
+ // signature-valid without a re-sign pass.
82
+ export const getTxBase64 = (tx: any, encoding: TxEncoding = 'cbor') =>
83
+ toBase64(encodeTxBytes(tx, encoding));
84
+
85
+ // Re-sign a tx object for a target encoding. Strips any existing signature,
86
+ // encodes with the chosen format, signs the raw bytes via the wallet, and
87
+ // returns a *new* object with the updated signature. Used by the encoding
88
+ // matrix + e2e protobuf paths to produce a tx whose signature matches the
89
+ // bytes the chain will verify under `context.txEncoding`.
90
+ export const signTxForEncoding = async (tx: any, wallet: any, encoding: TxEncoding) => {
91
+ // Mirror the chain's verify-signature pipe (core/tx-pipeline/src/pipes/
92
+ // verify-signature.ts §21-26): set signature = undefined so the CBOR
93
+ // encoder drops the key (Phase 1 §3.2 rule 3), and DELETE the signatures
94
+ // list entirely (chain does deleteListField for non-multiSignV2 txs). An
95
+ // empty-array `signatures: []` would encode as CBOR 0x80, desyncing from
96
+ // the chain's bytes-under-signature.
97
+ const stripped: any = { ...tx, signature: undefined };
98
+ delete stripped.signatures;
99
+ delete stripped.signaturesList;
100
+ const bytes = encodeTxBytes(stripped, encoding);
101
+ const signature = await wallet.sign(toHex(bytes));
102
+ return { ...stripped, signature };
74
103
  };
75
104
 
76
105
  const context = { txHash: getTxHash(), txTime: new Date().toISOString() };
@@ -445,8 +474,8 @@ export const prepareTokenFactory = (props: any = {}) => {
445
474
  ...rest,
446
475
  };
447
476
 
448
- itx.token.address = token?.address || toTokenAddress(itx.token);
449
- itx.address = rest?.address || toTokenFactoryAddress(itx);
477
+ itx.token.address = token?.address || toTokenAddress(itx.token, 'cbor');
478
+ itx.address = rest?.address || toTokenFactoryAddress(itx, 'cbor');
450
479
 
451
480
  return itx;
452
481
  };
@@ -499,19 +528,22 @@ export const mockWebsiteVC = (subject: any, issuer: any, options: any = {}) => {
499
528
  };
500
529
 
501
530
  export const executeTx = (tx: any, withTxHash = false, _statedb: any = {}, options: any = {}) => {
502
- const { runAsLambda, withGasPayer, config: _config, ...extraCtx } = options;
531
+ const { runAsLambda, withGasPayer, config: _config, encoding, ...extraCtx } = options;
532
+ const resolvedEncoding: TxEncoding = encoding || 'cbor';
503
533
 
504
534
  return new Promise(async (resolve) => {
505
535
  const ctx: any = {
506
- txBase64: getTxBase64(tx),
536
+ txBase64: getTxBase64(tx, resolvedEncoding),
507
537
  config: _config || config,
508
538
  statedb: { ...statedb, ..._statedb },
509
539
  ...extraCtx,
510
540
  };
511
541
 
512
542
  if (withGasPayer) {
513
- const typeUrl = fromTypeUrl(tx.itx.typeUrl);
514
- const { buffer: txBuffer } = await getClient()[`encode${typeUrl}`]({ tx });
543
+ // Phase 2 changed client signer to return {type, value} itx shape instead of jspb {typeUrl, value}.
544
+ // Accept both so the gas-payer path keeps working across the transition.
545
+ const typeName = tx.itx.typeUrl ? fromTypeUrl(tx.itx.typeUrl) : tx.itx.type;
546
+ const { buffer: txBuffer } = await getClient()[`encode${typeName}`]({ tx });
515
547
  ctx.extra = {
516
548
  ...(ctx.extra || {}),
517
549
  ...(await getGasPayerHeader(txBuffer)),