forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
static_service.go
195 lines (176 loc) · 6.18 KB
/
static_service.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
// Copyright (C) 2019-2023, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package avm
import (
"errors"
"fmt"
"net/http"
stdjson "encoding/json"
"github.com/MetalBlockchain/metalgo/ids"
"github.com/MetalBlockchain/metalgo/utils"
"github.com/MetalBlockchain/metalgo/utils/formatting"
"github.com/MetalBlockchain/metalgo/utils/formatting/address"
"github.com/MetalBlockchain/metalgo/utils/json"
"github.com/MetalBlockchain/metalgo/vms/avm/fxs"
"github.com/MetalBlockchain/metalgo/vms/avm/txs"
"github.com/MetalBlockchain/metalgo/vms/components/avax"
"github.com/MetalBlockchain/metalgo/vms/components/verify"
"github.com/MetalBlockchain/metalgo/vms/nftfx"
"github.com/MetalBlockchain/metalgo/vms/propertyfx"
"github.com/MetalBlockchain/metalgo/vms/secp256k1fx"
)
var (
errUnknownAssetType = errors.New("unknown asset type")
_ avax.TransferableIn = (*secp256k1fx.TransferInput)(nil)
_ verify.State = (*secp256k1fx.MintOutput)(nil)
_ avax.TransferableOut = (*secp256k1fx.TransferOutput)(nil)
_ fxs.FxOperation = (*secp256k1fx.MintOperation)(nil)
_ verify.Verifiable = (*secp256k1fx.Credential)(nil)
_ verify.State = (*nftfx.MintOutput)(nil)
_ verify.State = (*nftfx.TransferOutput)(nil)
_ fxs.FxOperation = (*nftfx.MintOperation)(nil)
_ fxs.FxOperation = (*nftfx.TransferOperation)(nil)
_ verify.Verifiable = (*nftfx.Credential)(nil)
_ verify.State = (*propertyfx.MintOutput)(nil)
_ verify.State = (*propertyfx.OwnedOutput)(nil)
_ fxs.FxOperation = (*propertyfx.MintOperation)(nil)
_ fxs.FxOperation = (*propertyfx.BurnOperation)(nil)
_ verify.Verifiable = (*propertyfx.Credential)(nil)
)
// StaticService defines the base service for the asset vm
type StaticService struct{}
func CreateStaticService() *StaticService {
return &StaticService{}
}
// BuildGenesisArgs are arguments for BuildGenesis
type BuildGenesisArgs struct {
NetworkID json.Uint32 `json:"networkID"`
GenesisData map[string]AssetDefinition `json:"genesisData"`
Encoding formatting.Encoding `json:"encoding"`
}
type AssetDefinition struct {
Name string `json:"name"`
Symbol string `json:"symbol"`
Denomination json.Uint8 `json:"denomination"`
InitialState map[string][]interface{} `json:"initialState"`
Memo string `json:"memo"`
}
// BuildGenesisReply is the reply from BuildGenesis
type BuildGenesisReply struct {
Bytes string `json:"bytes"`
Encoding formatting.Encoding `json:"encoding"`
}
// BuildGenesis returns the UTXOs such that at least one address in [args.Addresses] is
// referenced in the UTXO.
func (*StaticService) BuildGenesis(_ *http.Request, args *BuildGenesisArgs, reply *BuildGenesisReply) error {
parser, err := txs.NewParser([]fxs.Fx{
&secp256k1fx.Fx{},
&nftfx.Fx{},
&propertyfx.Fx{},
})
if err != nil {
return err
}
g := Genesis{}
genesisCodec := parser.GenesisCodec()
for assetAlias, assetDefinition := range args.GenesisData {
assetMemo, err := formatting.Decode(args.Encoding, assetDefinition.Memo)
if err != nil {
return fmt.Errorf("problem formatting asset definition memo due to: %w", err)
}
asset := GenesisAsset{
Alias: assetAlias,
CreateAssetTx: txs.CreateAssetTx{
BaseTx: txs.BaseTx{BaseTx: avax.BaseTx{
NetworkID: uint32(args.NetworkID),
BlockchainID: ids.Empty,
Memo: assetMemo,
}},
Name: assetDefinition.Name,
Symbol: assetDefinition.Symbol,
Denomination: byte(assetDefinition.Denomination),
},
}
if len(assetDefinition.InitialState) > 0 {
initialState := &txs.InitialState{
FxIndex: 0, // TODO: Should lookup secp256k1fx FxID
}
for assetType, initialStates := range assetDefinition.InitialState {
switch assetType {
case "fixedCap":
for _, state := range initialStates {
b, err := stdjson.Marshal(state)
if err != nil {
return fmt.Errorf("problem marshaling state: %w", err)
}
holder := Holder{}
if err := stdjson.Unmarshal(b, &holder); err != nil {
return fmt.Errorf("problem unmarshaling holder: %w", err)
}
_, addrbuff, err := address.ParseBech32(holder.Address)
if err != nil {
return fmt.Errorf("problem parsing holder address: %w", err)
}
addr, err := ids.ToShortID(addrbuff)
if err != nil {
return fmt.Errorf("problem parsing holder address: %w", err)
}
initialState.Outs = append(initialState.Outs, &secp256k1fx.TransferOutput{
Amt: uint64(holder.Amount),
OutputOwners: secp256k1fx.OutputOwners{
Threshold: 1,
Addrs: []ids.ShortID{addr},
},
})
}
case "variableCap":
for _, state := range initialStates {
b, err := stdjson.Marshal(state)
if err != nil {
return fmt.Errorf("problem marshaling state: %w", err)
}
owners := Owners{}
if err := stdjson.Unmarshal(b, &owners); err != nil {
return fmt.Errorf("problem unmarshaling Owners: %w", err)
}
out := &secp256k1fx.MintOutput{
OutputOwners: secp256k1fx.OutputOwners{
Threshold: 1,
},
}
for _, addrStr := range owners.Minters {
_, addrBytes, err := address.ParseBech32(addrStr)
if err != nil {
return fmt.Errorf("problem parsing minters address: %w", err)
}
addr, err := ids.ToShortID(addrBytes)
if err != nil {
return fmt.Errorf("problem parsing minters address: %w", err)
}
out.Addrs = append(out.Addrs, addr)
}
out.Sort()
initialState.Outs = append(initialState.Outs, out)
}
default:
return errUnknownAssetType
}
}
initialState.Sort(genesisCodec)
asset.States = append(asset.States, initialState)
}
utils.Sort(asset.States)
g.Txs = append(g.Txs, &asset)
}
utils.Sort(g.Txs)
b, err := genesisCodec.Marshal(txs.CodecVersion, &g)
if err != nil {
return fmt.Errorf("problem marshaling genesis: %w", err)
}
reply.Bytes, err = formatting.Encode(args.Encoding, b)
if err != nil {
return fmt.Errorf("couldn't encode genesis as string: %w", err)
}
reply.Encoding = args.Encoding
return nil
}