payid 0.6.0 → 1.0.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.
Files changed (38) hide show
  1. package/dist/{chunk-BLIRABV7.js → chunk-AUW7WDAB.js} +11 -20
  2. package/dist/{chunk-SSO66YQI.js → chunk-E6VQETBC.js} +13 -0
  3. package/dist/{chunk-TQJUKEFO.js → chunk-ESTGPUEQ.js} +24 -21
  4. package/dist/{chunk-2VO4XLTT.js → chunk-EZ3BGZ7G.js} +19 -14
  5. package/dist/chunk-FZNMDGVK.js +24 -0
  6. package/dist/chunk-HKHRYRD6.js +752 -0
  7. package/dist/chunk-X7NYQ47Y.js +27 -0
  8. package/dist/{chunk-Q4UZCGU4.js → chunk-XMUHMJRD.js} +1 -1
  9. package/dist/context/index.d.ts +3 -2
  10. package/dist/context.v1-C1m-tz0o.d.ts +39 -0
  11. package/dist/context.v2-DIzPotmW.d.ts +37 -0
  12. package/dist/core/client/index.d.ts +5 -4
  13. package/dist/core/client/index.js +9 -5
  14. package/dist/core/server/index.d.ts +4 -3
  15. package/dist/core/server/index.js +7 -4
  16. package/dist/{index-2JCvey4-.d.ts → index-CDnE3SGM.d.ts} +18 -3
  17. package/dist/index-CsynGAGv.d.ts +53 -0
  18. package/dist/{index-Dj9IEios.d.ts → index-CubM9whW.d.ts} +4 -17
  19. package/dist/{index-C1DHMQA0.d.ts → index-DSxDlF9J.d.ts} +45 -68
  20. package/dist/{index-BEvnPzzt.d.ts → index-Dm2VdDEB.d.ts} +2 -1
  21. package/dist/{index-DSHZvYii.d.ts → index-G_1SiZJo.d.ts} +8 -7
  22. package/dist/index.d.ts +407 -72
  23. package/dist/index.js +584 -79
  24. package/dist/issuer/index.d.ts +3 -2
  25. package/dist/issuer/index.js +4 -1
  26. package/dist/rule/index.d.ts +2 -2
  27. package/dist/rule/index.js +4 -3
  28. package/dist/rule-a_5ed-93.d.ts +39 -0
  29. package/dist/sessionPolicy/index.d.ts +3 -3
  30. package/dist/sessionPolicy/index.js +2 -2
  31. package/dist/{types-CpXiPRYs.d.ts → types-D2o6XS7a.d.ts} +1 -1
  32. package/dist/types-i4eTkhWa.d.ts +50 -0
  33. package/package.json +22 -9
  34. package/src/rule/engine/rule_engine.wasm +0 -0
  35. package/dist/chunk-DZ6GVRER.js +0 -309
  36. package/dist/chunk-QC24X74O.js +0 -41
  37. package/dist/index-CtdogR8X.d.ts +0 -82
  38. package/dist/types-B8pJQdMQ.d.ts +0 -26
@@ -1,28 +1,13 @@
1
- import {
2
- canonicalizeRuleSet
3
- } from "./chunk-6VPSJFO4.js";
4
1
  import {
5
2
  randomHex
6
3
  } from "./chunk-KDC67LIN.js";
7
4
 
8
5
  // src/sessionPolicy/create.ts
9
6
  import { ethers } from "ethers";
10
- async function createSessionPolicyPayload(params) {
11
- const issuedAt = Math.floor(Date.now() / 1e3);
12
- const nonce = randomHex(16);
13
- const payload = {
14
- version: "payid.session.policy.v1",
15
- receiver: params.receiver,
16
- rule: canonicalizeRuleSet(params.rule),
17
- issuedAt,
18
- expiresAt: params.expiresAt,
19
- nonce
20
- };
21
- const message = ethers.keccak256(
22
- ethers.toUtf8Bytes(JSON.stringify(payload))
7
+ async function createSessionPolicyPayload(_params) {
8
+ throw new Error(
9
+ "SESSION_POLICY_V1_DISABLED: V1 policies have no chainId binding and are vulnerable to cross-chain replay. Use createSessionPolicyV2() instead."
23
10
  );
24
- const signature = await params.signer.signMessage(message);
25
- return { ...payload, signature };
26
11
  }
27
12
  var SESSION_POLICY_V2_DOMAIN_NAME = "PAY.ID SessionPolicy";
28
13
  var SESSION_POLICY_V2_DOMAIN_VERSION = "1";
@@ -59,14 +44,20 @@ async function createSessionPolicyV2(params) {
59
44
  verifyingContract,
60
45
  signer
61
46
  } = params;
47
+ const MIN_TTL_SECONDS = 60;
48
+ const MAX_TTL_SECONDS = 30 * 24 * 3600;
62
49
  if (!ethers.isAddress(receiver)) {
63
50
  throw new Error(`SESSION_POLICY_V2: receiver address tidak valid: ${receiver}`);
64
51
  }
65
52
  if (maxAmount <= 0n) {
66
53
  throw new Error("SESSION_POLICY_V2: maxAmount harus > 0");
67
54
  }
68
- if (expiresAt <= Math.floor(Date.now() / 1e3)) {
69
- throw new Error("SESSION_POLICY_V2: expiresAt sudah lewat");
55
+ const now = Math.floor(Date.now() / 1e3);
56
+ if (expiresAt <= now + MIN_TTL_SECONDS) {
57
+ throw new Error(`SESSION_POLICY_V2: expiresAt terlalu dekat \u2014 minimum ${MIN_TTL_SECONDS}s dari sekarang`);
58
+ }
59
+ if (expiresAt > now + MAX_TTL_SECONDS) {
60
+ throw new Error(`SESSION_POLICY_V2: expiresAt terlalu jauh \u2014 maksimum ${MAX_TTL_SECONDS / 86400} hari`);
70
61
  }
71
62
  const policyNonce = randomHex(32);
72
63
  const payIdBytes32 = ethers.keccak256(ethers.toUtf8Bytes(payId));
@@ -16,9 +16,22 @@ __export(issuer_exports, {
16
16
  issueOracleContext: () => issueOracleContext,
17
17
  issueRiskContext: () => issueRiskContext,
18
18
  issueStateContext: () => issueStateContext,
19
+ issueTokenPriceContext: () => issueTokenPriceContext,
19
20
  signAttestation: () => signAttestation
20
21
  });
21
22
 
23
+ // src/issuer/issueTokenPriceContext.ts
24
+ async function issueTokenPriceContext(wallet, tokenPrice, tokenAmount, tokenDecimals) {
25
+ const amountUsd = tokenAmount * tokenPrice / 10n ** BigInt(tokenDecimals + 8);
26
+ const proof = await signAttestation(
27
+ wallet,
28
+ { amountUsd: amountUsd.toString() },
29
+ 120
30
+ );
31
+ return { amountUsd: amountUsd.toString(), proof };
32
+ }
33
+
22
34
  export {
35
+ issueTokenPriceContext,
23
36
  issuer_exports
24
37
  };
@@ -2,7 +2,7 @@ import {
2
2
  evaluate,
3
3
  generateDecisionProof,
4
4
  resolveRule
5
- } from "./chunk-DZ6GVRER.js";
5
+ } from "./chunk-HKHRYRD6.js";
6
6
  import {
7
7
  __export
8
8
  } from "./chunk-MLKGABMK.js";
@@ -10,20 +10,20 @@ import {
10
10
  // src/core/server/index.ts
11
11
  var server_exports = {};
12
12
  __export(server_exports, {
13
- createPayID: () => createPayID
13
+ PayIDServer: () => PayIDServer,
14
+ createPayIDServer: () => createPayIDServer
14
15
  });
15
16
 
16
17
  // src/erc4337/build.ts
17
18
  import { ethers } from "ethers";
19
+ var DECISION_TUPLE = "bytes32 version,bytes32 payId,address payer,address receiver,address asset,uint256 amount,bytes32 contextHash,bytes32 ruleSetHash,address ruleAuthority,uint64 issuedAt,uint64 expiresAt,bytes32 nonce,bool requiresAttestation,bytes32 attestationUIDsHash";
18
20
  var PAY_WITH_PAYID_ABI = [
19
- // ETH payment attestationUIDs adalah EAS UIDs, pass [] jika tidak perlu
20
- "function payETH((bytes32 version, bytes32 payId, address payer, address receiver, address asset, uint256 amount, bytes32 contextHash, bytes32 ruleSetHash, address ruleAuthority, uint64 issuedAt, uint64 expiresAt, bytes32 nonce, bool requiresAttestation) d, bytes sig, bytes32[] attestationUIDs) payable",
21
- // ERC20 payment
22
- "function payERC20((bytes32 version, bytes32 payId, address payer, address receiver, address asset, uint256 amount, bytes32 contextHash, bytes32 ruleSetHash, address ruleAuthority, uint64 issuedAt, uint64 expiresAt, bytes32 nonce, bool requiresAttestation) d, bytes sig, bytes32[] attestationUIDs)"
21
+ `function payNative((${DECISION_TUPLE}) d, bytes sig, bytes32[] attestationUIDs) payable`,
22
+ `function payERC20((${DECISION_TUPLE}) d, bytes sig, bytes32[] attestationUIDs)`
23
23
  ];
24
- function buildPayETHCallData(contractAddress, proof, attestationUIDs = []) {
24
+ function buildPayNativeCallData(contractAddress, proof, attestationUIDs = []) {
25
25
  const iface = new ethers.Interface(PAY_WITH_PAYID_ABI);
26
- return iface.encodeFunctionData("payETH", [
26
+ return iface.encodeFunctionData("payNative", [
27
27
  proof.payload,
28
28
  proof.signature,
29
29
  attestationUIDs
@@ -61,14 +61,22 @@ function isRuleSource(rule) {
61
61
  return typeof rule === "object" && rule !== null && "uri" in rule;
62
62
  }
63
63
  var PayIDServer = class {
64
- constructor(signer, trustedIssuers, debugTrace, wasm) {
64
+ constructor(signer, trustedIssuers, debugTrace, wasm, storage, resolverOptions) {
65
65
  this.signer = signer;
66
66
  this.trustedIssuers = trustedIssuers;
67
67
  this.debugTrace = debugTrace;
68
68
  this.wasm = wasm;
69
+ this.storage = storage;
70
+ this.resolverOptions = resolverOptions;
69
71
  }
72
+ signer;
73
+ trustedIssuers;
74
+ debugTrace;
75
+ wasm;
76
+ storage;
77
+ resolverOptions;
70
78
  async evaluateAndProve(params) {
71
- const authorityConfig = isRuleSource(params.authorityRule) ? (await resolveRule(params.authorityRule)).config : params.authorityRule;
79
+ const authorityConfig = isRuleSource(params.authorityRule) ? (await resolveRule(params.authorityRule, this.resolverOptions)).config : params.authorityRule;
72
80
  const evalConfig = params.evaluationRule ?? authorityConfig;
73
81
  const result = await evaluate(
74
82
  params.context,
@@ -100,11 +108,8 @@ var PayIDServer = class {
100
108
  return { result, proof };
101
109
  }
102
110
  buildUserOperation(params) {
103
- const callData = buildPayERC20CallData(
104
- params.targetContract,
105
- params.proof,
106
- params.attestationUIDs ?? []
107
- );
111
+ const isETH = params.paymentType === "eth";
112
+ const callData = isETH ? buildPayNativeCallData(params.targetContract, params.proof, params.attestationUIDs ?? []) : buildPayERC20CallData(params.targetContract, params.proof, params.attestationUIDs ?? []);
108
113
  return buildUserOperation({
109
114
  sender: params.smartAccount,
110
115
  nonce: params.nonce,
@@ -116,19 +121,17 @@ var PayIDServer = class {
116
121
  };
117
122
 
118
123
  // src/core/server/index.ts
119
- function createPayID(params) {
124
+ function createPayIDServer(params) {
120
125
  return new PayIDServer(
121
126
  params.signer,
122
127
  params.trustedIssuers,
123
- params.debugTrace ?? false,
128
+ params.debugTrace,
124
129
  params.wasm
125
130
  );
126
131
  }
127
132
 
128
133
  export {
129
- buildPayETHCallData,
130
- buildPayERC20CallData,
131
- buildUserOperation,
132
- createPayID,
134
+ PayIDServer,
135
+ createPayIDServer,
133
136
  server_exports
134
137
  };
@@ -4,12 +4,13 @@ import {
4
4
  import {
5
5
  decodeSessionPolicy,
6
6
  decodeSessionPolicyV2
7
- } from "./chunk-BLIRABV7.js";
7
+ } from "./chunk-AUW7WDAB.js";
8
8
  import {
9
9
  evaluate,
10
10
  generateDecisionProof,
11
+ loadWasm,
11
12
  resolveRule
12
- } from "./chunk-DZ6GVRER.js";
13
+ } from "./chunk-HKHRYRD6.js";
13
14
  import {
14
15
  __export
15
16
  } from "./chunk-MLKGABMK.js";
@@ -17,33 +18,38 @@ import {
17
18
  // src/core/client/index.ts
18
19
  var client_exports = {};
19
20
  __export(client_exports, {
20
- createPayID: () => createPayID
21
+ PayIDClient: () => PayIDClient,
22
+ createPayID: () => createPayIDClient,
23
+ createPayIDClient: () => createPayIDClient
21
24
  });
22
25
 
23
26
  // src/core/client/client.ts
24
- import { loadWasm } from "payid-rule-engine";
25
27
  import "ethers";
26
28
  function isRuleSource(rule) {
27
29
  return typeof rule === "object" && rule !== null && "uri" in rule;
28
30
  }
29
31
  var PayIDClient = class {
30
- constructor(debugTrace, wasm) {
32
+ constructor(debugTrace, wasm, resolverOptions) {
31
33
  this.debugTrace = debugTrace;
32
34
  this.wasm = wasm;
35
+ this.resolverOptions = resolverOptions;
33
36
  this._ready = loadWasm(this.wasm).then(() => {
34
37
  }).catch(() => {
35
38
  });
36
39
  }
40
+ debugTrace;
41
+ wasm;
42
+ resolverOptions;
37
43
  _ready;
38
44
  async ready() {
39
45
  return this._ready;
40
46
  }
41
47
  async evaluate(context, rule) {
42
- const config = isRuleSource(rule) ? (await resolveRule(rule)).config : rule;
48
+ const config = isRuleSource(rule) ? (await resolveRule(rule, this.resolverOptions)).config : rule;
43
49
  return evaluate(context, config, { debug: this.debugTrace }, this.wasm);
44
50
  }
45
51
  async evaluateAndProve(params) {
46
- const authorityConfig = isRuleSource(params.authorityRule) ? (await resolveRule(params.authorityRule)).config : params.authorityRule;
52
+ const authorityConfig = isRuleSource(params.authorityRule) ? (await resolveRule(params.authorityRule, this.resolverOptions)).config : params.authorityRule;
47
53
  const evalConfig = params.evaluationRule ?? (params.sessionPolicyV2 ? (() => {
48
54
  decodeSessionPolicyV2(params.sessionPolicyV2, params.blockTimestamp);
49
55
  return authorityConfig;
@@ -77,21 +83,20 @@ var PayIDClient = class {
77
83
  ruleAuthority: params.ruleAuthority,
78
84
  chainId: params.chainId ?? params.context?.tx?.chainId,
79
85
  ttlSeconds: params.ttlSeconds,
80
- blockTimestamp: params.blockTimestamp
86
+ blockTimestamp: params.blockTimestamp,
87
+ attestationUIDs: params.attestationUIDs
81
88
  });
82
89
  return { result, proof };
83
90
  }
84
91
  };
85
92
 
86
93
  // src/core/client/index.ts
87
- function createPayID(params) {
88
- return new PayIDClient(
89
- params.debugTrace ?? false,
90
- params.wasm
91
- );
94
+ function createPayIDClient(params) {
95
+ return new PayIDClient(params?.debugTrace, params?.wasm);
92
96
  }
93
97
 
94
98
  export {
95
- createPayID,
99
+ PayIDClient,
100
+ createPayIDClient,
96
101
  client_exports
97
102
  };
@@ -0,0 +1,24 @@
1
+ import {
2
+ combineRules
3
+ } from "./chunk-GG34PNTF.js";
4
+ import {
5
+ canonicalizeRuleSet
6
+ } from "./chunk-6VPSJFO4.js";
7
+ import {
8
+ hashRuleSet
9
+ } from "./chunk-X7NYQ47Y.js";
10
+ import {
11
+ __export
12
+ } from "./chunk-MLKGABMK.js";
13
+
14
+ // src/rule/index.ts
15
+ var rule_exports = {};
16
+ __export(rule_exports, {
17
+ canonicalizeRuleSet: () => canonicalizeRuleSet,
18
+ combineRules: () => combineRules,
19
+ hashRuleSet: () => hashRuleSet
20
+ });
21
+
22
+ export {
23
+ rule_exports
24
+ };