Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions chains/neo/wallet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package neo

import (
"fmt"
"os"

"github.com/joeqian10/neo-gogogo/wallet"
"github.com/polynetwork/poly-io-test/config"
"golang.org/x/crypto/ssh/terminal"
)

func LoadAccount() *wallet.Account {
// open the NEO wallet
//neoAccount, err := wallet.NewAccountFromWIF(config.DefConfig.NeoWalletWIF)
w, err := wallet.NewWalletFromFile(config.DefConfig.NeoWalletFile)
if err != nil {
panic("[NEO Relayer] Failed to open NEO wallet")
return nil
}

fmt.Printf("Neo Wallet Password:")
pwd, err := terminal.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
panic("[NEO Wallet] Invalid password entered")

}
neoPwd := string(pwd)

err = w.DecryptAll(neoPwd)
if err != nil {
panic("[NEO Wallet] Failed to decrypt NEO account")
}
return w.Accounts[0]
}
147 changes: 146 additions & 1 deletion cmd/tools/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ package main
import (
"bytes"
"context"
"crypto/elliptic"
"encoding/binary"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"io/ioutil"

"github.com/polynetwork/poly/core/states"
"github.com/polynetwork/poly/native/service/governance/neo3_state_manager"
"io/ioutil"

"math/big"
"os"
Expand All @@ -40,20 +42,26 @@ import (
"github.com/Zilliqa/gozilliqa-sdk/provider"
zilutil "github.com/Zilliqa/gozilliqa-sdk/util"

"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/wire"
types3 "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum"
"github.com/ethereum/go-ethereum/accounts/abi"
common3 "github.com/ethereum/go-ethereum/common"
"github.com/joeqian10/neo-gogogo/block"
"github.com/joeqian10/neo-gogogo/helper"
"github.com/joeqian10/neo-gogogo/helper/io"
"github.com/joeqian10/neo-gogogo/rpc"
"github.com/joeqian10/neo-gogogo/sc"
"github.com/joeqian10/neo-gogogo/tx"

block3 "github.com/joeqian10/neo3-gogogo/block"
helper3 "github.com/joeqian10/neo3-gogogo/helper"
io3 "github.com/joeqian10/neo3-gogogo/io"
rpc3 "github.com/joeqian10/neo3-gogogo/rpc"
"github.com/ontio/ontology-crypto/ec"
"github.com/ontio/ontology-crypto/keypair"
"github.com/ontio/ontology-crypto/sm2"
ontology_go_sdk "github.com/ontio/ontology-go-sdk"
common2 "github.com/ontio/ontology/common"
"github.com/ontio/ontology/core/types"
Expand All @@ -67,6 +75,7 @@ import (
"github.com/polynetwork/poly-io-test/chains/btc"
cosmos2 "github.com/polynetwork/poly-io-test/chains/cosmos"
"github.com/polynetwork/poly-io-test/chains/eth"
"github.com/polynetwork/poly-io-test/chains/neo"
"github.com/polynetwork/poly-io-test/chains/ont"
"github.com/polynetwork/poly-io-test/config"
"github.com/polynetwork/poly-io-test/log"
Expand Down Expand Up @@ -1247,6 +1256,51 @@ func SyncOntGenesisHeader(poly *poly_go_sdk.PolySdk, accArr []*poly_go_sdk.Accou
log.Infof("successful to sync poly genesis header to Ontology: ( txhash: %s )", txHash.ToHexString())
}

func getUncompressedKey(key keypair.PublicKey) []byte {
var buff bytes.Buffer
switch t := key.(type) {
case *ec.PublicKey:
switch t.Algorithm {
case ec.ECDSA:
// Take P-256 as a special case
if t.Params().Name == elliptic.P256().Params().Name {
return ec.EncodePublicKey(t.PublicKey, false)
}
buff.WriteByte(byte(0x12))
case ec.SM2:
buff.WriteByte(byte(0x13))
}
label, err := getCurveLabel(t.Curve.Params().Name)
if err != nil {
panic(err)
}
buff.WriteByte(label)
buff.Write(ec.EncodePublicKey(t.PublicKey, false))
default:
panic("err")
}
return buff.Bytes()
}

func getCurveLabel(name string) (byte, error) {
switch strings.ToUpper(name) {
case strings.ToUpper(elliptic.P224().Params().Name):
return 1, nil
case strings.ToUpper(elliptic.P256().Params().Name):
return 2, nil
case strings.ToUpper(elliptic.P384().Params().Name):
return 3, nil
case strings.ToUpper(elliptic.P521().Params().Name):
return 4, nil
case strings.ToUpper(sm2.SM2P256V1().Params().Name):
return 20, nil
case strings.ToUpper(btcec.S256().Name):
return 5, nil
default:
panic("err")
}
}

func SyncNeoGenesisHeader(poly *poly_go_sdk.PolySdk, accArr []*poly_go_sdk.Account) error {
cli := rpc.NewClient(config.DefConfig.NeoUrl)
resp := cli.GetBlockHeaderByIndex(config.DefConfig.NeoEpoch)
Expand Down Expand Up @@ -1274,7 +1328,98 @@ func SyncNeoGenesisHeader(poly *poly_go_sdk.PolySdk, accArr []*poly_go_sdk.Accou
testcase.WaitPolyTx(txhash, poly)
log.Infof("successful to sync neo genesis header: ( txhash: %s )", txhash.ToHexString())
}
block, err := poly.GetBlockByHeight(config.DefConfig.RCEpoch)
if err != nil {
panic(err)
}
headerBytes := block.Header.GetMessage()
// raw header
cp1 := sc.ContractParameter{
Type: sc.ByteArray,
Value: headerBytes,
}
log.Infof("raw header: %s", helper.BytesToHex(headerBytes))

// public keys
bs := []byte{}
blkInfo := &vconfig.VbftBlockInfo{}
_ = json.Unmarshal(block.Header.ConsensusPayload, blkInfo) // already checked before
if blkInfo.NewChainConfig != nil {
var bookkeepers []keypair.PublicKey
for _, peer := range blkInfo.NewChainConfig.Peers {
keyBytes, _ := hex.DecodeString(peer.ID)
key, _ := keypair.DeserializePublicKey(keyBytes) // compressed
bookkeepers = append(bookkeepers, key)
}
bookkeepers = keypair.SortPublicKeys(bookkeepers)
for _, key := range bookkeepers {
uncompressed := getUncompressedKey(key)
bs = append(bs, uncompressed...)
}
}
cp2 := sc.ContractParameter{
Type: sc.ByteArray,
Value: bs,
}
log.Infof("pub keys: %s", helper.BytesToHex(bs))

/*
// signatures
bs2 := []byte{}
for _, sig := range block.Header.SigData {
newSig, _ := signature.ConvertToEthCompatible(sig) // convert to eth
bs2 = append(bs2, newSig...)
}
cp3 := sc.ContractParameter{
Type: sc.ByteArray,
Value: bs2,
}
log.Infof("signature: %s", helper.BytesToHex(bs2))
*/

// build script
sb := sc.NewScriptBuilder()
scriptHash := helper.HexToBytes(config.DefConfig.NeoCCMC) // hex string to little endian byte[]
sb.MakeInvocationScript(scriptHash, "InitGenesisBlock", []sc.ContractParameter{cp1, cp2})

script := sb.ToArray()

tb := tx.NewTransactionBuilder(config.DefConfig.NeoUrl)
w := neo.LoadAccount()
from, err := helper.AddressToScriptHash(w.Address)
// create an InvocationTransaction
sysFee := helper.Fixed8FromFloat64(config.DefConfig.NeoSysFee)
netFee := helper.Fixed8FromFloat64(config.DefConfig.NeoNetFee)
itx, err := tb.MakeInvocationTransaction(script, from, nil, from, sysFee, netFee)
if err != nil {
return fmt.Errorf("[InitGenesisBlock] tb.MakeInvocationTransaction error: %s", err)
}
// sign transaction
err = tx.AddSignature(itx, w.KeyPair)
if err != nil {
return fmt.Errorf("[InitGenesisBlock] tx.AddSignature error: %s", err)
}

rawTxString := itx.RawTransactionString()
log.Infof("rawTxString: %s", rawTxString)
// send the raw transaction
response := cli.SendRawTransaction(rawTxString)
if response.HasError() {
return fmt.Errorf("[InitGenesisBlock] SendRawTransaction error: %s, "+
"unsigned header hex string: %s, "+
"public keys hex string: %s, "+
"script hex string: %s, "+
"changeBookKeeper RawTransactionString: %s",
response.ErrorResponse.Error.Message,
helper.BytesToHex(headerBytes),
helper.BytesToHex(bs),
helper.BytesToHex(script),
rawTxString)
}

log.Infof("[InitGenesisBlock] neoTxHash is: %s", itx.HashString())
hash, _ := helper.UInt256FromString(itx.HashString())
neo.WaitNeoTx(cli, hash)
return nil
}

Expand Down
9 changes: 6 additions & 3 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,12 @@ type TestConfig struct {
CMEpoch int64

// neo chain conf
NeoUrl string
NeoWif string
NeoEpoch uint32
NeoUrl string
NeoWif string
NeoEpoch uint32
NeoWalletFile string
NeoSysFee float64
NeoNetFee float64

// neo3 chain
Neo3Url string
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ require (
github.com/polynetwork/btc-vendor-tools v0.0.0-20200813091748-3b19a5fd7666
github.com/polynetwork/cosmos-poly-module v0.0.0-20200810030259-95d586518759
github.com/polynetwork/eth-contracts v0.0.0-20200903021827-c9212e419943
github.com/polynetwork/poly v0.0.0-20201216061550-50185057319d
github.com/polynetwork/poly v0.0.0-20210112063446-24e3d053e9d6
github.com/polynetwork/poly-go-sdk v0.0.0-20200817120957-365691ad3493
github.com/prometheus/tsdb v0.9.1 // indirect
github.com/rjeczalik/notify v0.9.2 // indirect
Expand All @@ -39,6 +39,7 @@ require (
github.com/status-im/keycard-go v0.0.0-20190424133014-d95853db0f48 // indirect
github.com/stretchr/testify v1.7.0
github.com/tendermint/tendermint v0.33.9
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2
gopkg.in/check.v1 v1.0.0-20200902074654-038fdea0a05b // indirect
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776 // indirect
)
Expand Down
Loading