@0to1pay/server-sdk 1.0.1 → 2.1.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.
@@ -24,4 +24,3 @@ export declare const openapi: {
24
24
  };
25
25
  };
26
26
  export declare function validateSchema(name: string, data: unknown): string | undefined;
27
- export declare function check(ref: Ref | undefined, data: unknown): string | undefined;
@@ -14,6 +14,3 @@ export function validateSchema(name, data) {
14
14
  throw new Error('Unknown OpenAPI schema ' + name);
15
15
  return schema(data) ? undefined : ajv.errorsText(schema.errors);
16
16
  }
17
- export function check(ref, data) {
18
- return ref?.$ref ? validateSchema(ref.$ref.split('/').at(-1), data) : undefined;
19
- }
@@ -1,7 +1,7 @@
1
1
  import type { components, operations } from './schema.js';
2
2
  export type { components, operations, paths } from './schema.js';
3
3
  export type Schema<K extends keyof components['schemas']> = components['schemas'][K];
4
- type PrivateOperation = 'ingestProviderWebhook' | 'receiveStablePayEvent';
4
+ type PrivateOperation = 'receiveStablePayEvent';
5
5
  type OperationName = Exclude<keyof operations, PrivateOperation>;
6
6
  type JsonContent<T> = T extends {
7
7
  content: {
@@ -37,6 +37,8 @@ type OperationInput<K extends OperationName> = RequiredPath<K> & Body<K> & Idemp
37
37
  query?: ParametersOf<K>['query'];
38
38
  signal?: AbortSignal;
39
39
  };
40
+ /** Return the exact UTF-8 message the approved payer signs after broadcasting. */
41
+ export declare function chainIntentClaimMessage(intent: Pick<Schema<'ChainIntent'>, 'family' | 'signing_message'>, transactionId: string): string;
40
42
  export declare class ZeroToOnePayError extends Error {
41
43
  readonly status: number;
42
44
  readonly code: string;
@@ -121,28 +123,40 @@ export declare class ZeroToOnePayClient {
121
123
  expires_at: string;
122
124
  created_at: string;
123
125
  }>;
126
+ createChainIntent(quote_id: string, body: Schema<'CreateChainIntent'>, idempotencyKey: string): Promise<{
127
+ intent_id: string;
128
+ quote_id: string;
129
+ family: components["schemas"]["ChainFamily"];
130
+ payer_address: components["schemas"]["ChainAddress"];
131
+ expires_at: string;
132
+ signing_message: string;
133
+ status: "pending" | "authorized" | "submitted";
134
+ transaction_id: components["schemas"]["ChainIntentTransactionId"] | null;
135
+ }>;
136
+ authorizeChainIntent(quote_id: string, intent_id: string, body: Schema<'AuthorizeChainIntent'>, idempotencyKey: string): Promise<{
137
+ intent_id: string;
138
+ quote_id: string;
139
+ family: components["schemas"]["ChainFamily"];
140
+ payer_address: components["schemas"]["ChainAddress"];
141
+ expires_at: string;
142
+ signing_message: string;
143
+ status: "pending" | "authorized" | "submitted";
144
+ transaction_id: components["schemas"]["ChainIntentTransactionId"] | null;
145
+ }>;
146
+ submitChainIntent(quote_id: string, intent_id: string, body: Schema<'SubmitChainIntent'>, idempotencyKey: string): Promise<{
147
+ intent_id: string;
148
+ quote_id: string;
149
+ family: components["schemas"]["ChainFamily"];
150
+ payer_address: components["schemas"]["ChainAddress"];
151
+ expires_at: string;
152
+ signing_message: string;
153
+ status: "pending" | "authorized" | "submitted";
154
+ transaction_id: components["schemas"]["ChainIntentTransactionId"] | null;
155
+ }>;
124
156
  createAttempt(payment_id: string, body: Schema<'CreateAttempt'>, idempotencyKey: string): Promise<{
125
157
  attempt: components["schemas"]["Attempt"];
126
158
  operation: components["schemas"]["Operation"] | null;
127
159
  }>;
128
- attachExternalAttempt(payment_id: string, body: Schema<'AttachExternalAttempt'>, idempotencyKey: string): Promise<{
129
- merchant_id: string;
130
- project_id: string;
131
- environment: components["schemas"]["Environment"];
132
- attempt_id: string;
133
- payment_id: string;
134
- quote_id: string;
135
- connection_id: string;
136
- mode: "managed" | "external";
137
- execution_status: "created" | "requires_action" | "processing" | "unknown" | "succeeded" | "failed" | "canceled";
138
- operation_id: string | null;
139
- external_resources: components["schemas"]["ExternalResource"][];
140
- binding: components["schemas"]["Binding"];
141
- next_action: components["schemas"]["NextAction"];
142
- created_at: string;
143
- updated_at: string;
144
- resource_version: number;
145
- }>;
146
160
  claimFulfillment(command_id: string, body: Schema<'ClaimRequest'>, idempotencyKey: string): Promise<{
147
161
  claim_id: string;
148
162
  command_id: string;
@@ -12,6 +12,33 @@ for (const [path, item] of Object.entries(spec.paths)) {
12
12
  }
13
13
  }
14
14
  const keyPattern = /^[A-Za-z0-9._:-]{16,128}$/;
15
+ const hexTransactionId = /^0x[0-9a-f]{64}$/i;
16
+ const tronTransactionId = /^(?:0x)?[0-9a-f]{64}$/i;
17
+ const solanaTransactionId = /^[1-9A-HJ-NP-Za-km-z]{64,88}$/;
18
+ const base58Alphabet = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz';
19
+ function isSolanaTransactionId(value) {
20
+ if (!solanaTransactionId.test(value))
21
+ return false;
22
+ const leadingZeroBytes = value.search(/[^1]/);
23
+ if (leadingZeroBytes < 0 || leadingZeroBytes >= 64)
24
+ return false;
25
+ let decoded = 0n;
26
+ for (const digit of value.slice(leadingZeroBytes)) {
27
+ decoded = decoded * 58n + BigInt(base58Alphabet.indexOf(digit));
28
+ }
29
+ const limit = 1n << BigInt((64 - leadingZeroBytes) * 8);
30
+ return decoded >= (limit >> 8n) && decoded < limit;
31
+ }
32
+ /** Return the exact UTF-8 message the approved payer signs after broadcasting. */
33
+ export function chainIntentClaimMessage(intent, transactionId) {
34
+ const valid = intent.family === 'solana' ? isSolanaTransactionId(transactionId) :
35
+ { evm: hexTransactionId, tron: tronTransactionId }[intent.family]?.test(transactionId);
36
+ if (!valid)
37
+ throw new Error('Invalid chain transaction ID');
38
+ const canonical = intent.family === 'solana' ? transactionId :
39
+ '0x' + transactionId.replace(/^0x/i, '').toLowerCase();
40
+ return intent.signing_message + '\nTransaction: ' + canonical;
41
+ }
15
42
  export class ZeroToOnePayError extends Error {
16
43
  status;
17
44
  code;
@@ -140,12 +167,18 @@ export class ZeroToOnePayClient {
140
167
  createQuote(payment_id, body, idempotencyKey) {
141
168
  return this.call('createQuote', { path: { payment_id }, body, idempotencyKey });
142
169
  }
170
+ createChainIntent(quote_id, body, idempotencyKey) {
171
+ return this.call('createChainIntent', { path: { quote_id }, body, idempotencyKey });
172
+ }
173
+ authorizeChainIntent(quote_id, intent_id, body, idempotencyKey) {
174
+ return this.call('authorizeChainIntent', { path: { quote_id, intent_id }, body, idempotencyKey });
175
+ }
176
+ submitChainIntent(quote_id, intent_id, body, idempotencyKey) {
177
+ return this.call('submitChainIntent', { path: { quote_id, intent_id }, body, idempotencyKey });
178
+ }
143
179
  createAttempt(payment_id, body, idempotencyKey) {
144
180
  return this.call('createAttempt', { path: { payment_id }, body, idempotencyKey });
145
181
  }
146
- attachExternalAttempt(payment_id, body, idempotencyKey) {
147
- return this.call('attachExternalAttempt', { path: { payment_id }, body, idempotencyKey });
148
- }
149
182
  claimFulfillment(command_id, body, idempotencyKey) {
150
183
  return this.call('claimFulfillment', { path: { command_id }, body, idempotencyKey });
151
184
  }