This repository has been archived by the owner on May 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 29
/
genesis.go
284 lines (261 loc) · 7.73 KB
/
genesis.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// Copyright (c) 2020-2022 Blockwatch Data Inc.
// Author: alex@blockwatch.cc
package rpc
import (
"encoding/hex"
"encoding/json"
"fmt"
"strconv"
"github.com/echa/bson"
"blockwatch.cc/tzgo/micheline"
"blockwatch.cc/tzgo/tezos"
)
// lacking the algorithm to compute KT1 addresses from content,
// we hard-code all mainnet vesting KT1 addresses here
var vestingContractAddrs = []tezos.Address{
tezos.MustParseAddress("KT1QuofAgnsWffHzLA7D78rxytJruGHDe7XG"),
tezos.MustParseAddress("KT1CSKPf2jeLpMmrgKquN2bCjBTkAcAdRVDy"),
tezos.MustParseAddress("KT1SLWhfqPtQq7f4zLomh8BNgDeprF9B6d2M"),
tezos.MustParseAddress("KT1WPEis2WhAc2FciM2tZVn8qe6pCBe9HkDp"),
tezos.MustParseAddress("KT1Um7ieBEytZtumecLqGeL56iY6BuWoBgio"),
tezos.MustParseAddress("KT1Cz7TyVFvHxXpxLS57RFePrhTGisUpPhvD"),
tezos.MustParseAddress("KT1Q1kfbvzteafLvnGz92DGvkdypXfTGfEA3"),
tezos.MustParseAddress("KT1PDAELuX7CypUHinUgFgGFskKs7ytwh5Vw"),
tezos.MustParseAddress("KT1A56dh8ivKNvLiLVkjYPyudmnY2Ti5Sba3"),
tezos.MustParseAddress("KT1RUT25eGgo9KKWXfLhj1xYjghAY1iZ2don"),
tezos.MustParseAddress("KT1FuFDZGdw86p6krdBUKoZfEMkcUmezqX5o"),
tezos.MustParseAddress("KT1THsDNgHtN56ew9VVCAUWnqPC81pqAxCEp"),
tezos.MustParseAddress("KT1EWLAQGPMF2uhtVRPaCH2vtFVN36Njdr6z"),
tezos.MustParseAddress("KT1FN5fcNNcgieGjzxbVEPWUpJGwZEpzNGA8"),
tezos.MustParseAddress("KT1TcAHw5gpejyemwRtdNyFKGBLc4qwA5gtw"),
tezos.MustParseAddress("KT1VsSxSXUkgw6zkBGgUuDXXuJs9ToPqkrCg"),
tezos.MustParseAddress("KT1Msatnmdy24sQt6knzpALs4tvHfSPPduA2"),
tezos.MustParseAddress("KT1LZFMGrdnPjRLsCZ1aEDUAF5myA5Eo4rQe"),
tezos.MustParseAddress("KT1LQ99RfGcmFe98PiBcGXuyjBkWzAcoXXhW"),
tezos.MustParseAddress("KT1Kfbk3B6NYPCPohPBDU3Hxf5Xeyy9PdkNp"),
tezos.MustParseAddress("KT1DnfT4hfikoMY3uiPE9mQV4y3Xweramb2k"),
tezos.MustParseAddress("KT19xDbLsvQKnp9xqfDNPWJbKJJmV93dHDUa"),
tezos.MustParseAddress("KT1HvwFnXteMbphi7mfPDhCWkZSDvXEz8iyv"),
tezos.MustParseAddress("KT1KRyTaxCAM3YRquifEe29BDbUKNhJ6hdtx"),
tezos.MustParseAddress("KT1Gow8VzXZx3Akn5kvjACqnjnyYBxQpzSKr"),
tezos.MustParseAddress("KT1W148mcjmfvr9J2RvWcGHxsAFApq9mcfgT"),
tezos.MustParseAddress("KT1D5NmtDtgCwPxYNb2ZK2But6dhNLs1T1bV"),
tezos.MustParseAddress("KT1TzamC1SCj68ia2E4q2GWZeT24yRHvUZay"),
tezos.MustParseAddress("KT1CM1g1o9RKDdtDKgcBWE59X2KgTc2TcYtC"),
tezos.MustParseAddress("KT1FL3C6t9Lyfskyb6rQrCRQTnf7M9t587VM"),
tezos.MustParseAddress("KT1JW6PwhfaEJu6U3ENsxUeja48AdtqSoekd"),
tezos.MustParseAddress("KT1VvXEpeBpreAVpfp4V8ZujqWu2gVykwXBJ"),
}
type GenesisData struct {
Accounts []*X0
Contracts []*X1
Commitments []*X2
}
// bootstrap account with or without known public key
type X0 struct {
Addr tezos.Address
Key tezos.Key
Value int64
}
// bootstrap contract
type X1 struct {
Addr tezos.Address
Delegate tezos.Address
Value int64
Script micheline.Script
}
// commitment
type X2 struct {
Addr tezos.Address
Value int64
}
func (b *GenesisData) Supply() int64 {
var s int64
for _, v := range b.Accounts {
s += v.Value
}
for _, v := range b.Contracts {
s += v.Value
}
for _, v := range b.Commitments {
s += v.Value
}
return s
}
func (b *GenesisData) UnmarshalText(data []byte) error {
buf := make([]byte, hex.DecodedLen(len(data)))
if _, err := hex.Decode(buf, data); err != nil {
return err
}
// decode BSON
encoded := &bootstrap{}
if err := bson.Unmarshal(buf[4:], encoded); err != nil {
return err
}
// convert BSON to Structs
acc, err := encoded.DecodeAccounts()
if err != nil {
return err
}
b.Accounts = acc
contracts, err := encoded.DecodeContracts()
if err != nil {
return err
}
b.Contracts = contracts
commit, err := encoded.DecodeCommitments()
if err != nil {
return err
}
b.Commitments = commit
return nil
}
// BSON data types
type bootstrap struct {
Accounts [][]string `bson:"bootstrap_accounts"`
Contracts []*contract `bson:"bootstrap_contracts"`
Commitments [][]string `bson:"commitments"`
}
type contract struct {
Delegate string `bson:"delegate"`
Value string `bson:"amount"`
Script bson.M `bson:"script"`
}
func (b *bootstrap) DecodeContracts() ([]*X1, error) {
// ignore non-mainnet contract lists (we don't know their addresses)
if len(b.Contracts) != len(vestingContractAddrs) {
return nil, nil
}
c := make([]*X1, len(b.Contracts))
for i, v := range b.Contracts {
c[i] = &X1{
Addr: vestingContractAddrs[i],
}
addr, err := tezos.ParseAddress(v.Delegate)
if err != nil {
return nil, err
}
c[i].Delegate = addr
value, err := strconv.ParseInt(v.Value, 10, 64)
if err != nil {
return nil, err
}
c[i].Value = value
// script unmarshalling BSON -> JSON -> Micheline
buf, err := json.Marshal(v.Script)
if err != nil {
return nil, err
}
if err := json.Unmarshal(buf, &c[i].Script); err != nil {
return nil, err
}
// skip when this does not look like a vesting contract
isVesting := true
switch {
case !c[i].Script.Storage.IsValid():
isVesting = false
case len(c[i].Script.Storage.Args) == 0:
isVesting = false
case !c[i].Script.Storage.Args[0].IsValid():
isVesting = false
case len(c[i].Script.Storage.Args[0].Args) == 0:
isVesting = false
case !c[i].Script.Storage.Args[0].Args[1].IsValid():
isVesting = false
case len(c[i].Script.Storage.Args[0].Args[1].Args) == 0:
isVesting = false
case !c[i].Script.Storage.Args[0].Args[1].Args[0].IsValid():
isVesting = false
case len(c[i].Script.Storage.Args[0].Args[1].Args[0].Args) == 0:
isVesting = false
}
if !isVesting {
continue
}
// patch initial storage (convert strings to bytes) to circumvent tezos
// origination bug
// - replace edpk strings with byte sequences
// - replace delegate addesses with binary pkh 00 TT AAAA...
// keygroups >> signatories
for _, v := range c[i].Script.Storage.Args[0].Args[1].Args[0].Args {
for _, vv := range v.Args[0].Args {
edpk, err := tezos.ParseKey(vv.String)
if err != nil {
return nil, fmt.Errorf("decoding signatory key %s: %v", vv.String, err)
}
vv.Type = micheline.PrimBytes
vv.Bytes = append([]byte{0}, edpk.Data...)
vv.String = ""
}
}
// only the first 8 contracts have authorizers set
if i < 8 {
// pour_dest
pair := c[i].Script.Storage.Args[1].Args[1].Args[0].Args
dest, err := tezos.ParseAddress(pair[0].String)
if err != nil {
return nil, fmt.Errorf("decoding pour_dest %s: %v", pair[0].String, err)
}
pair[0].Type = micheline.PrimBytes
pair[0].Bytes = dest.Encode()
pair[0].String = ""
// pour_authorizer
edpk, err := tezos.ParseKey(pair[1].String)
if err != nil {
return nil, fmt.Errorf("decoding pour_authorizer key %s: %v", pair[1].String, err)
}
// replace with byte sequence
pair[1].Type = micheline.PrimBytes
pair[1].Bytes = append([]byte{0}, edpk.Data...)
pair[1].String = ""
}
}
return c, nil
}
func (b *bootstrap) DecodeAccounts() ([]*X0, error) {
acc := make([]*X0, len(b.Accounts))
for i, v := range b.Accounts {
acc[i] = &X0{}
pk := v[0]
switch {
case tezos.HasKeyPrefix(pk):
key, err := tezos.ParseKey(pk)
if err != nil {
return nil, err
}
acc[i].Key = key
acc[i].Addr = key.Address()
case tezos.HasAddressPrefix(pk):
addr, err := tezos.ParseAddress(pk)
if err != nil {
return nil, err
}
acc[i].Addr = addr
}
amount, err := strconv.ParseInt(v[1], 10, 64)
if err != nil {
return nil, err
}
acc[i].Value = amount
}
return acc, nil
}
func (b *bootstrap) DecodeCommitments() ([]*X2, error) {
c := make([]*X2, len(b.Commitments))
for i, v := range b.Commitments {
c[i] = &X2{}
// [ $Blinded public key hash, $mutez ]
pk := v[0]
addr, err := tezos.ParseAddress(pk)
if err != nil {
return nil, err
}
c[i].Addr = addr
amount, err := strconv.ParseInt(v[1], 10, 64)
if err != nil {
return nil, err
}
c[i].Value = amount
}
return c, nil
}