-
Notifications
You must be signed in to change notification settings - Fork 21
/
openoracle.go
141 lines (121 loc) · 3.86 KB
/
openoracle.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
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero 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 Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package openoracle
import (
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/accounts/abi"
"github.com/ethereum/go-ethereum/crypto"
)
type OracleResponse struct {
Timestamp string `json:"timestamp"`
Messages []string `json:"messages"`
Signatures []string `json:"signatures"`
Prices map[string]string `json:"prices"`
}
// UnmarshalVerify will unmarshal a json raw payload
// into and OracleResponse
// if the unmarshal is successful then the content is verified.
func UnmarshalVerify(payload []byte, address string) (*OracleResponse, error) {
oresp, err := Unmarshal(payload)
if err != nil {
return nil, err
}
_, _, err = Verify(*oresp)
return oresp, err
}
func Unmarshal(payload []byte) (*OracleResponse, error) {
oresp := OracleResponse{}
err := json.Unmarshal(payload, &oresp)
if err != nil {
return nil, err
}
return &oresp, nil
}
func Verify(oresp OracleResponse) ([]string, map[string]string, error) {
typString, err := abi.NewType("string", "", nil)
if err != nil {
return nil, nil, err
}
typUint64, err := abi.NewType("uint64", "", nil)
if err != nil {
return nil, nil, err
}
args := abi.Arguments([]abi.Argument{
{
Name: "kind",
Type: typString,
},
{
Name: "timestamp",
Type: typUint64,
},
{
Name: "key",
Type: typString,
},
{
Name: "value",
Type: typUint64,
},
})
// ensure we have as much signature than messages
if len(oresp.Messages) != len(oresp.Signatures) {
return nil, nil, fmt.Errorf("got %v signatures, but have %v messages", len(oresp.Signatures), len(oresp.Messages))
}
addresses := map[string]struct{}{}
keyValues := map[string]string{}
for i := 0; i < len(oresp.Messages); i++ {
sigBytes, err := hex.DecodeString(strings.TrimPrefix(oresp.Signatures[i], "0x"))
if err != nil {
return nil, nil, err
}
msgBytes, err := hex.DecodeString(strings.TrimPrefix(oresp.Messages[i], "0x"))
if err != nil {
return nil, nil, err
}
hashDecoded := crypto.Keccak256Hash(msgBytes)
hashDecodedPadded := accounts.TextHash(hashDecoded.Bytes())
if len(sigBytes) > 65 {
sigBytes[64] = sigBytes[len(sigBytes)-1]
sigBytes = sigBytes[:65]
}
sigBytes[64] = sigBytes[64] - 27
// SigToPub actually goes through the verification of the signature
// before returning the pubkey that we can use to retrieve the address later
// on. so no need for extra steps in verifying the signature later on.
sigPublicKeyECDSA, err := crypto.SigToPub(hashDecodedPadded, sigBytes)
if err != nil {
return nil, nil, err
}
addrHex := crypto.PubkeyToAddress(*sigPublicKeyECDSA).Hex()
addresses[addrHex] = struct{}{}
m := map[string]interface{}{}
err = args.UnpackIntoMap(m, msgBytes)
if err != nil {
return nil, nil, err
}
keyValues[fmt.Sprintf("%v.%v.value", m["kind"], m["key"])] = fmt.Sprintf("%v", m["value"])
keyValues[fmt.Sprintf("%v.%v.timestamp", m["kind"], m["key"])] = fmt.Sprintf("%v", m["timestamp"])
}
addressesSlice := make([]string, 0, len(addresses))
for k := range addresses {
addressesSlice = append(addressesSlice, k)
}
return addressesSlice, keyValues, nil
}