-
Notifications
You must be signed in to change notification settings - Fork 15
/
crypto.go
148 lines (120 loc) · 5.49 KB
/
crypto.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/* Mysterium network payment library.
*
* Copyright (C) 2020 BlockDev AG
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package crypto
import (
"encoding/binary"
"encoding/hex"
"errors"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
// RecoverAddress recovers the address from message and signature
func RecoverAddress(message []byte, signature []byte) (common.Address, error) {
publicKey, err := crypto.Ecrecover(crypto.Keccak256(message), signature)
if err != nil {
return common.Address{}, err
}
pubKey, err := crypto.UnmarshalPubkey(publicKey)
if err != nil {
return common.Address{}, err
}
recoveredAddress := crypto.PubkeyToAddress(*pubKey)
return recoveredAddress, nil
}
// GetProxyCode generates bytecode of minimal proxy contract (EIP 1167)
func GetProxyCode(destinationAddress string) ([]byte, error) {
return hex.DecodeString("3d602d80600a3d3981f3363d3d373d3d3d363d73" + destinationAddress + "5af43d82803e903d91602b57fd5bf3")
}
// keccak("0xff++msg.sender++salt++keccak(byteCode)")
func deriveCreate2Address(salt string, msgSender string, implementation string) (address string, err error) {
if !isHexAddress(msgSender) || !isHexAddress(implementation) {
return "", errors.New("msgSender and implementation have to be hex addresses")
}
bytecode, _ := GetProxyCode(ensureNoPrefix(implementation))
input, _ := hex.DecodeString("ff" + ensureNoPrefix(msgSender) + ensureNoPrefix(salt) + common.Bytes2Hex(crypto.Keccak256(bytecode)))
return "0x" + common.Bytes2Hex(crypto.Keccak256(input))[24:], nil
}
// GenerateChannelAddress generate channel address from given identity hash
func GenerateChannelAddress(identity, hermes, registry, channelImplementation string) (address string, err error) {
if !isHexAddress(identity) || !isHexAddress(registry) || !isHexAddress(channelImplementation) {
return "", errors.New("given identity, registry and channelImplementation params have to be hex addresses")
}
saltBytes, err := hex.DecodeString(ensureNoPrefix(identity) + ensureNoPrefix(hermes))
if err != nil {
return "", err
}
salt := hex.EncodeToString(crypto.Keccak256(saltBytes))
return deriveCreate2Address(salt, registry, channelImplementation)
}
// GenerateProviderChannelID generated channelID for provider channels from given identity hash
func GenerateProviderChannelID(providerIdentity, hermesAddress string) (string, error) {
return generateProviderChannelID(providerIdentity, hermesAddress, ProviderChannelTypeDefault)
}
// GenerateProviderChannelIDForPayAndSettle generated channelID for provider channels from given identity hash
func GenerateProviderChannelIDForPayAndSettle(providerIdentity, hermesAddress string) (string, error) {
return generateProviderChannelID(providerIdentity, hermesAddress, ProviderChannelTypePayAndSettle)
}
type ProviderChannelType string
const (
ProviderChannelTypeDefault ProviderChannelType = ""
ProviderChannelTypePayAndSettle ProviderChannelType = "withdrawal"
)
func generateProviderChannelID(providerIdentity, hermesAddress string, channelType ProviderChannelType) (string, error) {
if !isHexAddress(providerIdentity) || !isHexAddress(hermesAddress) {
return "", errors.New("given providerIdentity and hermesAddress params have to be hex addresses")
}
input := append(
common.HexToAddress(providerIdentity).Bytes(),
common.HexToAddress(hermesAddress).Bytes()...,
)
if channelType == ProviderChannelTypePayAndSettle {
input = append(input, []byte(string(ProviderChannelTypePayAndSettle))...)
}
return "0x" + common.Bytes2Hex(crypto.Keccak256(input)), nil
}
// GenerateHermesAddress generate hermes address from given hermes operator address
func GenerateHermesAddress(operator string, registry string, hermesImplementation string) (address string, err error) {
if !isHexAddress(operator) || !isHexAddress(registry) || !isHexAddress(hermesImplementation) {
return "", errors.New("given operator, registry and hermesImplementation params have to be hex addresses")
}
salt, err := toBytes32(operator)
if err != nil {
return "", err
}
return deriveCreate2Address(salt, registry, hermesImplementation)
}
// ReformatSignatureVForBC takes in the signature and modifies its last byte to correspond to the format required for SC
func ReformatSignatureVForBC(signature []byte) error {
if len(signature) != 65 {
return errors.New("the signature must be 65 bytes long")
}
var v = 27 + (uint64(signature[len(signature)-1]) % 2)
buffer := make([]byte, 1)
_ = binary.PutUvarint(buffer, v)
signature[64] = buffer[0]
return nil
}
// ReformatSignatureVForRecovery takes in the signature and modifies its last byte to normalize V to either 0 or 1
func ReformatSignatureVForRecovery(signature []byte) error {
if len(signature) != 65 {
return errors.New("the signature must be 65 bytes long")
}
v := uint64(signature[64]) % 27
buffer := make([]byte, 1)
_ = binary.PutUvarint(buffer, v)
signature[64] = buffer[0]
return nil
}