forked from zhangchiqing/merkle-patricia-trie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
erc20_proof.go
29 lines (23 loc) · 889 Bytes
/
erc20_proof.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
package main
import (
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
func GetSlotForMapKey(keyInMap []byte, slotIndexForMap int) [32]byte {
return crypto.Keccak256Hash(
keyInMap,
common.LeftPadBytes(big.NewInt(int64(slotIndexForMap)).Bytes(), 32),
)
}
func GetSlotForERC20TokenHolder(slotIndexForHoldersMap int, tokenHolder common.Address) [32]byte {
return GetSlotForMapKey(common.LeftPadBytes(tokenHolder[:], 32), slotIndexForHoldersMap)
}
func GetSlotForArrayItem(slotIndexForArray int, indexInArray int, itemSize int) [32]byte {
bytes := crypto.Keccak256Hash(common.LeftPadBytes(big.NewInt(int64(slotIndexForArray)).Bytes(), 32))
arrayPos := new(big.Int).SetBytes(bytes[:])
itemPos := arrayPos.Add(arrayPos, big.NewInt(int64(indexInArray*itemSize)))
var pos [32]byte
copy(pos[:], itemPos.Bytes()[:32])
return pos
}