Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add proof support #54

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions cmd/proof/mpt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package proof

import (
"fmt"
core2 "github.com/Zilliqa/gozilliqa-sdk/core"
"github.com/Zilliqa/gozilliqa-sdk/mpt"
"github.com/Zilliqa/gozilliqa-sdk/provider"
"github.com/Zilliqa/gozilliqa-sdk/util"
"github.com/spf13/cobra"
"strings"
wallet2 "zli/cmd/wallet"
"zli/core"
)

var wallet *core.Wallet
var api string
var contractAddress string
var key string
var block string

func init() {
mptCmd.Flags().StringVarP(&contractAddress, "contract", "c", "", "smart contract address")
mptCmd.Flags().StringVarP(&api, "api", "u", "", "api endpoint")
mptCmd.Flags().StringVarP(&key, "key", "k", "", "for now only support simple key without nesting")
mptCmd.Flags().StringVarP(&block, "block", "b", "", "block num")
ProofCmd.AddCommand(mptCmd)
}

var mptCmd = &cobra.Command{
Use: "mpt",
Short: "test mpt verification",
Long: "test mpt verification",
PreRun: func(cmd *cobra.Command, args []string) {
home := core.UserHomeDir()
w, err := core.LoadFromFile(home + "/" + wallet2.DefaultConfigName)
if err != nil {
fmt.Println("cannot load wallet = ", err.Error())
}
wallet = w
},
Run: func(cmd *cobra.Command, args []string) {
var a string
if api != "" {
a = api
} else {
a = wallet.API
}
p := provider.NewProvider(a)
contractAddress = strings.TrimPrefix(contractAddress, "0x")
storageKey := core2.GenerateStorageKey(contractAddress, key, []string{})
hashedStorageKey := util.Sha256(storageKey)

proofResult, err := p.GetStateProof(contractAddress, util.EncodeHex(hashedStorageKey), &block)
if err != nil {
fmt.Println("get state proof error: " + err.Error())
return
}

if proofResult == nil {
fmt.Println("proof is nil")
return
}

txBlock, err := p.GetTxBlock(block)
if err != nil {
fmt.Println("get tx block error: " + err.Error())
return
}

if proofResult.AccountProof == nil {
fmt.Println("account proof is nil")
return
}

if proofResult.StateProof == nil {
fmt.Println("state proof is nil")
return
}

var proof [][]byte
for _, p := range proofResult.AccountProof {
bytes := util.DecodeHex(p)
proof = append(proof, bytes)
}

db := mpt.NewFromProof(proof)
stateRoot := util.DecodeHex(txBlock.Header.StateRootHash)
value, err := mpt.Verify([]byte(contractAddress), db, stateRoot)
if err != nil {
fmt.Println("account proof failed, err: ", err.Error())
return
}

accountBase, _ := core2.AccountBaseFromBytes(value)

var proof2 [][]byte
for _, p := range proofResult.StateProof {
bytes := util.DecodeHex(p)
proof2 = append(proof2, bytes)
}

db3 := mpt.NewFromProof(proof2)
value2, err2 := mpt.Verify([]byte((util.EncodeHex(hashedStorageKey))), db3, accountBase.StorageRoot)
if err2 != nil {
fmt.Println("state proof failed, err: " + err2.Error())
return
}

fmt.Println(string(value2))

},
}
12 changes: 12 additions & 0 deletions cmd/proof/proof.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package proof

import "github.com/spf13/cobra"

var ProofCmd = &cobra.Command{
Use: "proof",
Short: "proof tool",
Long: "proof tool",
Run: func(cmd *cobra.Command, args []string) {

},
}
2 changes: 2 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"zli/cmd/account"
"zli/cmd/contract"
"zli/cmd/converter"
"zli/cmd/proof"
"zli/cmd/rpc"
"zli/cmd/staking"
"zli/cmd/testsuite"
Expand All @@ -40,6 +41,7 @@ func init() {
RootCmd.AddCommand(testsuite.TestSuite)
RootCmd.AddCommand(converter.ConverterCmd)
RootCmd.AddCommand(staking.StakingCmd)
RootCmd.AddCommand(proof.ProofCmd)
}

var RootCmd = &cobra.Command{
Expand Down
6 changes: 1 addition & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,11 @@ module zli
go 1.12

require (
github.com/Zilliqa/gozilliqa-sdk v1.2.0
github.com/golang/protobuf v1.3.2 // indirect
github.com/Zilliqa/gozilliqa-sdk v1.2.1-0.20210406170702-178fd8166bae
github.com/howeyc/gopass v0.0.0-20170109162249-bf9dde6d0d2c
github.com/kr/pretty v0.1.0 // indirect
github.com/satori/go.uuid v1.2.0 // indirect
github.com/spf13/cobra v0.0.5
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect
golang.org/x/net v0.0.0-20190813141303-74dc4d7220e7 // indirect
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a // indirect
golang.org/x/text v0.3.2 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)
Loading