Skip to content

Commit 52ff3a8

Browse files
authored
client: add GetProofFromAddr function (#64)
1 parent a201d7d commit 52ff3a8

File tree

2 files changed

+43
-8
lines changed

2 files changed

+43
-8
lines changed

clients/go/lanyard/client.go

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,14 @@ type ProofResponse struct {
208208
Proof []hexutil.Bytes `json:"proof"`
209209
}
210210

211-
// If the tree has been published to Lanyard, GetProof will
212-
// return the proof associated with an unHashedLeaf.
213-
// This endpoint will return ErrNotFound if the tree
214-
// associated with the root has not been published.
211+
// If the tree has been published to Lanyard,
212+
// GetProofFromLeaf will return the proof associated
213+
// with an unhashedLeaf. This endpoint will return
214+
// ErrNotFound if the tree associated with the root
215+
// has not been published.
215216
func (c *Client) GetProofFromLeaf(
216217
ctx context.Context,
217-
root hexutil.Bytes,
218-
unhashedLeaf hexutil.Bytes,
218+
root, unhashedLeaf hexutil.Bytes,
219219
) (*ProofResponse, error) {
220220
resp := &ProofResponse{}
221221

@@ -234,6 +234,32 @@ func (c *Client) GetProofFromLeaf(
234234
return resp, nil
235235
}
236236

237+
// If the tree has been published to Lanyard,
238+
// GetProofFromAddr will return the proof associated
239+
// with an address. This endpoint will return
240+
// ErrNotFound if the tree associated with the root
241+
// has not been published.
242+
func (c *Client) GetProofFromAddr(
243+
ctx context.Context,
244+
root, addr hexutil.Bytes,
245+
) (*ProofResponse, error) {
246+
resp := &ProofResponse{}
247+
248+
err := c.sendRequest(
249+
ctx, http.MethodGet,
250+
fmt.Sprintf("/proof?root=%s&address=%s",
251+
root.String(), addr.String(),
252+
),
253+
nil, resp,
254+
)
255+
256+
if err != nil {
257+
return nil, err
258+
}
259+
260+
return resp, nil
261+
}
262+
237263
type RootResponse struct {
238264
Root hexutil.Bytes `json:"root"`
239265
}

clients/go/lanyard/client_integration_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ func init() {
2929
}
3030
}
3131

32-
const basicRoot = "0xa7a6b1cb6d12308ec4818baac3413fafa9e8b52cdcd79252fa9e29c9a2f8aff1"
32+
const (
33+
basicRoot = "0xa7a6b1cb6d12308ec4818baac3413fafa9e8b52cdcd79252fa9e29c9a2f8aff1"
34+
typedRoot = "0x6306f03ad6ae2ffeca080333a0a6828669192f5f8b61f70738bfe8ceb7e0a434"
35+
)
3336

3437
func TestBasicMerkleTree(t *testing.T) {
3538
tree, err := client.CreateTree(context.Background(), basicMerkle)
@@ -57,7 +60,6 @@ func TestCreateTypedTree(t *testing.T) {
5760
t.Fatal(err)
5861
}
5962

60-
const typedRoot = "0x6306f03ad6ae2ffeca080333a0a6828669192f5f8b61f70738bfe8ceb7e0a434"
6163
if tree.MerkleRoot.String() != typedRoot {
6264
t.Fatalf("expected %s, got %s", typedRoot, tree.MerkleRoot.String())
6365
}
@@ -77,6 +79,13 @@ func TestBasicMerkleProof404(t *testing.T) {
7779
}
7880
}
7981

82+
func TestGetProofFromAddr(t *testing.T) {
83+
_, err := client.GetProofFromAddr(context.Background(), hexutil.MustDecode(typedRoot), hexutil.MustDecode("0x0000000000000000000000000000000000000001"))
84+
if err != nil {
85+
t.Fatal(err)
86+
}
87+
}
88+
8089
func TestGetRootFromProof(t *testing.T) {
8190
p, err := client.GetProofFromLeaf(context.Background(), hexutil.MustDecode(basicRoot), basicMerkle[0])
8291

0 commit comments

Comments
 (0)