-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
55 lines (44 loc) · 1.15 KB
/
util.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package main
import (
"crypto/ecdsa"
"crypto/sha256"
"encoding/binary"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
var randomNumber = []byte("0xaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccddaabbccdd")
func assertNoError(err error, msg string) {
if err == nil {
return
}
if len(msg) == 0 {
msg = "assertNoError failed"
}
panic(fmt.Sprintf("%s: %s", msg, err))
}
func assertEq(v1, v2, msg string) {
if v1 == v2 {
return
}
if len(msg) == 0 {
msg = "assertEq failed"
}
panic(fmt.Sprintf("%s: %s != %s", msg, v1, v2))
}
func genRandomHash(randomNumber []byte) common.Hash {
randomNumberPadded := make([]byte, RandomNumberLength+8)
copy(randomNumberPadded[:RandomNumberLength], randomNumber)
binary.BigEndian.PutUint64(randomNumberPadded[RandomNumberLength:], uint64(0))
res := sha256.Sum256(randomNumberPadded)
return res
}
func deriveAddressFromKey(privKey *ecdsa.PrivateKey) common.Address {
publicKey := privKey.Public()
publicKeyECDSA, ok := publicKey.(*ecdsa.PublicKey)
if !ok {
panic("public key error")
}
address := crypto.PubkeyToAddress(*publicKeyECDSA)
return address
}