88 "sync/atomic"
99 "time"
1010
11+ "github.com/erc7824/nitrolite/clearnode/pkg/rpc"
1112 "github.com/ethereum/go-ethereum/common"
1213 "github.com/ethereum/go-ethereum/common/hexutil"
1314 "github.com/ethereum/go-ethereum/crypto"
@@ -57,37 +58,6 @@ type RPCResponse struct {
5758 Timestamp uint64 `json:"timestamp"`
5859}
5960
60- // Structured response types
61- type Asset struct {
62- Token string `json:"token"`
63- ChainID int `json:"chain_id"`
64- Symbol string `json:"symbol"`
65- Decimals int `json:"decimals"`
66- }
67-
68- type Balance struct {
69- Asset string `json:"asset"`
70- Amount decimal.Decimal `json:"amount"`
71- }
72-
73- type Allocation struct {
74- Asset string `json:"asset"`
75- Amount decimal.Decimal `json:"amount"`
76- }
77-
78- type TransferRequest struct {
79- Destination string `json:"destination"`
80- Allocations []Allocation `json:"allocations"`
81- }
82-
83- type TransferResult struct {
84- TransactionID string `json:"transaction_id"`
85- Amount decimal.Decimal `json:"amount"`
86- Asset string `json:"asset"`
87- Destination string `json:"destination"`
88- Status string `json:"status"`
89- }
90-
9161func NewClient (ownerPrivateKeyHex , signerPrivateKeyHex , clearnodeURL string , tokenSymbol string , standardTipAmount decimal.Decimal , minTransferCount int ) (* Client , error ) {
9262 // Clean the owner private key (remove 0x prefix if present)
9363 if len (ownerPrivateKeyHex ) > 2 && ownerPrivateKeyHex [:2 ] == "0x" {
@@ -156,24 +126,24 @@ func (c *Client) Authenticate() error {
156126 logger .Info ("Starting authentication flow" )
157127
158128 // Authentication parameters
159- appName := "Nitrolite Faucet"
129+ appName := "clearnode" // "clearnode" to allow unlimited allowances
160130 scope := "app.transfer"
161- expire := "36000000" // 10_000 hours
162- sessionKey := c .signerAddress // Use signer address as session key
163- application := common.Address {} // Zero address if no specific app
131+ expiresAt := uint64 ( time . Now (). Add ( 10000 * time . Hour ). Unix ()) // 10_000 hours in seconds
132+ sessionKey := c .signerAddress // Use signer address as session key
133+ applicationAddress := common.Address {} // Zero address if no specific app
164134
165- // Step 1: Send auth_request
166- authRequestData := map [string ]interface {}{
135+ // Step 1: Send auth_request using a map to match the local server's expectations
136+ // Note: The published rpc package types don't match the latest local server yet
137+ authRequest := map [string ]interface {}{
167138 "address" : c .ownerAddress .Hex (),
168139 "session_key" : sessionKey .Hex (),
169- "app_name" : appName ,
140+ "application" : appName ,
170141 "scope" : scope ,
171- "expire" : expire ,
172- "application" : application .Hex (),
173- "allowances" : []map [string ]interface {}{}, // Empty allowances for faucet
142+ "expires_at" : expiresAt ,
143+ "allowances" : []rpc.Allowance {}, // Use rpc.Allowance type for consistency
174144 }
175145
176- challengeResponse , err := c .sendRequest ("auth_request" , authRequestData )
146+ challengeResponse , err := c .sendRequest ("auth_request" , authRequest )
177147 if err != nil {
178148 return fmt .Errorf ("auth_request failed: %w" , err )
179149 }
@@ -186,15 +156,15 @@ func (c *Client) Authenticate() error {
186156 logger .Debugf ("Received challenge: %s" , challengeMessage )
187157
188158 // Step 2: Sign the challenge using EIP-712
189- allowances := []Allowance {} // Empty allowances for faucet
159+ allowances := []rpc. Allowance {} // Empty allowances for faucet
190160 signature , err := c .eip712Signer .SignChallenge (
191161 challengeMessage ,
192162 sessionKey ,
193163 appName ,
194164 allowances ,
195165 scope ,
196- application ,
197- expire ,
166+ applicationAddress ,
167+ expiresAt ,
198168 )
199169 if err != nil {
200170 return fmt .Errorf ("failed to sign challenge: %w" , err )
@@ -274,7 +244,7 @@ func (c *Client) Authenticate() error {
274244 }
275245}
276246
277- func (c * Client ) GetAssets () ([]Asset , error ) {
247+ func (c * Client ) GetAssets () ([]rpc. Asset , error ) {
278248 if err := c .EnsureConnected (); err != nil {
279249 return nil , err
280250 }
@@ -297,7 +267,7 @@ func (c *Client) GetAssets() ([]Asset, error) {
297267 return assets , nil
298268}
299269
300- func (c * Client ) GetFaucetBalance (tokenSymbol string ) (* Balance , error ) {
270+ func (c * Client ) GetFaucetBalance (tokenSymbol string ) (* rpc. LedgerBalance , error ) {
301271 if err := c .EnsureConnected (); err != nil {
302272 return nil , err
303273 }
@@ -320,13 +290,13 @@ func (c *Client) GetFaucetBalance(tokenSymbol string) (*Balance, error) {
320290 return balance , nil
321291}
322292
323- func (c * Client ) Transfer (destination , asset string , amount decimal.Decimal ) (* TransferResult , error ) {
324- transferData := TransferRequest {
293+ func (c * Client ) Transfer (destination , asset string , amount decimal.Decimal ) (* rpc. TransferResponse , error ) {
294+ transferData := rpc. TransferRequest {
325295 Destination : destination ,
326- Allocations : []Allocation {
296+ Allocations : []rpc. TransferAllocation {
327297 {
328- Asset : asset ,
329- Amount : amount ,
298+ AssetSymbol : asset ,
299+ Amount : amount ,
330300 },
331301 },
332302 }
@@ -338,7 +308,7 @@ func (c *Client) Transfer(destination, asset string, amount decimal.Decimal) (*T
338308 return nil , fmt .Errorf ("transfer failed: %w" , err )
339309 }
340310
341- logger .Info ("Transfer completed successfully" , " destination" , destination )
311+ logger .Infof ("Transfer completed successfully, destination: %s " , destination )
342312
343313 // Parse the response data
344314 result , err := c .parseTransferResult (response .Data , destination , asset , amount )
@@ -494,13 +464,13 @@ func (c *Client) signMessage(data interface{}) (string, error) {
494464
495465// Parsing helper methods
496466
497- func (c * Client ) parseAssets (data map [string ]interface {}) ([]Asset , error ) {
467+ func (c * Client ) parseAssets (data map [string ]interface {}) ([]rpc. Asset , error ) {
498468 assetsInterface , ok := data ["assets" ].([]interface {})
499469 if ! ok {
500470 return nil , fmt .Errorf ("invalid assets response format" )
501471 }
502472
503- var assets []Asset
473+ var assets []rpc. Asset
504474 for _ , assetInterface := range assetsInterface {
505475 assetData , ok := assetInterface .(map [string ]interface {})
506476 if ! ok {
@@ -512,18 +482,18 @@ func (c *Client) parseAssets(data map[string]interface{}) ([]Asset, error) {
512482 decimals , _ := assetData ["decimals" ].(float64 )
513483 chainID , _ := assetData ["chain_id" ].(float64 )
514484
515- assets = append (assets , Asset {
485+ assets = append (assets , rpc. Asset {
516486 Token : token ,
517- ChainID : int (chainID ),
487+ ChainID : uint32 (chainID ),
518488 Symbol : symbol ,
519- Decimals : int (decimals ),
489+ Decimals : uint8 (decimals ),
520490 })
521491 }
522492
523493 return assets , nil
524494}
525495
526- func (c * Client ) parseTokenBalance (data map [string ]interface {}, tokenSymbol string ) (* Balance , error ) {
496+ func (c * Client ) parseTokenBalance (data map [string ]interface {}, tokenSymbol string ) (* rpc. LedgerBalance , error ) {
527497 balancesInterface , ok := data ["ledger_balances" ].([]interface {})
528498 if ! ok {
529499 return nil , fmt .Errorf ("invalid ledger balances response format" )
@@ -550,49 +520,43 @@ func (c *Client) parseTokenBalance(data map[string]interface{}, tokenSymbol stri
550520 return nil , fmt .Errorf ("failed to parse balance amount: %w" , err )
551521 }
552522
553- return & Balance {
523+ return & rpc. LedgerBalance {
554524 Asset : asset ,
555525 Amount : amount ,
556526 }, nil
557527 }
558528
559- return & Balance {
529+ return & rpc. LedgerBalance {
560530 Asset : tokenSymbol ,
561531 Amount : decimal .Zero ,
562532 }, nil
563533}
564534
565- func (c * Client ) parseTransferResult (data map [string ]interface {}, destination , asset string , amount decimal.Decimal ) (* TransferResult , error ) {
535+ func (c * Client ) parseTransferResult (data map [string ]interface {}, destination , asset string , amount decimal.Decimal ) (* rpc.TransferResponse , error ) {
536+ // Parse the response data into RPC TransferResponse
537+ var response rpc.TransferResponse
538+
566539 transactionsInterface , ok := data ["transactions" ].([]interface {})
567540 if ! ok {
568541 return nil , fmt .Errorf ("invalid transfer response format" )
569542 }
570543
571- // Use the first transaction for the result
572- if len (transactionsInterface ) == 0 {
573- return & TransferResult {
574- TransactionID : "" ,
575- Amount : amount ,
576- Asset : asset ,
577- Destination : destination ,
578- Status : "completed" ,
579- }, nil
580- }
544+ // Parse transactions
545+ for _ , txInterface := range transactionsInterface {
546+ txDataRaw , err := json .Marshal (txInterface )
547+ if err != nil {
548+ continue
549+ }
581550
582- txData , ok := transactionsInterface [ 0 ].( map [ string ] interface {})
583- if ! ok {
584- return nil , fmt . Errorf ( "invalid transaction data format" )
585- }
551+ var tx rpc. LedgerTransaction
552+ if err := json . Unmarshal ( txDataRaw , & tx ); err != nil {
553+ continue
554+ }
586555
587- txID , _ := txData ["id" ].(string )
556+ response .Transactions = append (response .Transactions , tx )
557+ }
588558
589- return & TransferResult {
590- TransactionID : txID ,
591- Amount : amount ,
592- Asset : asset ,
593- Destination : destination ,
594- Status : "completed" ,
595- }, nil
559+ return & response , nil
596560}
597561
598562func (c * Client ) Close () error {
@@ -685,6 +649,10 @@ func (c *Client) EnsureOperational() error {
685649 return nil
686650}
687651
688- func (c * Client ) GetAddress () common.Address {
652+ func (c * Client ) GetOwnerAddress () common.Address {
653+ return c .ownerAddress
654+ }
655+
656+ func (c * Client ) GetSessionKeyAddress () common.Address {
689657 return c .signerAddress
690658}
0 commit comments