@@ -508,6 +508,72 @@ func (s *PublicBlockChainAPI) GetBalance(ctx context.Context, address common.Add
508508 return (* hexutil .Big )(state .GetBalance (address )), state .Error ()
509509}
510510
511+ // Result structs for GetProof
512+ type AccountResult struct {
513+ Address common.Address `json:"address"`
514+ AccountProof []string `json:"accountProof"`
515+ Balance * hexutil.Big `json:"balance"`
516+ CodeHash common.Hash `json:"codeHash"`
517+ Nonce hexutil.Uint64 `json:"nonce"`
518+ StorageHash common.Hash `json:"storageHash"`
519+ StorageProof []StorageResult `json:"storageProof"`
520+ }
521+ type StorageResult struct {
522+ Key string `json:"key"`
523+ Value * hexutil.Big `json:"value"`
524+ Proof []string `json:"proof"`
525+ }
526+
527+ // GetProof returns the Merkle-proof for a given account and optionally some storage keys.
528+ func (s * PublicBlockChainAPI ) GetProof (ctx context.Context , address common.Address , storageKeys []string , blockNr rpc.BlockNumber ) (* AccountResult , error ) {
529+ state , _ , err := s .b .StateAndHeaderByNumber (ctx , blockNr )
530+ if state == nil || err != nil {
531+ return nil , err
532+ }
533+
534+ storageTrie := state .StorageTrie (address )
535+ storageHash := types .EmptyRootHash
536+ codeHash := state .GetCodeHash (address )
537+ storageProof := make ([]StorageResult , len (storageKeys ))
538+
539+ // if we have a storageTrie, (which means the account exists), we can update the storagehash
540+ if storageTrie != nil {
541+ storageHash = storageTrie .Hash ()
542+ } else {
543+ // no storageTrie means the account does not exist, so the codeHash is the hash of an empty bytearray.
544+ codeHash = crypto .Keccak256Hash (nil )
545+ }
546+
547+ // create the proof for the storageKeys
548+ for i , key := range storageKeys {
549+ if storageTrie != nil {
550+ proof , storageError := state .GetStorageProof (address , common .HexToHash (key ))
551+ if storageError != nil {
552+ return nil , storageError
553+ }
554+ storageProof [i ] = StorageResult {key , (* hexutil .Big )(state .GetState (address , common .HexToHash (key )).Big ()), common .ToHexArray (proof )}
555+ } else {
556+ storageProof [i ] = StorageResult {key , & hexutil.Big {}, []string {}}
557+ }
558+ }
559+
560+ // create the accountProof
561+ accountProof , proofErr := state .GetProof (address )
562+ if proofErr != nil {
563+ return nil , proofErr
564+ }
565+
566+ return & AccountResult {
567+ Address : address ,
568+ AccountProof : common .ToHexArray (accountProof ),
569+ Balance : (* hexutil .Big )(state .GetBalance (address )),
570+ CodeHash : codeHash ,
571+ Nonce : hexutil .Uint64 (state .GetNonce (address )),
572+ StorageHash : storageHash ,
573+ StorageProof : storageProof ,
574+ }, state .Error ()
575+ }
576+
511577// GetBlockByNumber returns the requested block. When blockNr is -1 the chain head is returned. When fullTx is true all
512578// transactions in the block are returned in full detail, otherwise only the transaction hash is returned.
513579func (s * PublicBlockChainAPI ) GetBlockByNumber (ctx context.Context , blockNr rpc.BlockNumber , fullTx bool ) (map [string ]interface {}, error ) {
0 commit comments