Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
453edc5
commit 2300537
Showing
5 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package accounts | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/gorilla/mux" | ||
"github.com/pkg/errors" | ||
"github.com/vechain/thor/api/utils" | ||
"github.com/vechain/thor/chain" | ||
"github.com/vechain/thor/thor" | ||
) | ||
|
||
type merkleProof struct { | ||
StateRoot string `json:"stateRoot"` | ||
Account string `json:"account"` | ||
Slot string `json:"slot"` | ||
Value string `json:"value"` | ||
AccountProof []string `json:"accountProof"` | ||
StorageProof []string `json:"storageProof"` | ||
} | ||
|
||
func (a *Accounts) proofStorage(addr thor.Address, slot thor.Bytes32, summary *chain.BlockSummary) (p *merkleProof, err error) { | ||
state := a.stater. | ||
NewState(summary.Header.StateRoot(), summary.Header.Number(), summary.Conflicts, summary.SteadyNum) | ||
|
||
proof, storageProofBytes, value, err := state.ProveStorage(addr, slot) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var accountProof []string | ||
for _, p := range proof { | ||
accountProof = append(accountProof, hexutil.Encode(p)) | ||
} | ||
|
||
var storageProof []string | ||
for _, p := range storageProofBytes { | ||
storageProof = append(storageProof, hexutil.Encode(p)) | ||
} | ||
|
||
return &merkleProof{ | ||
StateRoot: summary.Header.StateRoot().String(), | ||
Account: hexutil.Encode(addr[:]), | ||
Slot: hexutil.Encode(slot[:]), | ||
Value: hexutil.Encode(value[:]), | ||
AccountProof: accountProof, | ||
StorageProof: storageProof, | ||
}, nil | ||
} | ||
|
||
func (a *Accounts) handleProofStorage(w http.ResponseWriter, req *http.Request) error { | ||
hexAddr := mux.Vars(req)["address"] | ||
addr, err := thor.ParseAddress(hexAddr) | ||
if err != nil { | ||
return utils.BadRequest(errors.WithMessage(err, "address")) | ||
} | ||
slot, err := thor.ParseBytes32(mux.Vars(req)["key"]) | ||
if err != nil { | ||
return utils.BadRequest(errors.WithMessage(err, "storage")) | ||
} | ||
summary, err := a.handleRevision(req.URL.Query().Get("revision")) | ||
if err != nil { | ||
return err | ||
} | ||
proof, err := a.proofStorage(addr, slot, summary) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return utils.WriteJSON(w, proof) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters