forked from lightninglabs/taproot-assets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
universe_rpc_registrar.go
183 lines (151 loc) · 4.83 KB
/
universe_rpc_registrar.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package taprootassets
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"time"
"github.com/lightninglabs/taproot-assets/fn"
"github.com/lightninglabs/taproot-assets/mssmt"
unirpc "github.com/lightninglabs/taproot-assets/taprpc/universerpc"
"github.com/lightninglabs/taproot-assets/universe"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
// RpcUniverseRegistrar is an implementation of the universe.Registrar interface
// that uses an RPC connection to target Universe.
type RpcUniverseRegistrar struct {
conn unirpc.UniverseClient
}
// NewRpcUniverseRegistrar creates a new RpcUniverseRegistrar instance that
// dials out to the target remote universe server address.
func NewRpcUniverseRegistrar(
serverAddr universe.ServerAddr) (universe.Registrar, error) {
conn, err := ConnectUniverse(serverAddr)
if err != nil {
return nil, fmt.Errorf("unable to connect to universe RPC "+
"server: %w", err)
}
return &RpcUniverseRegistrar{
conn: conn,
}, nil
}
// unmarshalIssuanceProof un-marshals an issuance proof response into a struct
// usable by the universe package.
func unmarshalIssuanceProof(uniKey *unirpc.UniverseKey,
proofResp *unirpc.AssetProofResponse) (*universe.Proof, error) {
leafKey, err := unmarshalLeafKey(uniKey.LeafKey)
if err != nil {
return nil, err
}
assetLeaf, err := unmarshalAssetLeaf(proofResp.AssetLeaf)
if err != nil {
return nil, err
}
var compressedProof mssmt.CompressedProof
err = compressedProof.Decode(
bytes.NewReader(proofResp.UniverseInclusionProof),
)
if err != nil {
return nil, err
}
inclusionProof, err := compressedProof.Decompress()
if err != nil {
return nil, err
}
return &universe.Proof{
LeafKey: leafKey,
UniverseRoot: mssmt.NewComputedBranch(
fn.ToArray[mssmt.NodeHash](
proofResp.UniverseRoot.MssmtRoot.RootHash,
),
uint64(proofResp.UniverseRoot.MssmtRoot.RootSum),
),
UniverseInclusionProof: inclusionProof,
Leaf: assetLeaf,
}, nil
}
// UpsertProofLeaf is an implementation of the universe.Registrar interface
// that uses a remote Universe server as the Registry instance.
func (r *RpcUniverseRegistrar) UpsertProofLeaf(ctx context.Context,
id universe.Identifier, key universe.LeafKey,
leaf *universe.Leaf) (*universe.Proof, error) {
uniID, err := MarshalUniID(id)
if err != nil {
return nil, err
}
// First, we'll parse the proofs and key into their RPC counterparts.
uniKey := &unirpc.UniverseKey{
Id: uniID,
LeafKey: marshalLeafKey(key),
}
assetLeaf, err := marshalAssetLeaf(ctx, nil, leaf)
if err != nil {
return nil, err
}
// With the RPC req prepared, we'll now send it off to the remote
// Universe serve as a new proof insertion request.
proofResp, err := r.conn.InsertProof(ctx, &unirpc.AssetProof{
Key: uniKey,
AssetLeaf: assetLeaf,
})
if err != nil {
return nil, err
}
// Finally, we'll map the response back into the Proof we expect
// as a response.
return unmarshalIssuanceProof(uniKey, proofResp)
}
// A compile time interface to ensure that RpcUniverseRegistrar implements the
// universe.Registrar interface.
var _ universe.Registrar = (*RpcUniverseRegistrar)(nil)
// CheckFederationServer attempts to connect to the target server and ensure
// that it is a valid federation server that isn't the local daemon.
func CheckFederationServer(localRuntimeID int64, connectTimeout time.Duration,
server universe.ServerAddr) error {
srvrLog.Debugf("Attempting to connect to federation server %v",
server.HostStr())
conn, err := ConnectUniverse(server)
if err != nil {
return fmt.Errorf("error connecting to server %v: %w",
server.HostStr(), err)
}
// We don't allow adding ourselves as a federation member.
ctxb := context.Background()
ctxt, cancel := context.WithTimeout(ctxb, connectTimeout)
defer cancel()
info, err := conn.Info(ctxt, &unirpc.InfoRequest{})
if err != nil {
return fmt.Errorf("error getting info from server %v: %w",
server.HostStr(), err)
}
if info.RuntimeId == localRuntimeID {
return fmt.Errorf("cannot add ourselves as a federation member")
}
return nil
}
// ConnectUniverse connects to a remote Universe server using the provided
// server address.
func ConnectUniverse(
serverAddr universe.ServerAddr) (unirpc.UniverseClient, error) {
// TODO(roasbeef): all info is authenticated, but also want to allow
// brontide connect as well, can avoid TLS certs
creds := credentials.NewTLS(&tls.Config{
InsecureSkipVerify: true,
})
// Create a dial options array.
opts := []grpc.DialOption{
grpc.WithTransportCredentials(creds),
grpc.WithDefaultCallOptions(MaxMsgReceiveSize),
}
uniAddr, err := serverAddr.Addr()
if err != nil {
return nil, err
}
rawConn, err := grpc.Dial(uniAddr.String(), opts...)
if err != nil {
return nil, fmt.Errorf("unable to connect to RPC "+
"server: %v", err)
}
return unirpc.NewUniverseClient(rawConn), nil
}