@mysten/payment-kit 0.1.9 → 0.1.10

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.
@@ -1,743 +0,0 @@
1
- # Payment Kit SDK
2
-
3
- > Payment Kit SDK API reference and configuration for the PaymentKitClient.
4
-
5
- > **Note:** This package is in active development and should be used with caution. APIs are
6
- > experimental and subject to breaking changes without notice. We recommend thoroughly testing any
7
- > implementation before using in production environments.
8
-
9
- Complete API reference for the Payment Kit SDK.
10
-
11
- ## Client setup
12
-
13
- ### PaymentKitClient
14
-
15
- The main client class that provides access to Payment Kit functionality.
16
-
17
- #### `paymentKit()`
18
-
19
- Function to create a Payment Kit client extension for `SuiGrpcClient`.
20
-
21
- ```ts
22
- paymentKit<Name extends string = 'paymentKit'>({
23
- name = 'paymentKit' as Name,
24
- }): SuiClientRegistration<PaymentKitCompatibleClient, Name, PaymentKitClient>
25
- ```
26
-
27
- **Example:**
28
-
29
- ```ts
30
- const client = new SuiGrpcClient({
31
- network: 'testnet',
32
- baseUrl: 'https://fullnode.testnet.sui.io:443',
33
- }).$extend(paymentKit());
34
-
35
- // Access Payment Kit functionality
36
- client.paymentKit.tx.processRegistryPayment(/* ... */);
37
- ```
38
-
39
- **Supported Networks:**
40
-
41
- - `testnet`
42
- - `mainnet`
43
-
44
- **Throws:**
45
-
46
- - `PaymentKitClientError` if network is unsupported
47
-
48
- ## Transaction methods
49
-
50
- Transaction methods create complete, ready-to-sign transactions.
51
-
52
- ### `client.paymentKit.tx.processRegistryPayment()`
53
-
54
- Create a transaction to process a registry-based payment.
55
-
56
- ```ts
57
- processRegistryPayment(
58
- options: ProcessRegistryPaymentOptions
59
- ): Transaction
60
- ```
61
-
62
- **Parameters:**
63
-
64
- ```ts
65
- type ProcessRegistryPaymentOptions = {
66
- nonce: string; // Unique payment identifier
67
- coinType: string; // Coin type (e.g., '0x2::sui::SUI')
68
- amount: number | bigint; // Amount in smallest unit
69
- receiver: string; // Recipient address
70
- sender: string; // Sender address (must match signer)
71
- } & Partial<Registry>;
72
- ```
73
-
74
- **Returns:** `Transaction` - Ready-to-sign transaction
75
-
76
- If you prefer to provide the `registryId`, you can do so instead of the `registryName`.
77
-
78
- **Example:**
79
-
80
- ```ts
81
- const tx = client.paymentKit.tx.processRegistryPayment({
82
- nonce: crypto.randomUUID(),
83
- coinType: '0x2::sui::SUI',
84
- amount: 1000000000,
85
- receiver,
86
- sender: senderAddress,
87
- });
88
-
89
- const result = await client.signAndExecuteTransaction({
90
- transaction: tx,
91
- signer: keypair,
92
- });
93
-
94
- // Check transaction status
95
- if (result.$kind === 'FailedTransaction') {
96
- throw new Error(`Payment failed: ${result.FailedTransaction.status.error?.message}`);
97
- }
98
- ```
99
-
100
- **On-Chain Effects:**
101
-
102
- - Transfers coins from sender to receiver
103
- - Creates a `PaymentRecord` dynamic field on the registry
104
- - Emits a `PaymentReceipt` event
105
-
106
- **Throws:**
107
-
108
- - Error if payment with same key already exists
109
- - Error if sender has insufficient balance
110
-
111
- ---
112
-
113
- ### `client.paymentKit.tx.processEphemeralPayment()`
114
-
115
- Create a transaction to process an ephemeral payment (no registry storage).
116
-
117
- ```ts
118
- processEphemeralPayment(
119
- options: ProcessEphemeralPaymentOptions
120
- ): Transaction
121
- ```
122
-
123
- **Parameters:**
124
-
125
- ```ts
126
- type ProcessEphemeralPaymentOptions = {
127
- nonce: string; // Payment identifier (not enforced for uniqueness)
128
- coinType: string; // Coin type
129
- amount: number | bigint; // Amount in smallest unit
130
- receiver: string; // Recipient address
131
- sender: string; // Sender address (must match signer)
132
- };
133
- ```
134
-
135
- **Returns:** `Transaction` - Ready-to-sign transaction
136
-
137
- **Example:**
138
-
139
- ```ts
140
- const tx = client.paymentKit.tx.processEphemeralPayment({
141
- nonce: crypto.randomUUID(),
142
- coinType: '0x2::sui::SUI',
143
- amount: 500000000,
144
- receiver,
145
- sender: senderAddress,
146
- });
147
-
148
- const result = await client.signAndExecuteTransaction({
149
- transaction: tx,
150
- signer: keypair,
151
- });
152
-
153
- // Check transaction status
154
- if (result.$kind === 'FailedTransaction') {
155
- throw new Error(`Ephemeral payment failed: ${result.FailedTransaction.status.error?.message}`);
156
- }
157
- ```
158
-
159
- **On-Chain Effects:**
160
-
161
- - Transfers coins from sender to receiver
162
- - Emits a `PaymentReceipt` event
163
- - No `PaymentRecord` created (no duplicate prevention)
164
-
165
- ---
166
-
167
- ### `client.paymentKit.tx.createRegistry()`
168
-
169
- Create a transaction to create a new payment registry.
170
-
171
- ```ts
172
- createRegistry(options: CreateRegistryOptions): Transaction
173
- ```
174
-
175
- **Parameters:**
176
-
177
- ```ts
178
- type CreateRegistryOptions = {
179
- registryName: string; // Unique name for the registry
180
- };
181
- ```
182
-
183
- **Returns:** `Transaction` - Ready-to-sign transaction
184
-
185
- **Example:**
186
-
187
- ```ts
188
- const tx = client.paymentKit.tx.createRegistry({
189
- registryName: 'my-payment-registry',
190
- });
191
-
192
- const result = await client.signAndExecuteTransaction({
193
- transaction: tx,
194
- signer: keypair,
195
- options: {
196
- showObjectChanges: true,
197
- },
198
- });
199
-
200
- // Extract the RegistryAdminCap from result.Transaction.objectChanges
201
- const adminCap = result.Transaction.objectChanges?.find(
202
- (change) => change.type === 'created' && change.objectType.includes('RegistryAdminCap'),
203
- );
204
- ```
205
-
206
- **On-Chain Effects:**
207
-
208
- - Creates a `PaymentRegistry` object
209
- - Creates a `RegistryAdminCap` owned by sender
210
- - Registry ID is deterministically derived from name
211
-
212
- **Important:**
213
-
214
- - Registry names must be unique globally
215
- - Save the `RegistryAdminCap` object ID for configuration operations
216
-
217
- ---
218
-
219
- ### `client.paymentKit.tx.setConfigEpochExpirationDuration()`
220
-
221
- Create a transaction to set the epoch expiration duration for payment records.
222
-
223
- ```ts
224
- setConfigEpochExpirationDuration(
225
- options: SetEpochExpirationDurationOptions
226
- ): Transaction
227
- ```
228
-
229
- **Parameters:**
230
-
231
- ```ts
232
- type SetEpochExpirationDurationOptions = {
233
- epochExpirationDuration: number | bigint; // Number of epochs before records expire
234
- adminCapId: string; // Admin capability object ID
235
- } & Partial<Registry>;
236
- ```
237
-
238
- **Returns:** `Transaction` - Ready-to-sign transaction
239
-
240
- **Example:**
241
-
242
- ```ts
243
- const tx = client.paymentKit.tx.setConfigEpochExpirationDuration({
244
- registryName: 'my-registry',
245
- epochExpirationDuration: 60,
246
- adminCapId: adminCapId,
247
- });
248
-
249
- await client.signAndExecuteTransaction({
250
- transaction: tx,
251
- signer: keypair,
252
- });
253
- ```
254
-
255
- **Requires:**
256
-
257
- - Transaction signer must own the admin capability
258
-
259
- **Default:** 30 epochs
260
-
261
- ---
262
-
263
- ### `client.paymentKit.tx.setConfigRegistryManagedFunds()`
264
-
265
- Create a transaction to enable/disable registry-managed funds.
266
-
267
- ```ts
268
- setConfigRegistryManagedFunds(
269
- options: SetRegistryManagedFundsOptions
270
- ): Transaction
271
- ```
272
-
273
- **Parameters:**
274
-
275
- ```ts
276
- type SetRegistryManagedFundsOptions = {
277
- registryManagedFunds: boolean; // Enable (true) or disable (false)
278
- adminCapId: string; // Admin capability object ID
279
- } & Partial<Registry>;
280
- ```
281
-
282
- **Returns:** `Transaction` - Ready-to-sign transaction
283
-
284
- **Example:**
285
-
286
- ```ts
287
- const tx = client.paymentKit.tx.setConfigRegistryManagedFunds({
288
- registryName: 'my-registry',
289
- registryManagedFunds: true,
290
- adminCapId: adminCapId,
291
- });
292
-
293
- await client.signAndExecuteTransaction({
294
- transaction: tx,
295
- signer: keypair,
296
- });
297
- ```
298
-
299
- **When Enabled:**
300
-
301
- - Payment receiver must be the registry itself
302
- - Funds accumulate in the registry
303
- - Admin can withdraw funds later
304
-
305
- **Default:** Disabled (funds go directly to receivers)
306
-
307
- ---
308
-
309
- ### `client.paymentKit.tx.withdrawFromRegistry()`
310
-
311
- Create a transaction to withdraw accumulated funds from a registry.
312
-
313
- ```ts
314
- withdrawFromRegistry(
315
- options: WithdrawFromRegistryOptions
316
- ): Transaction
317
- ```
318
-
319
- **Parameters:**
320
-
321
- ```ts
322
- type WithdrawFromRegistryOptions = {
323
- coinType: string; // Coin type to withdraw
324
- adminCapId: string; // Admin capability object ID
325
- } & Partial<Registry>;
326
- ```
327
-
328
- **Returns:** `Transaction` - Ready-to-sign transaction
329
-
330
- **Example:**
331
-
332
- ```ts
333
- const tx = client.paymentKit.tx.withdrawFromRegistry({
334
- coinType: '0x2::sui::SUI',
335
- registryName: 'my-registry',
336
- adminCapId: adminCapId,
337
- });
338
-
339
- const result = await client.signAndExecuteTransaction({
340
- transaction: tx,
341
- signer: keypair,
342
- });
343
- ```
344
-
345
- **Requires:**
346
-
347
- - Registry must have `registryManagedFunds` enabled
348
- - Transaction signer must own the admin capability
349
-
350
- **Effect:**
351
-
352
- - Withdraws all coins of specified type to the sender
353
-
354
- ---
355
-
356
- ### `client.paymentKit.tx.deletePaymentRecord()`
357
-
358
- Create a transaction to delete an expired payment record.
359
-
360
- ```ts
361
- deletePaymentRecord(
362
- options: DeletePaymentRecordOptions
363
- ): Transaction
364
- ```
365
-
366
- **Parameters:**
367
-
368
- ```ts
369
- type DeletePaymentRecordOptions = {
370
- nonce: string; // Payment nonce
371
- coinType: string; // Coin type of the payment
372
- amount: number | bigint; // Payment amount
373
- receiver: string; // Payment receiver
374
- } & Partial<Registry>;
375
- ```
376
-
377
- **Returns:** `Transaction` - Ready-to-sign transaction
378
-
379
- **Example:**
380
-
381
- ```ts
382
- const tx = client.paymentKit.tx.deletePaymentRecord({
383
- nonce: crypto.randomUUID(),
384
- coinType: '0x2::sui::SUI',
385
- amount: 1000000000,
386
- receiver,
387
- registryName: 'my-registry',
388
- });
389
-
390
- const result = await client.signAndExecuteTransaction({
391
- transaction: tx,
392
- signer: keypair,
393
- });
394
- ```
395
-
396
- **Requires:**
397
-
398
- - Payment record must be expired (current epoch >= record epoch + expiration duration)
399
- - Anyone can delete expired records (permissionless)
400
-
401
- **Effect:**
402
-
403
- - Removes the payment record from the registry
404
- - Sender receives storage rebate
405
-
406
- ## Query methods
407
-
408
- ### `client.paymentKit.getPaymentRecord()`
409
-
410
- Query the blockchain for a payment record.
411
-
412
- ```ts
413
- async getPaymentRecord(
414
- options: GetPaymentRecordOptions
415
- ): Promise<GetPaymentRecordResponse | null>
416
- ```
417
-
418
- **Parameters:**
419
-
420
- ```ts
421
- type GetPaymentRecordOptions = {
422
- nonce: string; // Payment nonce
423
- coinType: string; // Coin type
424
- amount: number | bigint; // Payment amount
425
- receiver: string; // Payment receiver
426
- } & Partial<Registry>;
427
- ```
428
-
429
- **Returns:**
430
-
431
- ```ts
432
- type GetPaymentRecordResponse = {
433
- key: string; // Dynamic field object ID
434
- paymentTransactionDigest: string | null; // Transaction that created the record
435
- epochAtTimeOfRecord: string; // Epoch when record was created
436
- } | null; // null if record doesn't exist
437
- ```
438
-
439
- **Example:**
440
-
441
- ```ts
442
- const record = await client.paymentKit.getPaymentRecord({
443
- nonce: crypto.randomUUID(),
444
- coinType: '0x2::sui::SUI',
445
- amount: 1000000000,
446
- receiver,
447
- });
448
-
449
- if (record) {
450
- console.log('Payment exists!');
451
- console.log('Transaction:', record.paymentTransactionDigest);
452
- console.log('Created at epoch:', record.epochAtTimeOfRecord);
453
- } else {
454
- console.log('Payment not found');
455
- }
456
- ```
457
-
458
- **Use Cases:**
459
-
460
- - Verify a payment was completed
461
- - Check for duplicate payments before processing
462
- - Retrieve payment details for reconciliation
463
-
464
- ## Call methods
465
-
466
- Call methods return transaction commands that can be composed in custom transactions. Use these when
467
- you need fine-grained control over transaction construction.
468
-
469
- ### `client.paymentKit.calls.processRegistryPayment()`
470
-
471
- Returns a transaction command for registry-based payment.
472
-
473
- ```ts
474
- processRegistryPayment(
475
- options: ProcessRegistryPaymentOptions
476
- ): TransactionObjectArgument
477
- ```
478
-
479
- **Example:**
480
-
481
- ```ts
482
- const tx = new Transaction();
483
-
484
- // Add custom logic before payment
485
- tx.moveCall({
486
- target: '0xMyPackage::my_module::pre_payment_check',
487
- arguments: [
488
- /* ... */
489
- ],
490
- });
491
-
492
- // Add payment command
493
- tx.add(
494
- client.paymentKit.calls.processRegistryPayment({
495
- nonce: crypto.randomUUID(),
496
- coinType: '0x2::sui::SUI',
497
- amount: 1000000000,
498
- receiver,
499
- sender: senderAddress,
500
- }),
501
- );
502
-
503
- // Add custom logic after payment
504
- tx.moveCall({
505
- target: '0xMyPackage::my_module::post_payment_action',
506
- arguments: [
507
- /* ... */
508
- ],
509
- });
510
-
511
- await client.signAndExecuteTransaction({
512
- transaction: tx,
513
- signer: keypair,
514
- });
515
- ```
516
-
517
- ### Other call methods
518
-
519
- All transaction methods have corresponding call methods:
520
-
521
- - `client.paymentKit.calls.processEphemeralPayment()`
522
- - `client.paymentKit.calls.createRegistry()`
523
- - `client.paymentKit.calls.setConfigEpochExpirationDuration()`
524
- - `client.paymentKit.calls.setConfigRegistryManagedFunds()`
525
- - `client.paymentKit.calls.withdrawFromRegistry()`
526
- - `client.paymentKit.calls.deletePaymentRecord()`
527
-
528
- ## Types
529
-
530
- ### PaymentReceipt event
531
-
532
- Emitted when a payment is processed (registry or ephemeral).
533
-
534
- ```ts
535
- type PaymentReceipt = {
536
- payment_type: 'Registry' | 'Ephemeral'; // Payment model used
537
- nonce: string; // Payment identifier
538
- amount: string; // Amount (as string)
539
- receiver: string; // Recipient address
540
- coin_type: string; // Coin type
541
- timestamp_ms: string; // Unix timestamp in ms
542
- };
543
- ```
544
-
545
- **Extracting from Transaction Result:**
546
-
547
- ```ts
548
- const result = await client.signAndExecuteTransaction({
549
- transaction: tx,
550
- signer: keypair,
551
- options: {
552
- showEvents: true,
553
- },
554
- });
555
-
556
- const receiptEvent = result.Transaction.events?.find((event) =>
557
- event.type.includes('PaymentReceipt'),
558
- );
559
-
560
- if (receiptEvent) {
561
- const receipt = receiptEvent.parsedJson as PaymentReceipt;
562
- console.log('Payment Type:', receipt.payment_type);
563
- console.log('Nonce:', receipt.nonce);
564
- console.log('Amount:', receipt.amount);
565
- }
566
- ```
567
-
568
- ## Error handling
569
-
570
- ### PaymentKitClientError
571
-
572
- Custom error class for Payment Kit errors.
573
-
574
- ```ts
575
- class PaymentKitClientError extends Error {
576
- constructor(message: string);
577
- }
578
- ```
579
-
580
- ## URI utilities
581
-
582
- Payment Kit provides utility functions for creating and parsing payment transaction URIs. These URIs
583
- follow the `sui:pay` protocol and can be used to encode payment parameters for QR codes, deep links,
584
- or other sharing mechanisms.
585
-
586
- ### URI format
587
-
588
- Payment URIs follow the `sui:pay` protocol:
589
-
590
- ```
591
- sui:pay?
592
- receiver=<address>
593
- &amount=<amount>
594
- &coinType=<coinType>
595
- &nonce=<nonce>
596
- &registry=<registryIdOrName>
597
- &label=<label>
598
- &message=<message>
599
- &iconUrl=<url>
600
- ```
601
-
602
- **Required Parameters:**
603
-
604
- | Parameter | Description |
605
- | ---------- | --------------------------------------------------- |
606
- | `receiver` | Recipient Sui address |
607
- | `amount` | Payment amount in smallest unit (for example, MIST) |
608
- | `coinType` | Coin type (for example, `0x2::sui::SUI`) |
609
- | `nonce` | Unique payment identifier (max 36 chars) |
610
-
611
- **Optional Parameters:**
612
-
613
- | Parameter | Description |
614
- | ---------- | ----------------------------------------------- |
615
- | `registry` | Registry ID or name for registry-based payments |
616
- | `label` | Human-readable label for the payment |
617
- | `message` | Message or memo for the payment |
618
- | `iconUrl` | URL to an icon image for display purposes |
619
-
620
- ---
621
-
622
- ### `createPaymentTransactionUri()`
623
-
624
- Creates a payment transaction URI from the given parameters.
625
-
626
- ```ts
627
- createPaymentTransactionUri(params: PaymentUriParams): string
628
- ```
629
-
630
- **Parameters:**
631
-
632
- ```ts
633
- type PaymentUriParams = {
634
- receiverAddress: string; // Recipient Sui address
635
- amount: bigint; // Amount in smallest unit
636
- coinType: string; // Coin type (e.g., '0x2::sui::SUI')
637
- nonce: string; // Unique identifier (max 36 chars)
638
- label?: string; // Optional human-readable label
639
- message?: string; // Optional message/memo
640
- iconUrl?: string; // Optional icon URL
641
- } & Partial<Registry>;
642
- ```
643
-
644
- **Returns:** `string` - The constructed payment URI
645
-
646
- **Example:**
647
-
648
- ```ts
649
- // Create a URI for a registry-based payment
650
- const uri = createPaymentTransactionUri({
651
- receiverAddress: '0x1234...abcd',
652
- amount: 1000000000n, // 1 SUI
653
- coinType: '0x2::sui::SUI',
654
- nonce: crypto.randomUUID(),
655
- registryName: 'my-payment-registry',
656
- label: 'Coffee Purchase',
657
- message: 'Thanks for your order!',
658
- });
659
-
660
- // Result:
661
- // sui:pay?
662
- // receiver=0x1234...
663
- // &amount=1000000000
664
- // &coinType=0x2::sui::SUI
665
- // &nonce=abc-123-def
666
- // &registry=my-payment-registry
667
- // &label=Coffee+Purchase
668
- // &message=Thanks+for+your+order!
669
-
670
- // Create a URI for an ephemeral payment (no registry)
671
- const ephemeralUri = createPaymentTransactionUri({
672
- receiverAddress: '0x1234...abcd',
673
- amount: 500000000n, // 0.5 SUI
674
- coinType: '0x2::sui::SUI',
675
- nonce: crypto.randomUUID(),
676
- });
677
- ```
678
-
679
- **Throws:**
680
-
681
- - `PaymentKitUriError` if receiver address is invalid
682
- - `PaymentKitUriError` if amount is not positive
683
- - `PaymentKitUriError` if coin type is invalid
684
- - `PaymentKitUriError` if nonce is missing or exceeds 36 characters
685
- - `PaymentKitUriError` if registry ID is not a valid Sui object ID
686
-
687
- ---
688
-
689
- ### `parsePaymentTransactionUri()`
690
-
691
- Parses a payment transaction URI into its component parameters.
692
-
693
- ```ts
694
- parsePaymentTransactionUri(uri: string): PaymentUriParams
695
- ```
696
-
697
- **Parameters:**
698
-
699
- | Parameter | Type | Description |
700
- | --------- | -------- | ------------------------ |
701
- | `uri` | `string` | The payment URI to parse |
702
-
703
- **Returns:** `PaymentUriParams` - The parsed payment parameters
704
-
705
- **Example:**
706
-
707
- ```ts
708
- const uri =
709
- 'sui:pay?receiver=0x1234...&amount=1000000000&coinType=0x2::sui::SUI&nonce=abc123&registry=my-registry';
710
-
711
- const params = parsePaymentTransactionUri(uri);
712
-
713
- console.log(params.receiverAddress); // '0x1234...'
714
- console.log(params.amount); // 1000000000n (bigint)
715
- console.log(params.coinType); // '0x2::sui::SUI'
716
- console.log(params.nonce); // 'abc123'
717
- console.log(params.registryName); // 'my-registry'
718
-
719
- // Use parsed params to process a payment
720
- const tx = client.paymentKit.tx.processRegistryPayment({
721
- receiver: params.receiverAddress,
722
- amount: params.amount,
723
- coinType: params.coinType,
724
- nonce: params.nonce,
725
- registryName: params.registryName!,
726
- sender: senderAddress,
727
- });
728
- ```
729
-
730
- **Throws:**
731
-
732
- - `PaymentKitUriError` if URI doesn't start with `sui:pay?`
733
- - `PaymentKitUriError` if required parameters are missing
734
- - `PaymentKitUriError` if receiver address is invalid
735
- - `PaymentKitUriError` if coin type is invalid
736
- - `PaymentKitUriError` if nonce exceeds 36 characters
737
- - `PaymentKitUriError` if amount is not a positive number
738
-
739
- ### Future versions
740
-
741
- Check the
742
- [CHANGELOG](https://github.com/MystenLabs/ts-sdks/blob/main/packages/payment-kit/CHANGELOG.md) for
743
- updates.