@gala-chain/gala-core-sdk 1.2.0 → 1.3.0-rc.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/dist/index.cjs CHANGED
@@ -99,6 +99,28 @@ const INVALID_NFT_INPUT_REASONS = [
99
99
  "bad_collection_name"
100
100
  ];
101
101
  /**
102
+ * Why a signed DTO's expiry is refused (`INVALID_DTO_EXPIRES_AT.params.reason`). A token for the
103
+ * app to map to its own text.
104
+ *
105
+ * - `not_a_safe_integer`: not a JS number that is a safe integer (`NaN`, `Infinity`, a fraction, a
106
+ * string, a bigint, a value beyond `Number.MAX_SAFE_INTEGER`).
107
+ * - `not_positive`: `0` or below. The chain reads a falsy `dtoExpiresAt` as "no expiry", so `0`
108
+ * is never signed as one.
109
+ * - `expired`: the deadline is not after the builder's clock (a wallet builder refuses to sign a
110
+ * DTO the chain would already refuse or refuse within the same millisecond).
111
+ * - `expiry_window`: the deadline is more than the wallet builder's window ahead of its clock, or
112
+ * an `expiresInMs` outside that window.
113
+ * - `bad_shape`: a wallet builder expiry that is not exactly one of `{ dtoExpiresAt }` or
114
+ * `{ expiresInMs, nowMs }` (never guessed between them).
115
+ */
116
+ const INVALID_DTO_EXPIRES_AT_REASONS = [
117
+ "not_a_safe_integer",
118
+ "not_positive",
119
+ "expired",
120
+ "expiry_window",
121
+ "bad_shape"
122
+ ];
123
+ /**
102
124
  * The `GatewayWrites` options that are refused when missing or of the wrong type
103
125
  * (`INVALID_WRITES_OPTION.params.option`). `walletAddress`, `requestTimeoutMs` and
104
126
  * `uniqueKeyPrefix` have codes of their own (`INVALID_WALLET_ADDRESS`, `INVALID_TIMEOUT`,
@@ -574,6 +596,19 @@ const GALA_ERROR_CATALOG = {
574
596
  template: "expires must be a non-negative whole number of epoch milliseconds, 0 for none (got {value}).",
575
597
  params: { value: "string" }
576
598
  },
599
+ INVALID_DTO_EXPIRES_AT: {
600
+ phase: "pre_send",
601
+ template: "Invalid {field}: {reason}.",
602
+ params: {
603
+ field: "string",
604
+ reason: INVALID_DTO_EXPIRES_AT_REASONS
605
+ }
606
+ },
607
+ INVALID_CONTRACT_PATH: {
608
+ phase: "pre_send",
609
+ template: "Invalid {field}: it names no contract this SDK knows a dtoOperation for; nothing was signed.",
610
+ params: { field: "string" }
611
+ },
577
612
  INVALID_LOCK_NAME: {
578
613
  phase: "pre_send",
579
614
  template: "The lock name must be a string (got a {valueType}).",
@@ -937,6 +972,8 @@ const GALA_ERROR_PHASE_BY_CODE = {
937
972
  INVALID_TOKEN_REFERENCE: "pre_send",
938
973
  INVALID_TOKEN_BATCH: "pre_send",
939
974
  INVALID_LOCK_EXPIRES: "pre_send",
975
+ INVALID_DTO_EXPIRES_AT: "pre_send",
976
+ INVALID_CONTRACT_PATH: "pre_send",
940
977
  INVALID_LOCK_NAME: "pre_send",
941
978
  INVALID_NFT_INPUT: "pre_send",
942
979
  NO_TOKEN_NAME_RESOLVER: "pre_send",
@@ -1310,7 +1347,7 @@ function galaChainUserRef(value, field) {
1310
1347
  }
1311
1348
  throw new GalaSDKError("INVALID_WALLET_ADDRESS", {
1312
1349
  field,
1313
- value: String(value)
1350
+ value: "[redacted]"
1314
1351
  });
1315
1352
  }
1316
1353
 
@@ -2704,6 +2741,18 @@ const CHAIN_METHOD_OPERATIONS = {
2704
2741
  "asset/bridge-contract:GrantAllowance": "asset-channel_bridge-chaincode_GalaChainToken:GrantAllowance"
2705
2742
  };
2706
2743
  /**
2744
+ * `value` as a {@link ChainContractPath}: a string naming a contract in the operation table (an
2745
+ * own key, so `toString` or a polluted prototype never counts). Checked before anything is built
2746
+ * or signed.
2747
+ *
2748
+ * @throws GalaSDKError `INVALID_CONTRACT_PATH` (`params.field` = `field`; the value is never
2749
+ * echoed).
2750
+ */
2751
+ function checkContractPath(value, field) {
2752
+ if (typeof value === "string" && Object.prototype.hasOwnProperty.call(CHAIN_CONTRACT_OPERATION_PREFIXES, value)) return value;
2753
+ throw new GalaSDKError("INVALID_CONTRACT_PATH", { field });
2754
+ }
2755
+ /**
2707
2756
  * The `dtoOperation` of `method` on the contract served at `contractPath`: the chain's own
2708
2757
  * (`CHAIN_METHOD_OPERATIONS`) where it deviates from the platform table, else the table's prefix plus `:<method>`.
2709
2758
  *
@@ -2933,7 +2982,7 @@ async function signPreSendFor(signer, contractPath, method, dto) {
2933
2982
  * fallback. It is the version in the default `User-Agent` (`GalaChain-SDK/<version>`) of every SDK
2934
2983
  * request, whichever product SDK sends it.
2935
2984
  */
2936
- const GALA_CORE_SDK_VERSION = "1.2.0";
2985
+ const GALA_CORE_SDK_VERSION = "1.3.0-rc.0";
2937
2986
 
2938
2987
  //#endregion
2939
2988
  //#region src/utils/transport.ts
@@ -2994,7 +3043,7 @@ function buildRequestHeaders(wire, parts = {}) {
2994
3043
  entries.set(name.toLowerCase(), [name, value]);
2995
3044
  }
2996
3045
  };
2997
- if ((wire === null || wire === void 0 ? void 0 : wire.userAgent) !== false) put({ "User-Agent": (wire === null || wire === void 0 ? void 0 : wire.userAgent) ?? `GalaChain-SDK/${"1.2.0"}` });
3046
+ if ((wire === null || wire === void 0 ? void 0 : wire.userAgent) !== false) put({ "User-Agent": (wire === null || wire === void 0 ? void 0 : wire.userAgent) ?? `GalaChain-SDK/${"1.3.0-rc.0"}` });
2998
3047
  put(parts.defaults);
2999
3048
  put(wire === null || wire === void 0 ? void 0 : wire.headers);
3000
3049
  if (parts.json === true && !entries.has("content-type")) put({ "Content-Type": "application/json" });
@@ -3774,6 +3823,36 @@ function getEthereumAddressFromPrivateKey(privateKey) {
3774
3823
  }
3775
3824
  }
3776
3825
 
3826
+ //#endregion
3827
+ //#region src/writes/dto_expiry.ts
3828
+ /**
3829
+ * A signed DTO's `dtoExpiresAt`, checked: a JS number that is a safe integer of epoch
3830
+ * MILLISECONDS, greater than zero. Returned unchanged, so the signed value is the one given.
3831
+ *
3832
+ * Chain semantics (`@GalaTransaction`, before authorization): the chain refuses the DTO with
3833
+ * `ExpiredError` when `dto.dtoExpiresAt && dto.dtoExpiresAt < Date.now()` (its own clock, strict
3834
+ * less-than: a DTO is still accepted in the millisecond it expires). A falsy value is no expiry at
3835
+ * all, so `0` is refused here rather than signed as one. A DTO without the field stays valid on
3836
+ * chain: the field is optional and additive.
3837
+ *
3838
+ * This checks the field only; it applies no window against a clock (the wallet builder in
3839
+ * `@gala-games/wallet-core` adds the 60 s quote window and refuses an expired deadline).
3840
+ *
3841
+ * @throws GalaSDKError `INVALID_DTO_EXPIRES_AT` (`reason` `not_a_safe_integer` or `not_positive`,
3842
+ * `field` = `field`).
3843
+ */
3844
+ function checkDtoExpiresAt(value, field = "dtoExpiresAt") {
3845
+ if (typeof value !== "number" || !Number.isSafeInteger(value)) throw new GalaSDKError("INVALID_DTO_EXPIRES_AT", {
3846
+ field,
3847
+ reason: "not_a_safe_integer"
3848
+ });
3849
+ if (value <= 0) throw new GalaSDKError("INVALID_DTO_EXPIRES_AT", {
3850
+ field,
3851
+ reason: "not_positive"
3852
+ });
3853
+ return value;
3854
+ }
3855
+
3777
3856
  //#endregion
3778
3857
  //#region src/writes/lock.ts
3779
3858
  /**
@@ -4401,20 +4480,33 @@ async function quoteNftFee(transport, uniqueKeyPrefix, callerPublicKey, method,
4401
4480
  //#region src/writes/token_ref.ts
4402
4481
  /** A plain decimal: digits, optionally a fraction. No sign, exponent, hex, spaces or separators. */
4403
4482
  const DECIMAL = /^[0-9]+(?:\.[0-9]+)?$/u;
4404
- const INSTANCE = /^[0-9]+$/u;
4405
4483
  /**
4406
- * The quantity of a write, exactly as the caller wrote it: whole tokens as a plain decimal string
4407
- * greater than zero. It is returned unchanged (never scaled to base units, never re-formatted), so
4408
- * the signed amount is the amount the caller passed. A JS number is refused: it may already have
4409
- * lost digits (`0.1 + 0.2`), and the chain receives strings.
4484
+ * A token instance in the one spelling the chain's `TokenInstanceKey` (`@BigNumberProperty`)
4485
+ * accepts: a whole number with no leading zeros (`0`, `7`; never `007` or `00`). Not rewritten:
4486
+ * an instance names one token, so a spelling the chain would refuse is refused here.
4487
+ */
4488
+ const INSTANCE = /^(?:0|[1-9][0-9]*)$/u;
4489
+ /** What the quantity error reports instead of the supplied value: errors never echo input. */
4490
+ const REDACTED = "[redacted]";
4491
+ /**
4492
+ * The quantity of a write: whole tokens as a plain decimal string greater than zero (never scaled
4493
+ * to base units), returned in the chain's canonical spelling, `new BigNumber(value).toFixed()`.
4494
+ * The chain's DTO classes (`@BigNumberProperty` + `IsBigNumber`) refuse every other spelling of
4495
+ * the same number, so `"1.50"` is signed as `"1.5"`, `"5.0"` as `"5"` and `"01"` as `"1"`: the
4496
+ * amount the caller passed, in the bytes the chain can execute. A JS number is refused: it may
4497
+ * already have lost digits (`0.1 + 0.2`), and the chain receives strings.
4410
4498
  *
4411
- * @throws GalaSDKError `INVALID_QUANTITY`.
4499
+ * @throws GalaSDKError `INVALID_QUANTITY` (`params.value` is `[redacted]`: the input is never
4500
+ * echoed).
4412
4501
  */
4413
4502
  function writeQuantity(value, field) {
4414
- if (typeof value === "string" && DECIMAL.test(value) && new bignumber_js.default(value).isGreaterThan(0)) return value;
4503
+ if (typeof value === "string" && DECIMAL.test(value)) {
4504
+ const amount = new bignumber_js.default(value);
4505
+ if (amount.isGreaterThan(0)) return amount.toFixed();
4506
+ }
4415
4507
  throw new GalaSDKError("INVALID_QUANTITY", {
4416
4508
  field,
4417
- value: String(value)
4509
+ value: REDACTED
4418
4510
  });
4419
4511
  }
4420
4512
  /**
@@ -4538,16 +4630,19 @@ const GALA_TOKEN_CLASS_KEY = Object.freeze({
4538
4630
  });
4539
4631
  /**
4540
4632
  * The unsigned `TransferToken` DTO: plain JSON, exactly the fields the chain signs, in the order
4541
- * the gateway has always received them (`from, to, quantity, tokenInstance, uniqueKey`). No signer
4542
- * field is ever added: the chain recovers the signer from the signature.
4633
+ * the gateway has always received them (`from, to, quantity, tokenInstance, uniqueKey`), then
4634
+ * `dtoExpiresAt` only when one is given (absent: the DTO, its bytes and its signature are exactly
4635
+ * what they were before the field existed). No signer field is ever added: the chain recovers the
4636
+ * signer from the signature.
4543
4637
  */
4544
- function transferTokenDto(from, to, token, uniqueKey) {
4638
+ function transferTokenDto(from, to, token, uniqueKey, dtoExpiresAt) {
4545
4639
  return {
4546
4640
  from,
4547
4641
  to,
4548
4642
  quantity: token.quantity,
4549
4643
  tokenInstance: token.key,
4550
- uniqueKey
4644
+ uniqueKey,
4645
+ ...dtoExpiresAt === void 0 ? {} : { dtoExpiresAt }
4551
4646
  };
4552
4647
  }
4553
4648
  /** The unsigned `BurnTokens` DTO (`tokenInstances, uniqueKey`); burns are of the signer's own tokens. */
@@ -4588,7 +4683,7 @@ const DEFAULT_REQUEST_TIMEOUT_MS$1 = 3e4;
4588
4683
  * the quantity, the `uniqueKey`, the batch), so a refused input costs no signature and no request.
4589
4684
  * The DTO is plain JSON with exactly the chain's fields; the signer signs it as it is (nothing,
4590
4685
  * least of all a signer hint, is added) and the body sent is what the signer returned. Quantities
4591
- * are whole tokens and are signed exactly as written.
4686
+ * are whole tokens, signed in the chain's canonical spelling (`writeQuantity`: `"1.50"` as `"1.5"`).
4592
4687
  *
4593
4688
  * @example
4594
4689
  * ```ts
@@ -4631,9 +4726,12 @@ var GatewayWrites = class {
4631
4726
  /**
4632
4727
  * Transfer GALA (`GALA|Unit|none|none`, instance `"0"`) to `to`.
4633
4728
  *
4729
+ * `input.dtoExpiresAt` (optional, epoch ms) is signed into the DTO when given; see
4730
+ * `DtoExpiresAtInput.dtoExpiresAt`.
4731
+ *
4634
4732
  * @throws GalaSDKError pre-send: `INVALID_WALLET_ADDRESS`, `INVALID_QUANTITY`,
4635
- * `INVALID_UNIQUE_KEY`, `SIGNING_FAILED`; in flight: `GATEWAY_WRITE_OUTCOME_UNKNOWN` (check
4636
- * `error.uniqueKey`; never resend under a fresh key);
4733
+ * `INVALID_DTO_EXPIRES_AT`, `INVALID_UNIQUE_KEY`, `SIGNING_FAILED`; in flight:
4734
+ * `GATEWAY_WRITE_OUTCOME_UNKNOWN` (check `error.uniqueKey`; never resend under a fresh key);
4637
4735
  * post-send: `GATEWAY_WRITE_REJECTED`, `GATEWAY_HTTP_ERROR`, `GATEWAY_RESPONSE_INVALID`.
4638
4736
  */
4639
4737
  transferGala(input) {
@@ -4641,7 +4739,8 @@ var GatewayWrites = class {
4641
4739
  to: input.to,
4642
4740
  quantity: input.quantity,
4643
4741
  tokenClassKey: GALA_TOKEN_CLASS_KEY,
4644
- uniqueKey: input.uniqueKey
4742
+ uniqueKey: input.uniqueKey,
4743
+ dtoExpiresAt: input.dtoExpiresAt
4645
4744
  });
4646
4745
  }
4647
4746
  /**
@@ -4653,9 +4752,10 @@ var GatewayWrites = class {
4653
4752
  async transferToken(input) {
4654
4753
  const to = galaChainUserRef(input.to, "to");
4655
4754
  const checked = checkQuantity(input, "");
4755
+ const dtoExpiresAt = input.dtoExpiresAt === void 0 ? void 0 : checkDtoExpiresAt(input.dtoExpiresAt);
4656
4756
  const uniqueKey = this.uniqueKeyOf(input.uniqueKey);
4657
4757
  const token = await this.resolveOne(checked);
4658
- return this.submit("TransferToken", transferTokenDto(this.walletAddress, to, token, uniqueKey), uniqueKey);
4758
+ return this.submit("TransferToken", transferTokenDto(this.walletAddress, to, token, uniqueKey, dtoExpiresAt), uniqueKey);
4659
4759
  }
4660
4760
  /**
4661
4761
  * Burn 1 to 50 tokens of the writer's. IRREVERSIBLE: burned tokens are gone.
@@ -5574,6 +5674,7 @@ exports.GalaSDKError = GalaSDKError;
5574
5674
  exports.GalaWalletSigner = GalaWalletSigner;
5575
5675
  exports.GatewayReads = GatewayReads;
5576
5676
  exports.GatewayWrites = GatewayWrites;
5677
+ exports.INVALID_DTO_EXPIRES_AT_REASONS = INVALID_DTO_EXPIRES_AT_REASONS;
5577
5678
  exports.INVALID_NFT_INPUT_REASONS = INVALID_NFT_INPUT_REASONS;
5578
5679
  exports.INVALID_PRIVATE_KEY_REASONS = INVALID_PRIVATE_KEY_REASONS;
5579
5680
  exports.INVALID_TOKEN_REFERENCE_REASONS = INVALID_TOKEN_REFERENCE_REASONS;
@@ -5597,6 +5698,8 @@ exports.buildQueryString = buildQueryString;
5597
5698
  exports.buildRequestHeaders = buildRequestHeaders;
5598
5699
  exports.calculatePersonalSignPrefix = calculatePersonalSignPrefix;
5599
5700
  exports.canonicalDecimalString = canonicalDecimalString;
5701
+ exports.checkContractPath = checkContractPath;
5702
+ exports.checkDtoExpiresAt = checkDtoExpiresAt;
5600
5703
  exports.checksumEthereumAddress = checksumEthereumAddress;
5601
5704
  exports.compositeKeyOf = compositeKeyOf;
5602
5705
  exports.createLazyPrivateKeySigner = createLazyPrivateKeySigner;
@@ -5667,4 +5770,5 @@ exports.walletDeclineCode = walletDeclineCode;
5667
5770
  exports.withDtoOperation = withDtoOperation;
5668
5771
  exports.withGalaWalletIdentity = withGalaWalletIdentity;
5669
5772
  exports.withPrivateKey = withPrivateKey;
5773
+ exports.writeQuantity = writeQuantity;
5670
5774
  //# sourceMappingURL=index.cjs.map