@defuse-protocol/intents-sdk 0.33.0 → 0.33.1

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/README.md CHANGED
@@ -422,6 +422,33 @@ const payload = await sdk.intentBuilder()
422
422
  .build();
423
423
  ```
424
424
 
425
+ ### Versioned Nonce Builder
426
+
427
+ By default, the nonce is generated during the intent construction process, but it is possible to generate and pass the nonce within the builder independently. All of the nonces have specific [requirements](https://github.com/near/intents/tree/main/defuse#nonces) for the structure reflecting their validity.
428
+
429
+ ```typescript
430
+ import {VersionedNonceBuilder} from '@defuse-protocol/intents-sdk';
431
+
432
+ // Fetch current salt from the verifier contract
433
+ const salt_hex = await nearRPC.viewFunction({
434
+ contractId: "intents.near",
435
+ methodName: "current_salt",
436
+ });
437
+
438
+ // Example: 5-minute deadline, but actual deadline should not be greater than intent's deadline
439
+ const deadline = new Date(Date.now() + 5 * 60 * 1000)
440
+
441
+ // Create ready to use versioned nonce from salt and deadline
442
+ const versionedNonce = VersionedNonceBuilder.encodeNonce(
443
+ Uint8Array.from(Buffer.from(salt_hex, "hex")),
444
+ deadline
445
+ );
446
+
447
+ // Create intent builder with specified nonce
448
+ const builder = await sdk.intentBuilder().setNonce(versionedNonce);
449
+ ```
450
+
451
+
425
452
  ### Intent Publishing Hooks
426
453
 
427
454
  Use the `onBeforePublishIntent` hook to intercept and process intent data before it's published to the relayer. This is
@@ -135,7 +135,8 @@ var IntentPayloadBuilder = class IntentPayloadBuilder {
135
135
  */
136
136
  buildWithSalt(salt) {
137
137
  const deadline = this.deadline ?? new Date(Date.now() + require_intent_payload_factory.DEFAULT_DEADLINE_MS);
138
- const nonce = this.customNonce ?? require_expirable_nonce.VersionedNonceBuilder.encodeNonce(salt, deadline);
138
+ const nonceDeadline = new Date(deadline.getTime() + require_intent_payload_factory.DEFAULT_NONCE_DEADLINE_OFFSET_MS);
139
+ const nonce = this.customNonce ?? require_expirable_nonce.VersionedNonceBuilder.encodeNonce(salt, nonceDeadline);
139
140
  return {
140
141
  verifying_contract: this.verifyingContract,
141
142
  signer_id: this.signerId,
@@ -1,5 +1,5 @@
1
1
  import { VersionedNonceBuilder } from "./expirable-nonce.js";
2
- import { DEFAULT_DEADLINE_MS } from "./intent-payload-factory.js";
2
+ import { DEFAULT_DEADLINE_MS, DEFAULT_NONCE_DEADLINE_OFFSET_MS } from "./intent-payload-factory.js";
3
3
  import { configsByEnvironment, utils } from "@defuse-protocol/internal-utils";
4
4
 
5
5
  //#region src/intents/intent-payload-builder.ts
@@ -133,7 +133,8 @@ var IntentPayloadBuilder = class IntentPayloadBuilder {
133
133
  */
134
134
  buildWithSalt(salt) {
135
135
  const deadline = this.deadline ?? new Date(Date.now() + DEFAULT_DEADLINE_MS);
136
- const nonce = this.customNonce ?? VersionedNonceBuilder.encodeNonce(salt, deadline);
136
+ const nonceDeadline = new Date(deadline.getTime() + DEFAULT_NONCE_DEADLINE_OFFSET_MS);
137
+ const nonce = this.customNonce ?? VersionedNonceBuilder.encodeNonce(salt, nonceDeadline);
137
138
  return {
138
139
  verifying_contract: this.verifyingContract,
139
140
  signer_id: this.signerId,
@@ -2,13 +2,15 @@ const require_expirable_nonce = require('./expirable-nonce.cjs');
2
2
 
3
3
  //#region src/intents/intent-payload-factory.ts
4
4
  const DEFAULT_DEADLINE_MS = 60 * 1e3;
5
+ const DEFAULT_NONCE_DEADLINE_OFFSET_MS = 30 * 1e3;
5
6
  function defaultIntentPayloadFactory(salt, { intents, verifying_contract,...params }) {
6
7
  params = Object.fromEntries(Object.entries(params).filter(([, value]) => value !== void 0));
7
8
  const deadline = params.deadline != null ? new Date(params.deadline) : new Date(Date.now() + DEFAULT_DEADLINE_MS);
9
+ const nonceDeadline = new Date(deadline.getTime() + DEFAULT_NONCE_DEADLINE_OFFSET_MS);
8
10
  return {
9
11
  verifying_contract,
10
12
  deadline: deadline.toISOString(),
11
- nonce: require_expirable_nonce.VersionedNonceBuilder.encodeNonce(salt, deadline),
13
+ nonce: require_expirable_nonce.VersionedNonceBuilder.encodeNonce(salt, nonceDeadline),
12
14
  intents: intents == null ? [] : intents,
13
15
  signer_id: void 0,
14
16
  ...params
@@ -17,4 +19,5 @@ function defaultIntentPayloadFactory(salt, { intents, verifying_contract,...para
17
19
 
18
20
  //#endregion
19
21
  exports.DEFAULT_DEADLINE_MS = DEFAULT_DEADLINE_MS;
22
+ exports.DEFAULT_NONCE_DEADLINE_OFFSET_MS = DEFAULT_NONCE_DEADLINE_OFFSET_MS;
20
23
  exports.defaultIntentPayloadFactory = defaultIntentPayloadFactory;
@@ -2,13 +2,15 @@ import { VersionedNonceBuilder } from "./expirable-nonce.js";
2
2
 
3
3
  //#region src/intents/intent-payload-factory.ts
4
4
  const DEFAULT_DEADLINE_MS = 60 * 1e3;
5
+ const DEFAULT_NONCE_DEADLINE_OFFSET_MS = 30 * 1e3;
5
6
  function defaultIntentPayloadFactory(salt, { intents, verifying_contract,...params }) {
6
7
  params = Object.fromEntries(Object.entries(params).filter(([, value]) => value !== void 0));
7
8
  const deadline = params.deadline != null ? new Date(params.deadline) : new Date(Date.now() + DEFAULT_DEADLINE_MS);
9
+ const nonceDeadline = new Date(deadline.getTime() + DEFAULT_NONCE_DEADLINE_OFFSET_MS);
8
10
  return {
9
11
  verifying_contract,
10
12
  deadline: deadline.toISOString(),
11
- nonce: VersionedNonceBuilder.encodeNonce(salt, deadline),
13
+ nonce: VersionedNonceBuilder.encodeNonce(salt, nonceDeadline),
12
14
  intents: intents == null ? [] : intents,
13
15
  signer_id: void 0,
14
16
  ...params
@@ -16,4 +18,4 @@ function defaultIntentPayloadFactory(salt, { intents, verifying_contract,...para
16
18
  }
17
19
 
18
20
  //#endregion
19
- export { DEFAULT_DEADLINE_MS, defaultIntentPayloadFactory };
21
+ export { DEFAULT_DEADLINE_MS, DEFAULT_NONCE_DEADLINE_OFFSET_MS, defaultIntentPayloadFactory };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defuse-protocol/intents-sdk",
3
- "version": "0.33.0",
3
+ "version": "0.33.1",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "sideEffects": false,