-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
348 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package simulator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
// Copyright (c) 2020 The qitmeer developers | ||
// Use of this source code is governed by an ISC | ||
// license that can be found in the LICENSE file. | ||
|
||
package testprivatekey | ||
|
||
import ( | ||
"encoding/binary" | ||
"encoding/hex" | ||
"github.com/Qitmeer/qng/common/hash" | ||
"github.com/Qitmeer/qng/crypto/bip32" | ||
"github.com/Qitmeer/qng/params" | ||
) | ||
|
||
const ( | ||
CoinbaseIdx = 0 | ||
Password = "12345" | ||
) | ||
|
||
var ( | ||
// the default seed used in the testWallet | ||
defaultSeed = [hash.HashSize]byte{ | ||
0x7e, 0x44, 0x5a, 0xa5, 0xff, 0xd8, 0x34, 0xcb, | ||
0x2d, 0x3b, 0x2d, 0xb5, 0x0f, 0x89, 0x97, 0xdd, | ||
0x21, 0xaf, 0x29, 0xbe, 0xc3, 0xd2, 0x96, 0xaa, | ||
0xa0, 0x66, 0xd9, 0x02, 0xb9, 0x3f, 0x48, 0x4b, | ||
} | ||
) | ||
|
||
type Builder struct { | ||
// the bip32 master extended private key from a seed | ||
hdMaster *bip32.Key | ||
// the next hd child number from the master | ||
hdChildNumer uint32 | ||
// privkeys cached all private keys which derived from the master private key. | ||
// the keys of the private key map are their hd child number. | ||
privkeys map[uint32][]byte | ||
} | ||
|
||
func NewBuilder(id uint32) (*Builder, error) { | ||
params := params.ActiveNetParams.Params | ||
// The final seed is seed || nodeId, the purpose to make sure that each harness | ||
// node use a deterministic private key based on the its node id. | ||
var finalSeed [hash.HashSize + 4]byte | ||
// t.Logf("seed is %v",hexutil.Encode(seed[:])) | ||
copy(finalSeed[:], defaultSeed[:]) | ||
// t.Logf("finalseed is %v",hexutil.Encode(finalSeed[:])) | ||
binary.LittleEndian.PutUint32(finalSeed[hash.HashSize:], id) | ||
version := bip32.Bip32Version{ | ||
PrivKeyVersion: params.HDPrivateKeyID[:], | ||
PubKeyVersion: params.HDPublicKeyID[:], | ||
} | ||
// t.Logf("finalseed is %v",hexutil.Encode(finalSeed[:])) | ||
hdMaster, err := bip32.NewMasterKey2(finalSeed[:], version) | ||
if err != nil { | ||
return nil, err | ||
} | ||
child0, err := hdMaster.NewChildKey(0) | ||
if err != nil { | ||
return nil, err | ||
} | ||
key0 := child0.Key | ||
privkeys := make(map[uint32][]byte) | ||
privkeys[0] = key0 | ||
|
||
return &Builder{ | ||
hdMaster: hdMaster, | ||
hdChildNumer: 1, | ||
privkeys: privkeys, | ||
}, nil | ||
} | ||
|
||
func (b *Builder) Build() ([]byte, error) { | ||
num := b.hdChildNumer | ||
childx, err := b.hdMaster.NewChildKey(num) | ||
if err != nil { | ||
return nil, err | ||
} | ||
b.privkeys[num] = childx.Key | ||
b.hdChildNumer++ | ||
return childx.Key, nil | ||
} | ||
|
||
func (b *Builder) Get(idx int) []byte { | ||
if idx >= len(b.privkeys) { | ||
return nil | ||
} | ||
return b.privkeys[uint32(idx)] | ||
} | ||
|
||
func (b *Builder) GetHex(idx int) string { | ||
if idx >= len(b.privkeys) { | ||
return "" | ||
} | ||
return hex.EncodeToString(b.privkeys[uint32(idx)]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package testprivatekey | ||
|
||
import ( | ||
"github.com/Qitmeer/qng/common/util/hexutil" | ||
"github.com/Qitmeer/qng/params" | ||
"testing" | ||
) | ||
|
||
var ( | ||
expect = struct { | ||
ver string | ||
key string | ||
chaincode string | ||
priv0 string | ||
priv1 string | ||
}{ | ||
"0x040bee6e", | ||
"0x38015593945529cc0bd761108ad2fbd98a3f5f8e030c5acd3747ce3e54d95c16", | ||
"0x4eb4e56ada09795313734db329c362923c5b6fac75b924780e68b9c9b18a24b3", | ||
"0xe0b26a52b1a9676a365d6452fb04a1c05b58e959683862d73105e58d4416baba", | ||
"0xfff2cefe258ca60ae5f5abec99b5d63e2a561c40d784ee50b04eddf8efc84b0d", | ||
} | ||
) | ||
|
||
func TestPrivateKeyBuild(t *testing.T) { | ||
params.ActiveNetParams = ¶ms.PrivNetParam | ||
pb, err := NewBuilder(0) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if hexutil.Encode(pb.hdMaster.Key) != expect.key { | ||
t.Fatalf("hd master key not matched, expect %v but got %v", pb.hdMaster.Key, expect.key) | ||
} | ||
if hexutil.Encode(pb.hdMaster.Version) != expect.ver { | ||
t.Fatalf("hd master version not matched, expect %v but got %v", pb.hdMaster.Version, expect.ver) | ||
} | ||
if hexutil.Encode(pb.hdMaster.ChainCode) != expect.chaincode { | ||
t.Fatalf("hd master chain code not matched, expect %v but got %v", pb.hdMaster.ChainCode, expect.chaincode) | ||
} | ||
|
||
_, err = pb.Build() | ||
if err != nil { | ||
t.Fatalf("failed get new address : %v", err) | ||
} | ||
if hexutil.Encode(pb.Get(0)) != expect.priv0 { | ||
t.Fatalf("hd key0 priv key not matched, expect %x but got %v", pb.Get(0), expect.priv0) | ||
} | ||
if hexutil.Encode(pb.Get(1)) != expect.priv1 { | ||
t.Fatalf("hd key0 priv key not matched, expect %x but got %v", pb.Get(1), expect.priv1) | ||
} | ||
} |
Oops, something went wrong.