github.com/annchain/OG@v0.0.9/arefactor_core/core/genesis.go (about) 1 // Copyright © 2019 Annchain Authors <EMAIL ADDRESS> 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package core 15 16 import ( 17 "bytes" 18 "encoding/binary" 19 "encoding/json" 20 "fmt" 21 "io/ioutil" 22 "path/filepath" 23 24 "github.com/annchain/OG/arefactor/common/hexutil" 25 "github.com/annchain/OG/arefactor/og" 26 ogTypes "github.com/annchain/OG/arefactor/og_interface" 27 "github.com/annchain/OG/arefactor/types" 28 "github.com/annchain/OG/common/math" 29 "github.com/libp2p/go-libp2p-core/crypto" 30 pb "github.com/libp2p/go-libp2p-core/crypto/pb" 31 ) 32 33 const MaxAccountCount = 255 34 35 var keyType = pb.KeyType_Secp256k1 36 37 func DefaultGenesis(genesisPath string) (*types.Sequencer, map[ogTypes.AddressKey]*math.BigInt) { 38 //crypto.SignerSecp256k1{}, 39 seq := newUnsignedSequencer(0, 0) 40 seq.GetBase().Signature, _ = hexutil.FromHex("3044022012302bd7c951fcbfef2646d996fa42709a3cc35dfcaf480fa4f0f8782645585d0220424d7102da89f447b28c53aae388acf0ba57008c8048f5e34dc11765b1cab7f6") 41 seq.GetBase().PublicKey, _ = hexutil.FromHex("b3e1b8306e1bab15ed51a4c24b086550677ba99cd62835965316a36419e8f59ce6a232892182da7401a329066e8fe2af607287139e637d314bf0d61cb9d1c7ee") 42 43 pubUnmarshaller := crypto.PubKeyUnmarshallers[keyType] 44 pub, _ := pubUnmarshaller(seq.GetBase().PublicKey) 45 ledgerAccount := &ogTypes.OgLedgerAccount{ 46 PublicKey: pub, 47 } 48 49 issuer, _ := og.OgAddressConverter{}.AddressFromAccount(ledgerAccount) 50 seq.Issuer = issuer 51 hash := seq.CalcTxHash() 52 seq.SetHash(hash) 53 54 addr, _ := ogTypes.HexToAddress20("643d534e15a315173a3c18cd13c9f95c7484a9bc") 55 balance := map[ogTypes.AddressKey]*math.BigInt{} 56 balance[addr.AddressKey()] = math.NewBigInt(99999999) 57 accounts := GetGenesisAccounts(genesisPath) 58 //accounts := GetSampleAccounts(cryptoType) 59 var buf bytes.Buffer 60 for i := 0; i < len(accounts.Accounts); i++ { 61 balance[accounts.Accounts[i].address.AddressKey()] = math.NewBigInt(int64(accounts.Accounts[i].Balance)) 62 binary.Write(&buf, binary.BigEndian, accounts.Accounts[i].Address) 63 binary.Write(&buf, binary.BigEndian, accounts.Accounts[i].Balance) 64 } 65 //h := sha256.New() 66 //h.Write(buf.Bytes()) 67 //sum := h.Sum(nil) 68 //hash.MustSetBytes(sum, common.PaddingNone) 69 //seq.SetHash(hash) 70 71 return seq, balance 72 } 73 74 type Account struct { 75 address ogTypes.Address `json:"-"` 76 Address string `json:"address"` 77 Balance uint64 `json:"balance"` 78 } 79 80 type GenesisAccounts struct { 81 Accounts []Account `json:"accounts"` 82 } 83 84 func GetGenesisAccounts(genesisPath string) *GenesisAccounts { 85 absPath, err := filepath.Abs(genesisPath) 86 if err != nil { 87 panic(fmt.Sprintf("Error on parsing config file path: %s %v err", absPath, err)) 88 } 89 data, err := ioutil.ReadFile(absPath) 90 if err != nil { 91 panic(err) 92 } 93 var accounts GenesisAccounts 94 err = json.Unmarshal(data, &accounts) 95 if err != nil { 96 panic(err) 97 } 98 for i := 0; i < len(accounts.Accounts); i++ { 99 addr, err := ogTypes.AddressFromHex(accounts.Accounts[i].Address) 100 if err != nil { 101 panic(err) 102 } 103 accounts.Accounts[i].address = addr 104 } 105 return &accounts 106 107 } 108 109 //func GetSampleAccounts() []*account.SampleAccount { 110 // var accounts []*account.SampleAccount 111 // if crypto.Signer.GetCryptoType() == crypto.CryptoTypeSecp256k1 { 112 // logrus.WithField("len", MaxAccountCount).Debug("Generating secp256k1 sample accounts") 113 // for i := 0; i < MaxAccountCount; i++ { 114 // acc := account.NewAccount(fmt.Sprintf("0x0170E6B713CD32904D07A55B3AF5784E0B23EB38589EBF975F0AB89E6F8D786F%02X", i)) 115 // acc.Id = i 116 // accounts = append(accounts, acc) 117 // } 118 // 119 // } else { 120 // logrus.WithField("len", math.MinInt(len(sampleEd25519PrivKeys), MaxAccountCount)).Debug("Generating ed25519 sample accounts") 121 // for i := 0; i < MaxAccountCount; i++ { 122 // if i >= len(sampleEd25519PrivKeys) { 123 // break 124 // } 125 // acc := account.NewAccount(sampleEd25519PrivKeys[i]) 126 // acc.Id = i 127 // accounts = append(accounts, acc) 128 // } 129 // 130 // } 131 // return accounts 132 //} 133 134 func newUnsignedSequencer(height uint64, accountNonce uint64) *types.Sequencer { 135 tx := types.Sequencer{ 136 TxBase: types.TxBase{ 137 AccountNonce: accountNonce, 138 Type: types.TxBaseTypeSequencer, 139 Height: height, 140 }, 141 } 142 return &tx 143 }