github.com/cgcardona/r-subnet-evm@v0.1.5/warp/warp_client.go (about)

     1  // (c) 2023, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package warp
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  
    10  	"github.com/ava-labs/avalanchego/ids"
    11  	"github.com/cgcardona/r-subnet-evm/rpc"
    12  	"github.com/ethereum/go-ethereum/common/hexutil"
    13  )
    14  
    15  var _ WarpClient = (*warpClient)(nil)
    16  
    17  type WarpClient interface {
    18  	GetSignature(ctx context.Context, messageID ids.ID) ([]byte, error)
    19  	GetAggregateSignature(ctx context.Context, messageID ids.ID, quorumNum uint64) ([]byte, error)
    20  }
    21  
    22  // warpClient implementation for interacting with EVM [chain]
    23  type warpClient struct {
    24  	client *rpc.Client
    25  }
    26  
    27  // NewWarpClient returns a WarpClient for interacting with EVM [chain]
    28  func NewWarpClient(uri, chain string) (WarpClient, error) {
    29  	client, err := rpc.Dial(fmt.Sprintf("%s/ext/bc/%s/rpc", uri, chain))
    30  	if err != nil {
    31  		return nil, fmt.Errorf("failed to dial client. err: %w", err)
    32  	}
    33  	return &warpClient{
    34  		client: client,
    35  	}, nil
    36  }
    37  
    38  // GetSignature requests the BLS signature associated with a messageID
    39  func (c *warpClient) GetSignature(ctx context.Context, messageID ids.ID) ([]byte, error) {
    40  	var res hexutil.Bytes
    41  	err := c.client.CallContext(ctx, &res, "warp_getSignature", messageID)
    42  	if err != nil {
    43  		return nil, fmt.Errorf("call to warp_getSignature failed. err: %w", err)
    44  	}
    45  	return res, err
    46  }
    47  
    48  // GetAggregateSignature requests the aggregate signature associated with messageID
    49  func (c *warpClient) GetAggregateSignature(ctx context.Context, messageID ids.ID, quorumNum uint64) ([]byte, error) {
    50  	var res hexutil.Bytes
    51  	err := c.client.CallContext(ctx, &res, "warp_getAggregateSignature", messageID, quorumNum)
    52  	if err != nil {
    53  		return nil, fmt.Errorf("call to warp_getAggregateSignature failed. err: %w", err)
    54  	}
    55  	return res, err
    56  }