From 1a1b9bcbd32e5731ef195c0847770b789192eb68 Mon Sep 17 00:00:00 2001 From: wz Date: Tue, 17 Apr 2018 13:18:22 +0800 Subject: [PATCH] Main network and test network distinguish address prefix --- account/accounts.go | 6 +++--- account/builder.go | 2 +- api/accounts.go | 9 +++++---- blockchain/txbuilder/actions.go | 2 +- consensus/general.go | 2 ++ node/node.go | 5 ++++- wallet/annotated.go | 4 ++-- 7 files changed, 18 insertions(+), 12 deletions(-) diff --git a/account/accounts.go b/account/accounts.go index ef1982c6..eda6d92b 100644 --- a/account/accounts.go +++ b/account/accounts.go @@ -104,7 +104,7 @@ type Manager struct { delayedACPsMu sync.Mutex delayedACPs map[*txbuilder.TemplateBuilder][]*CtrlProgram - accIndexMu sync.Mutex + accIndexMu sync.Mutex } // ExpireReservations removes reservations that have expired periodically. @@ -276,7 +276,7 @@ func (m *Manager) createP2PKH(ctx context.Context, account *Account, change bool pubHash := crypto.Ripemd160(derivedPK) // TODO: pass different params due to config - address, err := common.NewAddressWitnessPubKeyHash(pubHash, &consensus.MainNetParams) + address, err := common.NewAddressWitnessPubKeyHash(pubHash, consensus.ActiveNetParams) if err != nil { return nil, err } @@ -307,7 +307,7 @@ func (m *Manager) createP2SH(ctx context.Context, account *Account, change bool) scriptHash := crypto.Sha256(signScript) // TODO: pass different params due to config - address, err := common.NewAddressWitnessScriptHash(scriptHash, &consensus.MainNetParams) + address, err := common.NewAddressWitnessScriptHash(scriptHash, consensus.ActiveNetParams) if err != nil { return nil, err } diff --git a/account/builder.go b/account/builder.go index 945c0855..19c45d52 100644 --- a/account/builder.go +++ b/account/builder.go @@ -152,7 +152,7 @@ func UtxoToInputs(signer *signers.Signer, u *UTXO) (*types.TxInput, *txbuilder.S return txInput, sigInst, nil } - address, err := common.DecodeAddress(u.Address, &consensus.MainNetParams) + address, err := common.DecodeAddress(u.Address, consensus.ActiveNetParams) if err != nil { return nil, nil, err } diff --git a/api/accounts.go b/api/accounts.go index c5b63715..0b721d58 100644 --- a/api/accounts.go +++ b/api/accounts.go @@ -2,6 +2,7 @@ package api import ( "context" + log "github.com/sirupsen/logrus" "github.com/bytom/account" @@ -13,9 +14,9 @@ import ( // POST /create-account func (a *API) createAccount(ctx context.Context, ins struct { - RootXPubs []chainkd.XPub `json:"root_xpubs"` - Quorum int `json:"quorum"` - Alias string `json:"alias"` + RootXPubs []chainkd.XPub `json:"root_xpubs"` + Quorum int `json:"quorum"` + Alias string `json:"alias"` }) Response { acc, err := a.wallet.AccountMgr.Create(ctx, ins.RootXPubs, ins.Quorum, ins.Alias) if err != nil { @@ -51,7 +52,7 @@ func (a *API) validateAddress(ctx context.Context, ins struct { Vaild: false, IsLocal: false, } - address, err := common.DecodeAddress(ins.Address, &consensus.MainNetParams) + address, err := common.DecodeAddress(ins.Address, consensus.ActiveNetParams) if err != nil { return NewSuccessResponse(resp) } diff --git a/blockchain/txbuilder/actions.go b/blockchain/txbuilder/actions.go index 84dac5c3..01bbd895 100644 --- a/blockchain/txbuilder/actions.go +++ b/blockchain/txbuilder/actions.go @@ -72,7 +72,7 @@ func (a *controlAddressAction) Build(ctx context.Context, b *TemplateBuilder) er return MissingFieldsError(missing...) } - address, err := common.DecodeAddress(a.Address, &consensus.MainNetParams) + address, err := common.DecodeAddress(a.Address, consensus.ActiveNetParams) if err != nil { return err } diff --git a/consensus/general.go b/consensus/general.go index 137d3aa7..53406d77 100644 --- a/consensus/general.go +++ b/consensus/general.go @@ -90,6 +90,8 @@ type Params struct { Bech32HRPSegwit string } +var ActiveNetParams = &MainNetParams + // MainNetParams is the config for production var MainNetParams = Params{ Name: "main", diff --git a/node/node.go b/node/node.go index 12dd442c..46909cff 100644 --- a/node/node.go +++ b/node/node.go @@ -18,6 +18,7 @@ import ( "github.com/bytom/blockchain/pseudohsm" "github.com/bytom/blockchain/txfeed" cfg "github.com/bytom/config" + "github.com/bytom/consensus" "github.com/bytom/crypto/ed25519/chainkd" "github.com/bytom/database/leveldb" "github.com/bytom/env" @@ -58,7 +59,9 @@ type Node struct { func NewNode(config *cfg.Config) *Node { ctx := context.Background() - + if config.ChainID == "testnet" { + consensus.ActiveNetParams = &consensus.TestNetParams + } // Get store txDB := dbm.NewDB("txdb", config.DBBackend, config.DBDir()) store := leveldb.NewStore(txDB) diff --git a/wallet/annotated.go b/wallet/annotated.go index c7332659..571f8524 100644 --- a/wallet/annotated.go +++ b/wallet/annotated.go @@ -241,7 +241,7 @@ func (w *Wallet) getAddressFromControlProgram(prog []byte) string { } func buildP2PKHAddress(pubHash []byte) string { - address, err := common.NewAddressWitnessPubKeyHash(pubHash, &consensus.MainNetParams) + address, err := common.NewAddressWitnessPubKeyHash(pubHash, consensus.ActiveNetParams) if err != nil { return "" } @@ -250,7 +250,7 @@ func buildP2PKHAddress(pubHash []byte) string { } func buildP2SHAddress(scriptHash []byte) string { - address, err := common.NewAddressWitnessScriptHash(scriptHash, &consensus.MainNetParams) + address, err := common.NewAddressWitnessScriptHash(scriptHash, consensus.ActiveNetParams) if err != nil { return "" } -- 2.11.0