OSDN Git Service

update
authorChengcheng Zhang <943420582@qq.com>
Thu, 23 May 2019 16:47:21 +0000 (00:47 +0800)
committerChengcheng Zhang <943420582@qq.com>
Thu, 23 May 2019 16:47:21 +0000 (00:47 +0800)
account/accounts.go
account/accounts_test.go
api/message.go
api/query.go
blockchain/pseudohsm/pseudohsm.go
blockchain/pseudohsm/pseudohsm_test.go
blockchain/txbuilder/txbuilder_test.go
crypto/csp/util.go
crypto/ed25519/chainkd/serialize.go

index 2db1b2b..fc5497d 100644 (file)
@@ -4,7 +4,6 @@ package account
 import (
        "encoding/json"
        "fmt"
-       "os"
        "reflect"
        "sort"
        "strings"
@@ -180,12 +179,7 @@ func CreateAccount(xpubs []crypto.XPubKeyer, quorum int, alias string, acctIndex
 }
 
 func (m *Manager) saveAccount(account *Account, updateIndex bool) error {
-       fmt.Println("saveAccount account.XPubs is:", account)
        rawAccount, err := json.Marshal(account)
-       ///////
-       fmt.Println("rawaccount is:", rawAccount)
-       os.Stdout.Write(rawAccount)
-       ///////
        if err != nil {
                return ErrMarshalAccount
        }
@@ -290,7 +284,6 @@ func (m *Manager) CreateAddress(accountID string, change bool) (cp *CtrlProgram,
 
        account, err := m.FindByID(accountID)
        if err != nil {
-               fmt.Println("CreateAddress 22", err)
                return nil, err
        }
 
@@ -437,14 +430,7 @@ func (m *Manager) FindByID(id string) (*Account, error) {
        }
 
        account := &Account{}
-       fmt.Println("FindByID account is:111", account)
-       fmt.Println("FindByID rawaccount is:", rawAccount, "====")
-       os.Stdout.Write(rawAccount)
-       fmt.Println("====")
        if err := json.Unmarshal(rawAccount, account); err != nil {
-               fmt.Print("FindByID err: ", err)
-               fmt.Println("FindByID account is:222", account)
-               fmt.Println("FindByID account.XPubs is:333", account.XPubs)
                return nil, err
        }
 
@@ -692,13 +678,20 @@ func CreateCtrlProgram(account *Account, addrIdx uint64, change bool) (cp *CtrlP
 }
 
 func createP2PKH(account *Account, path [][]byte) (*CtrlProgram, error) {
+       fmt.Println("createP2PKH account.XPubs is:", account.XPubs)
        derivedXPubs := csp.DeriveXPubs(account.XPubs, path)
+       fmt.Println("createP2PKH derivedXPubs is:", derivedXPubs)
        pubHash := make([]byte, 0)
+       fmt.Println("createP2PKH len(derivedXPubs) is:", len(derivedXPubs))
+       if len(derivedXPubs) == 0 {
+               return nil, errors.New("no derived XPub.")
+       }
        switch dxpub := derivedXPubs[0].(type) {
        case edchainkd.XPub:
                derivedPK := dxpub.PublicKey()
                pubHash = crypto.Ripemd160(derivedPK)
        }
+       fmt.Println("createP2PKH ...")
 
        address, err := common.NewAddressWitnessPubKeyHash(pubHash, &consensus.ActiveNetParams)
        if err != nil {
index 8386453..2cc1dc5 100644 (file)
@@ -1,6 +1,7 @@
 package account
 
 import (
+       "fmt"
        "io/ioutil"
        "os"
        "reflect"
@@ -10,7 +11,7 @@ import (
        "github.com/vapor/blockchain/pseudohsm"
        "github.com/vapor/blockchain/signers"
        "github.com/vapor/config"
-       edchainkd "github.com/vapor/crypto/ed25519/chainkd"
+       vcrypto "github.com/vapor/crypto"
        "github.com/vapor/database"
        dbm "github.com/vapor/database/leveldb"
        "github.com/vapor/errors"
@@ -22,7 +23,8 @@ import (
 func TestCreateAccountWithUppercase(t *testing.T) {
        m := mockAccountManager(t)
        alias := "UPPER"
-       account, err := m.Create([]edchainkd.XPub{testutil.TestXPub}, 1, alias, signers.BIP0044)
+       xpubs := []vcrypto.XPubKeyer{testutil.TestXPub}
+       account, err := m.Create(xpubs, 1, alias, signers.BIP0044)
 
        if err != nil {
                t.Fatal(err)
@@ -36,7 +38,8 @@ func TestCreateAccountWithUppercase(t *testing.T) {
 func TestCreateAccountWithSpaceTrimed(t *testing.T) {
        m := mockAccountManager(t)
        alias := " with space "
-       account, err := m.Create([]edchainkd.XPub{testutil.TestXPub}, 1, alias, signers.BIP0044)
+       xpubs := []vcrypto.XPubKeyer{testutil.TestXPub}
+       account, err := m.Create(xpubs, 1, alias, signers.BIP0044)
 
        if err != nil {
                t.Fatal(err)
@@ -59,15 +62,18 @@ func TestCreateAccountWithSpaceTrimed(t *testing.T) {
 
 func TestCreateAccount(t *testing.T) {
        m := mockAccountManager(t)
-       account, err := m.Create([]edchainkd.XPub{testutil.TestXPub}, 1, "test-alias", signers.BIP0044)
+       xpubs := []vcrypto.XPubKeyer{testutil.TestXPub}
+       account, err := m.Create(xpubs, 1, "test-alias", signers.BIP0044)
        if err != nil {
                testutil.FatalErr(t, err)
        }
+       fmt.Println("account is:", account, "account xpub:", account.XPubs)
 
        found, err := m.FindByID(account.ID)
        if err != nil {
                t.Errorf("unexpected error %v", err)
        }
+       fmt.Println("found is:", found, "found xpub:", found.XPubs)
        if !testutil.DeepEqual(account, found) {
                t.Errorf("expected account %v to be recorded as %v", account, found)
        }
@@ -77,7 +83,8 @@ func TestCreateAccountReusedAlias(t *testing.T) {
        m := mockAccountManager(t)
        m.createTestAccount(t, "test-alias", nil)
 
-       _, err := m.Create([]edchainkd.XPub{testutil.TestXPub}, 1, "test-alias", signers.BIP0044)
+       xpubs := []vcrypto.XPubKeyer{testutil.TestXPub}
+       _, err := m.Create(xpubs, 1, "test-alias", signers.BIP0044)
        if errors.Root(err) != ErrDuplicateAlias {
                t.Errorf("expected %s when reusing an alias, got %v", ErrDuplicateAlias, err)
        }
@@ -119,12 +126,13 @@ func TestUpdateAccountAlias(t *testing.T) {
 func TestDeleteAccount(t *testing.T) {
        m := mockAccountManager(t)
 
-       account1, err := m.Create([]edchainkd.XPub{testutil.TestXPub}, 1, "test-alias1", signers.BIP0044)
+       xpubs := []vcrypto.XPubKeyer{testutil.TestXPub}
+       account1, err := m.Create(xpubs, 1, "test-alias1", signers.BIP0044)
        if err != nil {
                testutil.FatalErr(t, err)
        }
 
-       account2, err := m.Create([]edchainkd.XPub{testutil.TestXPub}, 1, "test-alias2", signers.BIP0044)
+       account2, err := m.Create(xpubs, 1, "test-alias2", signers.BIP0044)
        if err != nil {
                testutil.FatalErr(t, err)
        }
@@ -194,10 +202,9 @@ func TestGetAccountIndexKey(t *testing.T) {
                t.Fatal(err)
        }
 
-       xpubs1 := []edchainkd.XPub{xpub1.XPub, xpub2.XPub}
-       xpubs2 := []edchainkd.XPub{xpub2.XPub, xpub1.XPub}
+       xpubs1 := []vcrypto.XPubKeyer{xpub1.XPub, xpub2.XPub}
+       xpubs2 := []vcrypto.XPubKeyer{xpub2.XPub, xpub1.XPub}
        if !reflect.DeepEqual(GetAccountIndexKey(xpubs1), GetAccountIndexKey(xpubs2)) {
-               // fmt.Printf("xpubs1 is: %s, xpubs2 is: %s", xpubs1, xpubs2)
                t.Fatal("GetAccountIndexKey test err")
        }
 
@@ -227,7 +234,8 @@ func mockAccountManager(t *testing.T) *Manager {
 }
 
 func (m *Manager) createTestAccount(t testing.TB, alias string, tags map[string]interface{}) *Account {
-       account, err := m.Create([]edchainkd.XPub{testutil.TestXPub}, 1, alias, signers.BIP0044)
+       xpubs := []vcrypto.XPubKeyer{testutil.TestXPub}
+       account, err := m.Create(xpubs, 1, alias, signers.BIP0044)
        if err != nil {
                testutil.FatalErr(t, err)
        }
index c18b447..29310cd 100644 (file)
@@ -12,6 +12,7 @@ import (
        vcrypto "github.com/vapor/crypto"
        "github.com/vapor/crypto/csp"
        "github.com/vapor/crypto/ed25519"
+       edchainkd "github.com/vapor/crypto/ed25519/chainkd"
        chainjson "github.com/vapor/encoding/json"
 )
 
@@ -68,25 +69,21 @@ func (a *API) verifyMessage(ctx context.Context, ins struct {
                return NewErrorResponse(err)
        }
 
-       derivedPK := ins.DerivedXPub.PublicKey()
-       pubHash := make([]byte, 20)
-       switch dpk := derivedPK.(type) {
-       case ed25519.PublicKey:
-               pubHash = crypto.Ripemd160(dpk)
-       }
-       addressPubHash, err := common.NewAddressWitnessPubKeyHash(pubHash, &consensus.ActiveNetParams)
-       if err != nil {
-               return NewErrorResponse(err)
-       }
+       switch dXPub := ins.DerivedXPub.(type) {
+       case edchainkd.XPub:
+               derivedPK := dXPub.PublicKey()
+               pubHash := crypto.Ripemd160(derivedPK)
+               addressPubHash, err := common.NewAddressWitnessPubKeyHash(pubHash, &consensus.ActiveNetParams)
+               if err != nil {
+                       return NewErrorResponse(err)
+               }
 
-       address := addressPubHash.EncodeAddress()
-       if address != strings.TrimSpace(ins.Address) {
-               return NewSuccessResponse(VerifyMsgResp{VerifyResult: false})
-       }
+               address := addressPubHash.EncodeAddress()
+               if address != strings.TrimSpace(ins.Address) {
+                       return NewSuccessResponse(VerifyMsgResp{VerifyResult: false})
+               }
 
-       switch dpk := derivedPK.(type) {
-       case ed25519.PublicKey:
-               if ed25519.Verify(dpk, ins.Message, sig) {
+               if ed25519.Verify(derivedPK, ins.Message, sig) {
                        return NewSuccessResponse(VerifyMsgResp{VerifyResult: true})
                }
        }
index 247c7a2..0030a1e 100644 (file)
@@ -16,6 +16,7 @@ import (
        "github.com/vapor/consensus"
        vcrypto "github.com/vapor/crypto"
        "github.com/vapor/crypto/ed25519"
+       edchainkd "github.com/vapor/crypto/ed25519/chainkd"
        chainjson "github.com/vapor/encoding/json"
        "github.com/vapor/errors"
        "github.com/vapor/protocol/bc"
@@ -341,8 +342,12 @@ func getPubkey(account *account.Account, change bool, index uint64) (*crypto.Pub
        if err != nil {
                return nil, nil, err
        }
-       derivedXPub := account.XPubs[0].Derive(rawPath)
-       pubkey := derivedXPub.PublicKey()
+       var pubkey crypto.PublicKey
+       switch xpub := account.XPubs[0].(type) {
+       case edchainkd.XPub:
+               derivedXPub := xpub.Derive(rawPath)
+               pubkey = derivedXPub.PublicKey()
+       }
        var path []chainjson.HexBytes
        for _, p := range rawPath {
                path = append(path, chainjson.HexBytes(p))
index 0d6e9ce..a68a5d9 100644 (file)
@@ -3,9 +3,7 @@ package pseudohsm
 
 import (
        "bytes"
-       "encoding/hex"
        "encoding/json"
-       "fmt"
        "io/ioutil"
        "os"
        "path/filepath"
@@ -73,7 +71,6 @@ func (h *HSM) XCreate(alias string, auth string, language string) (*XPub, *strin
                return nil, nil, err
        }
        h.cache.add(*xpub)
-       fmt.Println("XCreate xpub is:", xpub)
        return xpub, mnemonic, err
 }
 
@@ -111,9 +108,7 @@ func (h *HSM) ImportKeyFromMnemonic(alias string, auth string, mnemonic string,
 func (h *HSM) createKeyFromMnemonic(alias string, auth string, mnemonic string) (*XPub, error) {
        // Generate a Bip32 HD wallet for the mnemonic and a user supplied password
        seed := mnem.NewSeed(mnemonic, "")
-       fmt.Println("createKeyFromMnemonic seed is:", hex.EncodeToString(seed))
        xprv, xpub, err := csp.NewXKeys(bytes.NewBuffer(seed))
-       fmt.Println("createKeyFromMnemonic xprv is: ", xprv, " xpub is: ", xpub)
        if err != nil {
                return nil, err
        }
@@ -142,12 +137,10 @@ func (h *HSM) createChainKDKey(alias string, auth string, language string) (*XPu
        if err != nil {
                return nil, nil, err
        }
-       fmt.Println("createChainKDKey mnemonic is:", mnemonic)
        xpub, err := h.createKeyFromMnemonic(alias, auth, mnemonic)
        if err != nil {
                return nil, nil, err
        }
-       fmt.Println("createChainKDKey xpub is:", xpub)
        return xpub, &mnemonic, nil
 }
 
index 673c1f9..6c8b526 100644 (file)
@@ -8,6 +8,7 @@ import (
        "testing"
 
        "github.com/vapor/crypto/ed25519"
+       edchainkd "github.com/vapor/crypto/ed25519/chainkd"
        "github.com/vapor/errors"
 )
 
@@ -96,22 +97,33 @@ func TestPseudoHSMChainKDKeys(t *testing.T) {
        if err != nil {
                t.Fatal(err)
        }
-       if !xpub.XPub.Verify(msg, sig) {
-               t.Error("expected verify to succeed")
+       switch xpubkey := xpub.XPub.(type) {
+       case edchainkd.XPub:
+               if !xpubkey.Verify(msg, sig) {
+                       t.Error("expected verify to succeed")
+               }
        }
-       if xpub2.XPub.Verify(msg, sig) {
-               t.Error("expected verify with wrong pubkey to fail")
+
+       switch xpubkey2 := xpub2.XPub.(type) {
+       case edchainkd.XPub:
+               if xpubkey2.Verify(msg, sig) {
+                       t.Error("expected verify with wrong pubkey to fail")
+               }
        }
+
        path := [][]byte{{3, 2, 6, 3, 8, 2, 7}}
        sig, err = hsm.XSign(xpub2.XPub, path, msg, "nopassword")
        if err != nil {
                t.Fatal(err)
        }
-       if xpub2.XPub.Verify(msg, sig) {
-               t.Error("expected verify with underived pubkey of sig from derived privkey to fail")
-       }
-       if !xpub2.XPub.Derive(path).Verify(msg, sig) {
-               t.Error("expected verify with derived pubkey of sig from derived privkey to succeed")
+       switch xpubkey2 := xpub2.XPub.(type) {
+       case edchainkd.XPub:
+               if xpubkey2.Verify(msg, sig) {
+                       t.Error("expected verify with underived pubkey of sig from derived privkey to fail")
+               }
+               if !xpubkey2.Derive(path).Verify(msg, sig) {
+                       t.Error("expected verify with derived pubkey of sig from derived privkey to succeed")
+               }
        }
 
        xpubs := hsm.ListKeys()
@@ -154,28 +166,21 @@ func TestSignAndVerifyMessage(t *testing.T) {
        }
 
        path := [][]byte{{3, 2, 6, 3, 8, 2, 7}}
-       derivedXPub := xpub.XPub.Derive(path)
-
        msg := "this is a test message"
        sig, err := hsm.XSign(xpub.XPub, path, []byte(msg), "password")
        if err != nil {
                t.Fatal(err)
        }
-
-       dePublicKey := derivedXPub.PublicKey()
-       switch pubKey := dePublicKey.(type) {
-       case ed25519.PublicKey:
+       switch xpubkey := xpub.XPub.(type) {
+       case edchainkd.XPub:
+               derivedXPub := xpubkey.Derive(path)
                // derivedXPub verify success
-               if !ed25519.Verify(pubKey, []byte(msg), sig) {
+               if !ed25519.Verify(derivedXPub.PublicKey(), []byte(msg), sig) {
                        t.Fatal("right derivedXPub verify sign failed")
                }
-       }
 
-       roPublicKey := xpub.XPub.PublicKey()
-       switch pubKey := roPublicKey.(type) {
-       case ed25519.PublicKey:
                // rootXPub verify failed
-               if ed25519.Verify(pubKey, []byte(msg), sig) {
+               if ed25519.Verify(xpubkey.PublicKey(), []byte(msg), sig) {
                        t.Fatal("right rootXPub verify derivedXPub sign succeed")
                }
        }
index 68f4b7a..ddd621f 100644 (file)
@@ -12,10 +12,9 @@ import (
 
        "github.com/vapor/common"
        "github.com/vapor/consensus"
-       vcrypto "github.com/vapor/crypto"
+       "github.com/vapor/crypto"
        "github.com/vapor/crypto/csp"
-       "github.com/vapor/crypto/ed25519"
-       "github.com/vapor/crypto/ed25519/chainkd"
+       edchainkd "github.com/vapor/crypto/ed25519/chainkd"
        chainjson "github.com/vapor/encoding/json"
        "github.com/vapor/errors"
        "github.com/vapor/protocol/bc"
@@ -227,16 +226,17 @@ func TestCreateTxByUtxo(t *testing.T) {
                t.Fatal(err)
        }
 
-       pub := xpub.PublicKey()
        pubHash := make([]byte, 0)
-       switch pubKey := pub.(type) {
-       case ed25519.PublicKey:
-               pubHash = vcrypto.Ripemd160(pubKey)
-       }
-
-       program, err := vmutil.P2WPKHProgram([]byte(pubHash))
-       if err != nil {
-               t.Fatal(err)
+       program := make([]byte, 0)
+       pub := make([]byte, 0)
+       switch xpubkey := xpub.(type) {
+       case edchainkd.XPub:
+               pub = xpubkey.PublicKey()
+               pubHash = crypto.Ripemd160(pub)
+               program, err = vmutil.P2WPKHProgram([]byte(pubHash))
+               if err != nil {
+                       t.Fatal(err)
+               }
        }
 
        address, err := common.NewAddressWitnessPubKeyHash(pubHash, &consensus.ActiveNetParams)
@@ -278,12 +278,12 @@ func TestCreateTxByUtxo(t *testing.T) {
        }
 
        h := tpl.Hash(0).Byte32()
-       sig := xprv.Sign(h[:])
-       data := make([]byte, 0)
-       switch pubKey := pub.(type) {
-       case ed25519.PublicKey:
-               data = []byte(pubKey)
+       sig := make([]byte, 0)
+       switch xprvkey := xprv.(type) {
+       case edchainkd.XPrv:
+               sig = xprvkey.Sign(h[:])
        }
+       data := []byte(pub)
 
        // Test with more signatures than required, in correct order
        tpl.SigningInstructions = []*SigningInstruction{{
@@ -312,7 +312,7 @@ func TestAddContractArgs(t *testing.T) {
        }
 
        // TODO: it should use map[string]string adapt several algorithm
-       var xpub chainkd.XPub
+       var xpub edchainkd.XPub
        copy(xpub[:], hexXpub)
 
        rawTxSig := RawTxSigArgument{RootXPub: xpub, Path: []chainjson.HexBytes{{1, 1, 0, 0, 0, 0, 0, 0, 0}, {1, 0, 0, 0, 0, 0, 0, 0}}}
index a464c96..287f860 100644 (file)
@@ -3,7 +3,9 @@ package csp
 
 import (
        "crypto"
+       "fmt"
        "io"
+       "reflect"
 
        vcrypto "github.com/vapor/crypto"
        edchainkd "github.com/vapor/crypto/ed25519/chainkd"
@@ -30,14 +32,36 @@ func XPubKeys(xpubs []vcrypto.XPubKeyer) []crypto.PublicKey {
        return res
 }
 
+// func DeriveXPubs(xpubs []vcrypto.XPubKeyer, path [][]byte) []vcrypto.XPubKeyer {
+//     res := make([]vcrypto.XPubKeyer, 0, len(xpubs))
+//     for _, xpub := range xpubs {
+//             switch xpb := xpub.(type) {
+//             case edchainkd.XPub:
+//                     d := xpb.Derive(path)
+//                     res = append(res, d)
+//                     fmt.Println("DeriveXPubs d is:", d)
+//             }
+//     }
+//     fmt.Println("DeriveXPubs len(res) is:", len(res))
+//     return res
+// }
+
 func DeriveXPubs(xpubs []vcrypto.XPubKeyer, path [][]byte) []vcrypto.XPubKeyer {
        res := make([]vcrypto.XPubKeyer, 0, len(xpubs))
        for _, xpub := range xpubs {
-               switch xpb := xpub.(type) {
-               case edchainkd.XPub:
+               fmt.Println("DeriveXPubs =====", reflect.TypeOf(xpubs[0]))
+               if xpb, ok := xpub.(edchainkd.XPub); ok {
                        d := xpb.Derive(path)
                        res = append(res, d)
+                       fmt.Println("DeriveXPubs d is:", d)
                }
+               // switch xpb := xpub.(type) {
+               // case edchainkd.XPub:
+               //      d := xpb.Derive(path)
+               //      res = append(res, d)
+               //      fmt.Println("DeriveXPubs d is:", d)
+               // }
        }
+       fmt.Println("DeriveXPubs len(res) is:", len(res))
        return res
 }
index 8ca33a3..a6fe639 100644 (file)
@@ -3,7 +3,6 @@ package chainkd
 import (
        "encoding/hex"
        "errors"
-       "fmt"
 )
 
 const (
@@ -41,7 +40,6 @@ func (xpub *XPub) UnmarshalText(inp []byte) error {
                return ErrBadKeyStr
        }
        _, err := hex.Decode(xpub[:], inp)
-       fmt.Println("UnmarshalText is:", xpub)
        return err
 }