Skip to content

Commit

Permalink
api/www: add ltd and packedEncoding to tree response (#12)
Browse files Browse the repository at this point in the history
* api: use transaction when creating tree

* api: add .air.toml

* api: add ltd and packed to get tree response

* www: update docs
  • Loading branch information
malonehedges authored Aug 8, 2022
1 parent 92c593f commit 9e10a44
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 34 deletions.
32 changes: 32 additions & 0 deletions .air.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
root = "."
tmp_dir = "tmp"

[build]
bin = "./tmp/main"
cmd = "go build -o ./tmp/main ./cmd/api/main.go"
delay = 1000
exclude_dir = ["tmp", "contracts", "example", "www"]
exclude_file = []
exclude_regex = ["node_modules"]
exclude_unchanged = false
follow_symlink = false
full_bin = ""
include_dir = []
include_ext = ["go", "tpl", "tmpl", "html"]
kill_delay = "0s"
log = "build-errors.log"
send_interrupt = false
stop_on_error = true

[color]
app = ""
build = "yellow"
main = "magenta"
runner = "green"
watcher = "cyan"

[log]
time = false

[misc]
clean_on_exit = false
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tmp/
32 changes: 19 additions & 13 deletions api/db/queries/merkle.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions api/db/sql/queries/merkle.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ insert into merkle_trees (root, unhashed_leaves, ltd, packed)
values ($1, $2, $3, $4)
on conflict (root) do nothing;

-- name: SelectLeaves :one
select unhashed_leaves
-- name: SelectTree :one
select unhashed_leaves, ltd, packed
from merkle_trees
where root = $1;

Expand Down
38 changes: 32 additions & 6 deletions api/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,13 @@ func (jnb *jsonNullBool) UnmarshalJSON(d []byte) error {
return nil
}

func (jnb jsonNullBool) MarshalJSON() ([]byte, error) {
if jnb.Valid {
return json.Marshal(jnb.Bool)
}
return json.Marshal(nil)
}

type createTreeReq struct {
Leaves []hexutil.Bytes `json:"unhashedLeaves"`
Ltd []string `json:"leafTypeDescriptor"`
Expand All @@ -121,12 +128,21 @@ func (s *Server) CreateTree(w http.ResponseWriter, r *http.Request) {
return
}

tx, err := s.db.Begin(r.Context())
defer tx.Rollback(r.Context())
if err != nil {
s.sendJSONError(r, w, err, http.StatusInternalServerError, "Failed to start transaction")
return
}

q := s.dbq.WithTx(tx)

var leaves [][]byte
for i := range req.Leaves {
leaves = append(leaves, req.Leaves[i])
}
tree := merkle.New(leaves, merkle.SortPairs)
err := s.dbq.InsertTree(r.Context(), queries.InsertTreeParams{
err = q.InsertTree(r.Context(), queries.InsertTreeParams{
Root: tree.Root(),
UnhashedLeaves: leaves,
Ltd: req.Ltd,
Expand All @@ -143,7 +159,7 @@ func (s *Server) CreateTree(w http.ResponseWriter, r *http.Request) {
s.sendJSONError(r, w, nil, http.StatusBadRequest, "Must provide addresses that result in a proof")
return
}
err := s.dbq.InsertProof(r.Context(), queries.InsertProofParams{
err := q.InsertProof(r.Context(), queries.InsertProofParams{
Root: tree.Root(),
UnhashedLeaf: leaf,
Address: leaf2AddrBytes(leaf, req.Ltd, req.Packed.Bool),
Expand All @@ -155,6 +171,12 @@ func (s *Server) CreateTree(w http.ResponseWriter, r *http.Request) {
}
}

err = tx.Commit(r.Context())
if err != nil {
s.sendJSONError(r, w, err, http.StatusInternalServerError, "Failed to persist")
return
}

s.sendJSON(r, w, createTreeResp{
MerkleRoot: fmt.Sprintf("0x%s", hex.EncodeToString(tree.Root())),
})
Expand All @@ -163,6 +185,8 @@ func (s *Server) CreateTree(w http.ResponseWriter, r *http.Request) {
type getTreeResp struct {
UnhashedLeaves []hexutil.Bytes `json:"unhashedLeaves"`
LeafCount int `json:"leafCount"`
Ltd []string `json:"leafTypeDescriptor"`
Packed jsonNullBool `json:"packedEncoding"`
}

func (s *Server) GetTree(w http.ResponseWriter, r *http.Request) {
Expand All @@ -171,7 +195,7 @@ func (s *Server) GetTree(w http.ResponseWriter, r *http.Request) {
s.sendJSONError(r, w, nil, http.StatusBadRequest, "missing root")
return
}
leaves, err := s.dbq.SelectLeaves(r.Context(), common.FromHex(root))
row, err := s.dbq.SelectTree(r.Context(), common.FromHex(root))
if errors.Is(err, pgx.ErrNoRows) {
s.sendJSONError(r, w, err, http.StatusNotFound, "tree not found for root")
return
Expand All @@ -181,11 +205,13 @@ func (s *Server) GetTree(w http.ResponseWriter, r *http.Request) {
}

var l []hexutil.Bytes
for i := range leaves {
l = append(l, leaves[i])
for i := range row.UnhashedLeaves {
l = append(l, row.UnhashedLeaves[i])
}
s.sendJSON(r, w, getTreeResp{
UnhashedLeaves: l,
LeafCount: len(leaves),
LeafCount: len(row.UnhashedLeaves),
Ltd: row.Ltd,
Packed: jsonNullBool{row.Packed},
})
}
21 changes: 9 additions & 12 deletions example/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ const createTree = async (
}),
})

const encodedTree: { merkleRoot: string } = await encodedTreeRes.json()
return encodedTree
return await encodedTreeRes.json()
}

const getTree = async (
merkleRoot: string,
): Promise<{ unhashedLeaves: string[]; leafCount: number }> => {
): Promise<{
unhashedLeaves: string[]
leafCount: number
leafTypeDescriptor: string[] | null
packedEncoding: boolean | null
}> => {
const getTreeRes = await fetch(`${baseUrl}/api/v1/tree?root=${merkleRoot}`, {
method: 'GET',
headers: {
Expand All @@ -36,11 +40,7 @@ const getTree = async (
},
})

const tree: {
unhashedLeaves: string[]
leafCount: number
} = await getTreeRes.json()
return tree
return await getTreeRes.json()
}

const getProofForUnhashedLeaf = async (
Expand All @@ -58,10 +58,7 @@ const getProofForUnhashedLeaf = async (
},
)

const proof: {
proof: string[]
} = await proofRes.json()
return proof
return await proofRes.json()
}

/** Using this endpoint is discouraged. When possible, pass `unhashedLeaf` instead */
Expand Down
6 changes: 5 additions & 1 deletion www/pages/docs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ GET /api/v1/tree?root={root}
"0x0000000000000000000000000000000000000001",
"0x0000000000000000000000000000000000000002"
],
"leafCount": 400
"leafCount": 2,
// in general you can ignore the two following fields
"leafTypeDescriptor": null, // or an array of solidity types
"packedEncoding": null // or a boolean value
}
`.trim()

Expand Down

1 comment on commit 9e10a44

@vercel
Copy link

@vercel vercel bot commented on 9e10a44 Aug 8, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

allowlist – ./

allowlist.context.wtf
allowlist-git-main.context.wtf

Please sign in to comment.