kentucky-signer-viem 0.1.3 → 0.1.5

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,6 +1,6 @@
1
1
  import * as react_jsx_runtime from 'react/jsx-runtime';
2
2
  import { ReactNode } from 'react';
3
- import { Address, LocalAccount, Chain, WalletClient, Transport } from 'viem';
3
+ import { Address, LocalAccount, Hex, Chain, WalletClient, Transport } from 'viem';
4
4
 
5
5
  /**
6
6
  * Authenticated session with Kentucky Signer
@@ -29,16 +29,79 @@ interface TwoFactorCodes {
29
29
  /** PIN code */
30
30
  pin?: string;
31
31
  }
32
+ /**
33
+ * Parameters for signing an EIP-7702 authorization
34
+ */
35
+ interface SignAuthorizationParameters {
36
+ /** The contract address to delegate to */
37
+ contractAddress: Address;
38
+ /** Chain ID (0 for all chains, defaults to client chain) */
39
+ chainId?: number;
40
+ /** Nonce for the authorization (defaults to account's current nonce) */
41
+ nonce?: bigint;
42
+ /**
43
+ * Whether the authorization will be executed by the signer themselves.
44
+ * If 'self', the nonce in the authorization will be incremented by 1
45
+ * over the transaction nonce.
46
+ */
47
+ executor?: 'self';
48
+ }
49
+ /**
50
+ * Signed EIP-7702 authorization
51
+ * Compatible with viem's SignedAuthorizationList
52
+ */
53
+ interface SignedAuthorization {
54
+ /** Chain ID (0 for all chains) */
55
+ chainId: number;
56
+ /** Contract address to delegate to */
57
+ contractAddress: Address;
58
+ /** Nonce for the authorization */
59
+ nonce: bigint;
60
+ /** Recovery identifier (0 or 1) */
61
+ yParity: number;
62
+ /** Signature r component */
63
+ r: Hex;
64
+ /** Signature s component */
65
+ s: Hex;
66
+ }
32
67
  /**
33
68
  * Extended account type with Kentucky Signer specific properties
34
69
  */
35
- interface KentuckySignerAccount extends LocalAccount<'kentuckySigner'> {
70
+ interface KentuckySignerAccount extends Omit<LocalAccount<'kentuckySigner'>, 'signAuthorization'> {
36
71
  /** Account ID */
37
72
  accountId: string;
38
73
  /** Current session */
39
74
  session: AuthSession;
40
75
  /** Update the session (e.g., after refresh) */
41
76
  updateSession: (session: AuthSession) => void;
77
+ /**
78
+ * Sign an EIP-7702 authorization to delegate code to this account
79
+ *
80
+ * @param params - Authorization parameters
81
+ * @param nonce - Current account nonce (required if params.nonce not specified)
82
+ * @returns Signed authorization compatible with viem's authorizationList
83
+ *
84
+ * @example
85
+ * ```typescript
86
+ * // Get current nonce
87
+ * const nonce = await publicClient.getTransactionCount({ address: account.address })
88
+ *
89
+ * // Sign authorization
90
+ * const authorization = await account.sign7702Authorization({
91
+ * contractAddress: '0x69007702764179f14F51cdce752f4f775d74E139', // Alchemy MA v2
92
+ * chainId: 1,
93
+ * executor: 'self', // Account will execute the tx
94
+ * }, nonce)
95
+ *
96
+ * // Send EIP-7702 transaction
97
+ * const hash = await walletClient.sendTransaction({
98
+ * authorizationList: [authorization],
99
+ * to: account.address,
100
+ * data: initializeCalldata,
101
+ * })
102
+ * ```
103
+ */
104
+ sign7702Authorization: (params: SignAuthorizationParameters, nonce: bigint) => Promise<SignedAuthorization>;
42
105
  }
43
106
 
44
107
  /**
@@ -343,4 +406,403 @@ declare function useIsReady(): boolean;
343
406
  */
344
407
  declare function useAddress(): `0x${string}` | undefined;
345
408
 
346
- export { type KentuckySignerActions, type KentuckySignerContextValue, KentuckySignerProvider, type KentuckySignerProviderProps, type KentuckySignerState, type TwoFactorPromptState, type UseWalletClientOptions, useAddress, useIsReady, useKentuckySigner, useKentuckySignerAccount, useKentuckySignerContext, usePasskeyAuth, useSignMessage, useSignTypedData, useWalletClient };
409
+ /**
410
+ * Execution intent to be signed by the EOA owner
411
+ */
412
+ interface ExecutionIntent {
413
+ /** Replay protection nonce */
414
+ nonce: bigint;
415
+ /** Expiration timestamp (unix seconds) */
416
+ deadline: bigint;
417
+ /** Contract to call */
418
+ target: Address;
419
+ /** ETH value to send */
420
+ value: bigint;
421
+ /** Calldata for the call */
422
+ data: Hex;
423
+ }
424
+ /**
425
+ * Signed execution intent
426
+ */
427
+ interface SignedIntent {
428
+ /** The execution intent */
429
+ intent: ExecutionIntent;
430
+ /** Owner's signature on the intent */
431
+ signature: Hex;
432
+ }
433
+ /**
434
+ * Parameters for creating an execution intent
435
+ */
436
+ interface CreateIntentParams {
437
+ /** Account nonce (fetch from contract) */
438
+ nonce: bigint;
439
+ /** Expiration timestamp (unix seconds) */
440
+ deadline?: bigint;
441
+ /** Contract to call */
442
+ target: Address;
443
+ /** ETH value to send */
444
+ value?: bigint;
445
+ /** Calldata for the call */
446
+ data?: Hex;
447
+ }
448
+ /**
449
+ * Create an execution intent
450
+ *
451
+ * @param params - Intent parameters
452
+ * @returns Execution intent
453
+ *
454
+ * @example
455
+ * ```typescript
456
+ * const intent = createExecutionIntent({
457
+ * nonce: 0n,
458
+ * target: '0x...',
459
+ * value: parseEther('0.1'),
460
+ * data: '0x',
461
+ * })
462
+ * ```
463
+ */
464
+ declare function createExecutionIntent(params: CreateIntentParams): ExecutionIntent;
465
+ /**
466
+ * Sign an execution intent using a Kentucky Signer account
467
+ *
468
+ * @param account - Kentucky Signer account
469
+ * @param intent - The execution intent to sign
470
+ * @returns Signed intent
471
+ *
472
+ * @example
473
+ * ```typescript
474
+ * const account = useKentuckySignerAccount()
475
+ * const intent = createExecutionIntent({ nonce: 0n, target: '0x...' })
476
+ * const signedIntent = await signIntent(account, intent)
477
+ * ```
478
+ */
479
+ declare function signIntent(account: KentuckySignerAccount, intent: ExecutionIntent): Promise<SignedIntent>;
480
+
481
+ /**
482
+ * Payment mode for relaying
483
+ */
484
+ type PaymentMode = 'sponsored' | {
485
+ token: Address;
486
+ };
487
+ /**
488
+ * EIP-7702 Authorization for gasless onboarding
489
+ * When provided to relay(), allows users with 0 ETH to delegate their EOA
490
+ * to the smart account delegate in the same transaction as execution
491
+ */
492
+ interface Authorization7702 {
493
+ /** Chain ID (0 for all chains) */
494
+ chainId: number;
495
+ /** Contract address to delegate to */
496
+ contractAddress: Address;
497
+ /** Nonce for the authorization */
498
+ nonce: bigint;
499
+ /** Recovery identifier (0 or 1) */
500
+ yParity: number;
501
+ /** Signature r component */
502
+ r: Hex;
503
+ /** Signature s component */
504
+ s: Hex;
505
+ }
506
+ /**
507
+ * Token payment option returned by estimate
508
+ */
509
+ interface TokenOption {
510
+ /** Token address */
511
+ token: Address;
512
+ /** Token symbol */
513
+ symbol: string;
514
+ /** Estimated fee in token units */
515
+ estimatedFee: string;
516
+ /** Fee percentage (e.g., 5 = 5%) */
517
+ feePercentage: number;
518
+ }
519
+ /**
520
+ * Gas estimate response
521
+ */
522
+ interface EstimateResponse {
523
+ /** Estimated gas units */
524
+ gasEstimate: string;
525
+ /** Estimated gas cost in wei */
526
+ gasCostWei: string;
527
+ /** Whether sponsored mode is available */
528
+ sponsoredAvailable: boolean;
529
+ /** Available token payment options */
530
+ tokenOptions: TokenOption[];
531
+ }
532
+ /**
533
+ * Relay response
534
+ */
535
+ interface RelayResponse {
536
+ /** Whether the relay was successful */
537
+ success: boolean;
538
+ /** Transaction hash if successful */
539
+ txHash?: Hex;
540
+ /** Error message if failed */
541
+ error?: string;
542
+ }
543
+ /**
544
+ * Transaction status
545
+ */
546
+ type TransactionStatus = 'pending' | 'confirmed' | 'failed';
547
+ /**
548
+ * Status response
549
+ */
550
+ interface StatusResponse {
551
+ /** Current status */
552
+ status: TransactionStatus;
553
+ /** Transaction hash */
554
+ txHash: Hex;
555
+ /** Block number if confirmed */
556
+ blockNumber?: number;
557
+ /** Gas used if confirmed */
558
+ gasUsed?: string;
559
+ /** Token amount paid if applicable */
560
+ tokenPaid?: string;
561
+ }
562
+ /**
563
+ * Relayer client options
564
+ */
565
+ interface RelayerClientOptions {
566
+ /** Relayer API base URL */
567
+ baseUrl: string;
568
+ /** Request timeout in ms (default: 30000) */
569
+ timeout?: number;
570
+ }
571
+ /**
572
+ * Client for interacting with the Kentucky Signer Relayer API
573
+ *
574
+ * @example
575
+ * ```typescript
576
+ * const relayer = new RelayerClient({ baseUrl: 'https://relayer.example.com' })
577
+ *
578
+ * // Get nonce
579
+ * const nonce = await relayer.getNonce(42161, accountAddress)
580
+ *
581
+ * // Create and sign intent
582
+ * const intent = createExecutionIntent({ nonce, target: '0x...' })
583
+ * const signed = await signIntent(account, intent)
584
+ *
585
+ * // Estimate fees
586
+ * const estimate = await relayer.estimate(42161, accountAddress, intent)
587
+ *
588
+ * // Relay transaction
589
+ * const result = await relayer.relay(42161, accountAddress, signed, 'sponsored')
590
+ * console.log('TX Hash:', result.txHash)
591
+ *
592
+ * // Check status
593
+ * const status = await relayer.getStatus(42161, result.txHash!)
594
+ * ```
595
+ */
596
+ declare class RelayerClient {
597
+ private baseUrl;
598
+ private timeout;
599
+ constructor(options: RelayerClientOptions);
600
+ /**
601
+ * Check if the relayer is healthy
602
+ */
603
+ health(): Promise<{
604
+ status: string;
605
+ relayer: Address;
606
+ timestamp: string;
607
+ }>;
608
+ /**
609
+ * Get the current nonce for an account
610
+ *
611
+ * @param chainId - Chain ID
612
+ * @param address - Account address
613
+ * @returns Current nonce as bigint
614
+ */
615
+ getNonce(chainId: number, address: Address): Promise<bigint>;
616
+ /**
617
+ * Estimate gas and fees for an intent
618
+ *
619
+ * @param chainId - Chain ID
620
+ * @param accountAddress - Account address (the delegated EOA)
621
+ * @param intent - Execution intent
622
+ * @returns Estimate response
623
+ */
624
+ estimate(chainId: number, accountAddress: Address, intent: ExecutionIntent): Promise<EstimateResponse>;
625
+ /**
626
+ * Relay a signed intent
627
+ *
628
+ * @param chainId - Chain ID
629
+ * @param accountAddress - Account address (the delegated EOA)
630
+ * @param signedIntent - Signed execution intent
631
+ * @param paymentMode - Payment mode ('sponsored' or { token: Address })
632
+ * @param authorization - Optional EIP-7702 authorization for gasless onboarding
633
+ * @returns Relay response with transaction hash
634
+ *
635
+ * @example Gasless onboarding (delegate + execute in one tx)
636
+ * ```typescript
637
+ * // Get current nonce for authorization
638
+ * const txNonce = await publicClient.getTransactionCount({ address: accountAddress })
639
+ *
640
+ * // Sign EIP-7702 authorization
641
+ * const authorization = await account.sign7702Authorization({
642
+ * contractAddress: delegateAddress,
643
+ * chainId: 42161,
644
+ * }, txNonce)
645
+ *
646
+ * // Relay with authorization
647
+ * const result = await relayer.relay(
648
+ * 42161,
649
+ * accountAddress,
650
+ * signedIntent,
651
+ * 'sponsored',
652
+ * authorization
653
+ * )
654
+ * ```
655
+ */
656
+ relay(chainId: number, accountAddress: Address, signedIntent: SignedIntent, paymentMode: PaymentMode, authorization?: Authorization7702): Promise<RelayResponse>;
657
+ /**
658
+ * Get transaction status
659
+ *
660
+ * @param chainId - Chain ID
661
+ * @param txHash - Transaction hash
662
+ * @returns Status response
663
+ */
664
+ getStatus(chainId: number, txHash: Hex): Promise<StatusResponse>;
665
+ /**
666
+ * Make a fetch request to the relayer API
667
+ */
668
+ private fetch;
669
+ }
670
+ /**
671
+ * Create a relayer client
672
+ *
673
+ * @param baseUrl - Relayer API base URL
674
+ * @returns Relayer client instance
675
+ */
676
+ declare function createRelayerClient(baseUrl: string): RelayerClient;
677
+
678
+ /**
679
+ * Hook for relaying intents through the relayer
680
+ */
681
+ interface UseRelayIntentResult {
682
+ /** Relay a signed intent */
683
+ relay: (chainId: number, accountAddress: Address, signedIntent: SignedIntent, paymentMode: PaymentMode, authorization?: Authorization7702) => Promise<RelayResponse>;
684
+ /** Whether a relay is in progress */
685
+ isRelaying: boolean;
686
+ /** Last relay response */
687
+ response: RelayResponse | null;
688
+ /** Last error */
689
+ error: Error | null;
690
+ /** Reset state */
691
+ reset: () => void;
692
+ }
693
+ /**
694
+ * Hook for relaying signed intents through the Kentucky Signer Relayer
695
+ *
696
+ * @param client - Relayer client instance
697
+ * @returns Relay functions and state
698
+ *
699
+ * @example
700
+ * ```tsx
701
+ * const relayer = useRelayerClient('https://relayer.example.com')
702
+ * const { relay, isRelaying, response, error } = useRelayIntent(relayer)
703
+ *
704
+ * const handleSubmit = async () => {
705
+ * const result = await relay(42161, accountAddress, signedIntent, 'sponsored')
706
+ * if (result.success) {
707
+ * console.log('TX:', result.txHash)
708
+ * }
709
+ * }
710
+ * ```
711
+ */
712
+ declare function useRelayIntent(client: RelayerClient): UseRelayIntentResult;
713
+ /**
714
+ * Hook for tracking transaction status
715
+ */
716
+ interface UseTransactionStatusResult {
717
+ /** Current transaction status */
718
+ status: TransactionStatus | null;
719
+ /** Full status response */
720
+ statusResponse: StatusResponse | null;
721
+ /** Whether status is being fetched */
722
+ isLoading: boolean;
723
+ /** Last error */
724
+ error: Error | null;
725
+ /** Manually refresh status */
726
+ refresh: () => void;
727
+ }
728
+ /**
729
+ * Hook for tracking transaction status with polling
730
+ *
731
+ * @param client - Relayer client instance
732
+ * @param chainId - Chain ID
733
+ * @param txHash - Transaction hash to track (null to disable)
734
+ * @param pollInterval - Polling interval in ms (default: 3000)
735
+ * @returns Transaction status and state
736
+ *
737
+ * @example
738
+ * ```tsx
739
+ * const { status, statusResponse, isLoading } = useTransactionStatus(
740
+ * relayer,
741
+ * 42161,
742
+ * txHash
743
+ * )
744
+ *
745
+ * if (status === 'confirmed') {
746
+ * console.log('Transaction confirmed in block:', statusResponse?.blockNumber)
747
+ * }
748
+ * ```
749
+ */
750
+ declare function useTransactionStatus(client: RelayerClient, chainId: number, txHash: Hex | null, pollInterval?: number): UseTransactionStatusResult;
751
+ /**
752
+ * Hook for estimating gas and fees
753
+ */
754
+ interface UseEstimateResult {
755
+ /** Estimate gas and fees */
756
+ estimate: (chainId: number, accountAddress: Address, intent: ExecutionIntent) => Promise<EstimateResponse | null>;
757
+ /** Last estimate response */
758
+ estimateResponse: EstimateResponse | null;
759
+ /** Whether estimate is in progress */
760
+ isEstimating: boolean;
761
+ /** Last error */
762
+ error: Error | null;
763
+ }
764
+ /**
765
+ * Hook for estimating gas and token fees
766
+ *
767
+ * @param client - Relayer client instance
768
+ * @returns Estimate functions and state
769
+ *
770
+ * @example
771
+ * ```tsx
772
+ * const { estimate, estimateResponse, isEstimating } = useEstimate(relayer)
773
+ *
774
+ * useEffect(() => {
775
+ * estimate(42161, accountAddress, intent)
776
+ * }, [intent])
777
+ *
778
+ * if (estimateResponse) {
779
+ * console.log('Gas:', estimateResponse.gasEstimate)
780
+ * console.log('Tokens:', estimateResponse.tokenOptions)
781
+ * }
782
+ * ```
783
+ */
784
+ declare function useEstimate(client: RelayerClient): UseEstimateResult;
785
+ /**
786
+ * Hook for fetching account nonce
787
+ */
788
+ interface UseNonceResult {
789
+ /** Current nonce */
790
+ nonce: bigint | null;
791
+ /** Whether nonce is being fetched */
792
+ isLoading: boolean;
793
+ /** Last error */
794
+ error: Error | null;
795
+ /** Refresh nonce */
796
+ refresh: () => void;
797
+ }
798
+ /**
799
+ * Hook for fetching account nonce
800
+ *
801
+ * @param client - Relayer client instance
802
+ * @param chainId - Chain ID
803
+ * @param address - Account address (null to disable)
804
+ * @returns Nonce and state
805
+ */
806
+ declare function useNonce(client: RelayerClient, chainId: number, address: Address | null): UseNonceResult;
807
+
808
+ export { type Authorization7702, type EstimateResponse, type ExecutionIntent, type KentuckySignerActions, type KentuckySignerContextValue, KentuckySignerProvider, type KentuckySignerProviderProps, type KentuckySignerState, type PaymentMode, type RelayResponse, RelayerClient, type SignedIntent, type StatusResponse, type TokenOption, type TransactionStatus, type TwoFactorPromptState, type UseEstimateResult, type UseNonceResult, type UseRelayIntentResult, type UseTransactionStatusResult, type UseWalletClientOptions, createExecutionIntent, createRelayerClient, signIntent, useAddress, useEstimate, useIsReady, useKentuckySigner, useKentuckySignerAccount, useKentuckySignerContext, useNonce, usePasskeyAuth, useRelayIntent, useSignMessage, useSignTypedData, useTransactionStatus, useWalletClient };