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