mppx 0.22.0 → 0.22.2

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 (32) hide show
  1. package/CHANGELOG.md +15 -0
  2. package/dist/cli/account.d.ts +2 -1
  3. package/dist/cli/account.js +22 -11
  4. package/dist/cli/cli.js +18 -18
  5. package/dist/cli/plugins/index.js +12 -12
  6. package/dist/extensions/identity/attestation/client/index.js +1 -0
  7. package/dist/extensions/identity/attestation/server/index.js +1 -0
  8. package/dist/extensions/identity/email-otp/server/index.js +1 -0
  9. package/dist/extensions/identity/erc8004/client/index.js +1 -0
  10. package/dist/extensions/identity/erc8004/server/index.js +3 -1
  11. package/dist/extensions/identity/link/server/index.js +1 -0
  12. package/dist/extensions/identity/poppy/server/index.js +1 -0
  13. package/dist/extensions/identity/tempo/client/index.js +1 -0
  14. package/dist/extensions/identity/tempo/server/index.js +7 -1
  15. package/dist/extensions/identity/world/server/index.js +1 -0
  16. package/dist/identity/Headers.d.ts +3 -2
  17. package/dist/identity/Headers.js +3 -2
  18. package/dist/identity/Server.d.ts +8 -0
  19. package/dist/identity/Server.js +18 -0
  20. package/dist/identity/Types.d.ts +16 -2
  21. package/dist/internal/version.d.ts +2 -2
  22. package/dist/internal/version.js +1 -1
  23. package/dist/proxy/Proxy.js +59 -46
  24. package/dist/proxy/internal/Headers.d.ts +9 -2
  25. package/dist/proxy/internal/Headers.js +16 -3
  26. package/dist/server/Mppx.d.ts +2 -0
  27. package/dist/server/Mppx.js +1 -0
  28. package/dist/tempo/client/Charge.js +19 -0
  29. package/dist/tempo/server/Charge.d.ts +7 -0
  30. package/dist/tempo/server/Charge.js +100 -12
  31. package/dist/tempo/server/internal/html.gen.js +1 -1
  32. package/package.json +1 -1
@@ -186,11 +186,19 @@ export function charge(parameters = {}) {
186
186
  const { amount, chainId, challenge, client, currency, methodDetails, recipient, settlementSenders, supportedModes, } = context;
187
187
  if (supportedModes && !supportedModes.includes('push'))
188
188
  throw new MismatchError('Hash credentials are not supported for this challenge.', {});
189
+ // Read from the signed Challenge, which can differ from the route's current request.
190
+ const offered = credential.challenge.request.methodDetails;
191
+ // A server memo is shared by every payer of a route, so it can't bind a pushed transfer
192
+ // to the client that claims it.
193
+ if (offered?.memo !== undefined)
194
+ throw new MismatchError('Hash credentials are not supported for a challenge with a memo.', {});
189
195
  const source = parseHashCredentialSource({
190
196
  chainId: chainId ?? client.chain?.id,
191
197
  source: credential.source,
192
198
  });
193
- const transfers = getExpectedTransfers({ amount, methodDetails, recipient });
199
+ // Only a primary transfer carrying this challenge's attribution memo counts toward it.
200
+ const isBound = boundMemoMatcher({ challengeId: challenge.id, realm: challenge.realm });
201
+ const transfers = getExpectedTransfers({ amount, isBound, methodDetails, recipient });
194
202
  const receipt = await getTransactionReceipt(client, {
195
203
  hash: payload.hash,
196
204
  });
@@ -209,6 +217,11 @@ export function charge(parameters = {}) {
209
217
  realm: challenge.realm,
210
218
  required: true,
211
219
  });
220
+ // A counted transfer without this challenge's attribution memo (a plain split) isn't tied to
221
+ // it, so another challenge paid in the same transaction could count it again once this hash
222
+ // claim expires. Count such a transfer only when the transaction pays no other challenge.
223
+ if (matchedLogs.some((log) => log.kind !== 'memo' || !isBound(log.args.memo)))
224
+ assertNoOtherChallenge(receipt, { currency, isBound });
212
225
  // The payer is the account the matched transfers debited, not the submitter.
213
226
  // Settlement-contract transfers are funded by the transaction sender.
214
227
  const payers = new Set(matchedLogs.map(({ args }) => (settlementSenders.some((settlementSender) => TempoAddress.isEqual(settlementSender, args.from))
@@ -955,6 +968,11 @@ function chargeBinding(request) {
955
968
  zones,
956
969
  };
957
970
  }
971
+ /**
972
+ * The transfers a charge expects. With isBound (push payments), the primary transfer must carry
973
+ * this challenge's attribution memo, and no transfer counts one carrying another challenge's,
974
+ * so transfers for other challenges in the same transaction are skipped rather than matched.
975
+ */
958
976
  function getExpectedTransfers(parameters) {
959
977
  return Charge_internal.getTransfers({
960
978
  amount: parameters.amount,
@@ -962,10 +980,17 @@ function getExpectedTransfers(parameters) {
962
980
  splits: parameters.methodDetails?.splits,
963
981
  },
964
982
  recipient: parameters.recipient,
965
- }).map((transfer) => ({
966
- ...transfer,
967
- ...(!transfer.memo ? { allowAnyMemo: true } : {}),
968
- }));
983
+ }).map((transfer, index) => {
984
+ const { isBound } = parameters;
985
+ // Matching checks requireBound first; allowAnyMemo keeps the fee-payer route check accepting it.
986
+ if (index === 0 && isBound)
987
+ return { ...transfer, allowAnyMemo: true, isBound, requireBound: true };
988
+ return {
989
+ ...transfer,
990
+ ...(!transfer.memo ? { allowAnyMemo: true } : {}),
991
+ ...(isBound ? { isBound } : {}),
992
+ };
993
+ });
969
994
  }
970
995
  function assertTransferCalls(calls, parameters) {
971
996
  const transferCalls = getTransferCalls(calls);
@@ -977,9 +1002,11 @@ function assertTransferCalls(calls, parameters) {
977
1002
  // Match memo-specific transfers before wildcards to avoid greedy
978
1003
  // consumption of memo-bearing calls by allowAnyMemo entries.
979
1004
  const sorted = [...parameters.transfers].sort((a, b) => {
980
- if (a.memo && !b.memo)
1005
+ const aSpecific = !!(a.memo || a.requireBound);
1006
+ const bSpecific = !!(b.memo || b.requireBound);
1007
+ if (aSpecific && !bSpecific)
981
1008
  return -1;
982
- if (!a.memo && b.memo)
1009
+ if (!aSpecific && bSpecific)
983
1010
  return 1;
984
1011
  return 0;
985
1012
  });
@@ -996,6 +1023,8 @@ function assertTransferCalls(calls, parameters) {
996
1023
  return false;
997
1024
  if (decoded.amount !== expected.amount)
998
1025
  return false;
1026
+ if (expected.requireBound)
1027
+ return !!decoded.memo && !!expected.isBound?.(decoded.memo);
999
1028
  if (expected.memo) {
1000
1029
  return decoded.memo?.toLowerCase() === expected.memo.toLowerCase();
1001
1030
  }
@@ -1057,14 +1086,20 @@ async function assertTransferLogs(receipt, parameters) {
1057
1086
  // Match memo-specific transfers before wildcards to avoid greedy
1058
1087
  // consumption of memo-bearing logs by allowAnyMemo entries.
1059
1088
  const sorted = [...parameters.transfers].sort((a, b) => {
1060
- if (a.memo && !b.memo)
1089
+ const aSpecific = !!(a.memo || a.requireBound);
1090
+ const bSpecific = !!(b.memo || b.requireBound);
1091
+ if (aSpecific && !bSpecific)
1061
1092
  return -1;
1062
- if (!a.memo && b.memo)
1093
+ if (!aSpecific && bSpecific)
1063
1094
  return 1;
1064
1095
  return 0;
1065
1096
  });
1066
1097
  for (const transfer of sorted) {
1067
1098
  let matchIndex = -1;
1099
+ // An eligible transfer without this challenge's attribution memo, used when none carries it.
1100
+ let fallbackIndex = -1;
1101
+ // A transfer with the right amount and recipient whose memo isn't bound to this challenge.
1102
+ let unboundIndex = -1;
1068
1103
  for (const [index, log] of logs.entries()) {
1069
1104
  if (used.has(index))
1070
1105
  continue;
@@ -1074,19 +1109,34 @@ async function assertTransferLogs(receipt, parameters) {
1074
1109
  continue;
1075
1110
  if (log.args.amount.toString() !== transfer.amount)
1076
1111
  continue;
1112
+ const memo = log.kind === 'memo' ? log.args.memo : undefined;
1113
+ const bound = !!memo && !!transfer.isBound?.(memo);
1077
1114
  const memoMatches = (() => {
1115
+ if (transfer.requireBound)
1116
+ return bound;
1078
1117
  if (transfer.memo)
1079
- return log.kind === 'memo' && log.args.memo.toLowerCase() === transfer.memo.toLowerCase();
1118
+ return !!memo && memo.toLowerCase() === transfer.memo.toLowerCase();
1119
+ // Another challenge's attribution memo never counts toward this one.
1120
+ if (transfer.isBound && memo && Attribution.isMppMemo(memo) && !bound)
1121
+ return false;
1080
1122
  if (transfer.allowAnyMemo)
1081
- return log.kind === 'transfer' || log.kind === 'memo';
1123
+ return true;
1082
1124
  return log.kind === 'transfer';
1083
1125
  })();
1084
- if (!memoMatches)
1126
+ if (!memoMatches) {
1127
+ if (transfer.isBound && !transfer.memo && unboundIndex === -1)
1128
+ unboundIndex = index;
1085
1129
  continue;
1130
+ }
1086
1131
  // A Zone portal's payout settles a Zone withdrawal, which pays only through its `zone`
1087
1132
  // credential, so the same settlement can't also be redeemed by transaction hash.
1088
1133
  if (Zone.isPortalAddress(log.args.from))
1089
1134
  continue;
1135
+ // A wildcard prefers a transfer carrying this challenge's attribution memo: keep the first
1136
+ // usable one without it as a fallback, and don't check the sender of any later one.
1137
+ const fallback = !!transfer.isBound && !transfer.requireBound && !transfer.memo && !bound;
1138
+ if (fallback && fallbackIndex !== -1)
1139
+ continue;
1090
1140
  if (!(await isValidTransferSender({
1091
1141
  expectedSender: parameters.sender,
1092
1142
  sender: log.args.from,
@@ -1096,10 +1146,18 @@ async function assertTransferLogs(receipt, parameters) {
1096
1146
  validateSender: parameters.validateSender,
1097
1147
  })))
1098
1148
  continue;
1149
+ if (fallback) {
1150
+ fallbackIndex = index;
1151
+ continue;
1152
+ }
1099
1153
  matchIndex = index;
1100
1154
  break;
1101
1155
  }
1156
+ if (matchIndex === -1)
1157
+ matchIndex = fallbackIndex;
1102
1158
  if (matchIndex === -1) {
1159
+ if (unboundIndex !== -1)
1160
+ throw new MismatchError('Payment verification failed: memo is not bound to this challenge.', {});
1103
1161
  throw new MismatchError('Payment verification failed: no matching transfer found.', {
1104
1162
  amount: transfer.amount,
1105
1163
  currency: parameters.currency,
@@ -1111,6 +1169,36 @@ async function assertTransferLogs(receipt, parameters) {
1111
1169
  }
1112
1170
  return matched;
1113
1171
  }
1172
+ /**
1173
+ * Returns whether a memo is an attribution memo bound to this challenge's realm and ID, whatever
1174
+ * its client. The expected segments are hashed once, not per transfer.
1175
+ */
1176
+ function boundMemoMatcher(binding) {
1177
+ const expected = Attribution.encode({
1178
+ challengeId: binding.challengeId,
1179
+ serverId: binding.realm,
1180
+ }).toLowerCase();
1181
+ const server = expected.slice(12, 32);
1182
+ const nonce = expected.slice(52);
1183
+ return (memo) => {
1184
+ if (!Attribution.isMppMemo(memo))
1185
+ return false;
1186
+ const lower = memo.toLowerCase();
1187
+ return lower.slice(12, 32) === server && lower.slice(52) === nonce;
1188
+ };
1189
+ }
1190
+ /**
1191
+ * Rejects a transaction that carries an attribution memo for any other challenge, in any realm.
1192
+ * @internal
1193
+ */
1194
+ function assertNoOtherChallenge(receipt, parameters) {
1195
+ for (const log of getTransferLogEffects(receipt)) {
1196
+ if (log.kind !== 'memo' || !TempoAddress.isEqual(log.address, parameters.currency))
1197
+ continue;
1198
+ if (Attribution.isMppMemo(log.args.memo) && !parameters.isBound(log.args.memo))
1199
+ throw new MismatchError('Payment verification failed: memo is not bound to this challenge.', {});
1200
+ }
1201
+ }
1114
1202
  function getTransferLogEffects(receipt) {
1115
1203
  const transferLogs = parseEventLogs({
1116
1204
  abi: Abis.tip20,