From a36fbe53c86ee7a18d819a3727e2c0adc0bb99c5 Mon Sep 17 00:00:00 2001 From: Jin Date: Mon, 12 Aug 2019 07:40:25 +0800 Subject: [PATCH 1/2] Modify package name --- core/bip32verflag.go | 58 +++++++++ core/common.go | 11 ++ core/derivepathflag.go | 24 ++++ core/handle_addr.go | 22 ++++ core/handle_base58.go | 169 ++++++++++++++++++++++++ core/handle_base64.go | 27 ++++ core/handle_crypto.go | 257 +++++++++++++++++++++++++++++++++++++ core/handle_hash.go | 89 +++++++++++++ core/handle_message.go | 143 +++++++++++++++++++++ core/handle_rlp.go | 100 +++++++++++++++ core/handle_tx.go | 130 +++++++++++++++++++ core/qitmeer58checkflag.go | 37 ++++++ core/txflag.go | 130 +++++++++++++++++++ qx.go | 162 +++++++++++------------ 14 files changed, 1278 insertions(+), 81 deletions(-) create mode 100644 core/bip32verflag.go create mode 100644 core/common.go create mode 100644 core/derivepathflag.go create mode 100644 core/handle_addr.go create mode 100644 core/handle_base58.go create mode 100644 core/handle_base64.go create mode 100644 core/handle_crypto.go create mode 100644 core/handle_hash.go create mode 100644 core/handle_message.go create mode 100644 core/handle_rlp.go create mode 100644 core/handle_tx.go create mode 100644 core/qitmeer58checkflag.go create mode 100644 core/txflag.go diff --git a/core/bip32verflag.go b/core/bip32verflag.go new file mode 100644 index 0000000..71bad2a --- /dev/null +++ b/core/bip32verflag.go @@ -0,0 +1,58 @@ +// Copyright 2017-2018 The qitmeer developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. +package core + +import ( + "fmt" + "github.com/HalalChain/qitmeer-lib/crypto/bip32" + "github.com/HalalChain/qitmeer-lib/params" +) + +var ( + QitmeerMainnetBip32Version = bip32.Bip32Version{PrivKeyVersion: params.MainNetParams.HDPrivateKeyID[:], PubKeyVersion: params.MainNetParams.HDPublicKeyID[:]} + QitmeerTestnetBip32Version = bip32.Bip32Version{PrivKeyVersion: params.TestNetParams.HDPrivateKeyID[:], PubKeyVersion: params.TestNetParams.HDPublicKeyID[:]} + QitmeerPrivnetBip32Version = bip32.Bip32Version{PrivKeyVersion: params.PrivNetParams.HDPrivateKeyID[:], PubKeyVersion: params.PrivNetParams.HDPublicKeyID[:]} +) + +type Bip32VersionFlag struct { + Version bip32.Bip32Version + flag string +} + +func (v *Bip32VersionFlag) String() string { + return v.flag +} + +func (v *Bip32VersionFlag) Set(versionFlag string) error { + var version bip32.Bip32Version + switch versionFlag { + case "bip32", "btc": + version = bip32.DefaultBip32Version + case "mainnet": + version = QitmeerMainnetBip32Version + case "testnet": + version = QitmeerTestnetBip32Version + case "privnet": + version = QitmeerPrivnetBip32Version + default: + return fmt.Errorf("unknown bip32 version flag %s", versionFlag) + } + v.Version = version + v.flag = versionFlag + return nil +} + +func GetBip32NetworkInfo(rawVersionByte []byte) string { + if QitmeerMainnetBip32Version.IsPrivkeyVersion(rawVersionByte) || QitmeerMainnetBip32Version.IsPubkeyVersion(rawVersionByte) { + return "qx mainet" + } else if QitmeerTestnetBip32Version.IsPrivkeyVersion(rawVersionByte) || QitmeerTestnetBip32Version.IsPubkeyVersion(rawVersionByte) { + return "qx testnet" + } else if QitmeerPrivnetBip32Version.IsPrivkeyVersion(rawVersionByte) || QitmeerPrivnetBip32Version.IsPubkeyVersion(rawVersionByte) { + return "qx privnet" + } else if bip32.DefaultBip32Version.IsPrivkeyVersion(rawVersionByte) || bip32.DefaultBip32Version.IsPubkeyVersion(rawVersionByte) { + return "btc mainnet" + } else { + return "unknown" + } +} diff --git a/core/common.go b/core/common.go new file mode 100644 index 0000000..bdc3241 --- /dev/null +++ b/core/common.go @@ -0,0 +1,11 @@ +package core + +import ( + "fmt" + "os" +) + +func ErrExit(err error){ + fmt.Fprintf(os.Stderr, "Qx Error : %q\n",err) + os.Exit(1) +} \ No newline at end of file diff --git a/core/derivepathflag.go b/core/derivepathflag.go new file mode 100644 index 0000000..3d84455 --- /dev/null +++ b/core/derivepathflag.go @@ -0,0 +1,24 @@ +// Copyright 2017-2018 The qitmeer developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. +package core + +import "github.com/HalalChain/qitmeer-lib/wallet" + +type DerivePathFlag struct { + Path wallet.DerivationPath +} + +func (d *DerivePathFlag) Set(s string) error { + path, err := wallet.ParseDerivationPath(s) + if err!=nil { + return err + } + d.Path = path + return nil +} + +func (d *DerivePathFlag) String() string { + return d.Path.String() +} + diff --git a/core/handle_addr.go b/core/handle_addr.go new file mode 100644 index 0000000..becbf7b --- /dev/null +++ b/core/handle_addr.go @@ -0,0 +1,22 @@ +// Copyright 2017-2018 The qitmeer developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. +package core + +import ( + "encoding/hex" + "fmt" + "github.com/HalalChain/qitmeer-lib/common/encode/base58" + "github.com/HalalChain/qitmeer-lib/common/hash" +) + +func EcPubKeyToAddress(version []byte, pubkey string) { + data, err :=hex.DecodeString(pubkey) + if err != nil { + ErrExit(err) + } + h := hash.Hash160(data) + + address := base58.QitmeerCheckEncode(h, version[:]) + fmt.Printf("%s\n",address) +} diff --git a/core/handle_base58.go b/core/handle_base58.go new file mode 100644 index 0000000..9b62240 --- /dev/null +++ b/core/handle_base58.go @@ -0,0 +1,169 @@ +// Copyright 2017-2018 The qitmeer developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. +package core + +import ( + "encoding/hex" + "fmt" + "github.com/HalalChain/qitmeer-lib/common/encode/base58" + "github.com/HalalChain/qitmeer-lib/common/hash" + "github.com/HalalChain/qitmeer-lib/common/util" + "github.com/pkg/errors" + "strconv" +) + +func Base58CheckEncode(version []byte, mode string,hasher string, cksumSize int, input string){ + if hasher != "" && mode != "qitmeer" { + ErrExit(fmt.Errorf("invaid flag -a %s with -m %s",hasher,mode)) + } + data, err := hex.DecodeString(input) + if err!=nil { + ErrExit(err) + } + var encoded string + + if hasher != "" { + var cksumfunc func([]byte) []byte + switch (hasher) { + case "sha256": + cksumfunc = base58.SingleHashChecksumFunc(hash.GetHasher(hash.SHA256), cksumSize) + case "dsha256": + cksumfunc = base58.DoubleHashChecksumFunc(hash.GetHasher(hash.SHA256), cksumSize) + case "blake2b256": + cksumfunc = base58.SingleHashChecksumFunc(hash.GetHasher(hash.Blake2b_256), cksumSize) + case "dblake2b256": + cksumfunc = base58.DoubleHashChecksumFunc(hash.GetHasher(hash.Blake2b_256), cksumSize) + case "blake2b512": + cksumfunc = base58.SingleHashChecksumFunc(hash.GetHasher(hash.Blake2b_512), cksumSize) + default: + err = fmt.Errorf("unknown hasher %s", hasher) + } + if err!=nil { + ErrExit(err) + } + encoded = base58.CheckEncode(data, version, cksumSize, cksumfunc) + }else { + switch mode { + case "qitmeer": + if len(version) != 2 { + ErrExit(fmt.Errorf("invaid version byte size for qitmeer base58 check encode. input = %x (len = %d, required 2)",version,len(version))) + } + encoded = base58.QitmeerCheckEncode(data, version[:]) + case "btc": + if len(version) > 1 { + ErrExit(fmt.Errorf("invaid version size for btc base58check encode")) + } + encoded = base58.BtcCheckEncode(data, version[0]) + case "ss": + encoded = base58.CheckEncode(data, version[:], 2, base58.SingleHashChecksumFunc(hash.GetHasher(hash.Blake2b_512), 2)) + default: + ErrExit(fmt.Errorf("unknown encode mode %s", mode)) + } + } + // Show the encoded data. + //fmt.Printf("Encoded Data ver[%v] : %s\n",ver, encoded) + fmt.Printf("%s\n",encoded) +} + +func Base58CheckDecode(mode, hasher string, versionSize, cksumSize int, input string,showDetails bool) { + var err error + var data []byte + var version []byte + if hasher != "" && mode != "qitmeer" { + ErrExit(fmt.Errorf("invaid flag -a %s with -m %s",hasher,mode)) + } + if hasher != "" { + var v []byte + switch hasher { + case "sha256": + data, v, err = base58.CheckDecode(input, versionSize, cksumSize, base58.SingleHashChecksumFunc(hash.GetHasher(hash.SHA256), cksumSize)) + case "dsha256": + data, v, err = base58.CheckDecode(input, versionSize, cksumSize, base58.DoubleHashChecksumFunc(hash.GetHasher(hash.SHA256), cksumSize)) + case "blake2b256": + data, v, err = base58.CheckDecode(input, versionSize, cksumSize, base58.SingleHashChecksumFunc(hash.GetHasher(hash.Blake2b_256), cksumSize)) + case "dblake2b256": + data, v, err = base58.CheckDecode(input, versionSize, cksumSize, base58.DoubleHashChecksumFunc(hash.GetHasher(hash.Blake2b_256), cksumSize)) + case "blake2b512": + data, v, err = base58.CheckDecode(input, versionSize, cksumSize, base58.SingleHashChecksumFunc(hash.GetHasher(hash.Blake2b_512), cksumSize)) + default: + err = fmt.Errorf("unknown hasher %s",hasher) + } + if err!=nil { + ErrExit(err) + } + version = v + }else { + switch mode { + case "btc": + v := byte(0) + data, v, err = base58.BtcCheckDecode(input) + if err != nil { + ErrExit(err) + } + version = []byte{0x0, v} + case "qitmeer": + v := [2]byte{} + data, v, err = base58.QitmeerCheckDecode(input) + if err != nil { + ErrExit(err) + } + version = []byte{v[0], v[1]} + case "ss": + var v []byte + data, v, err = base58.CheckDecode(input, 1, 2, base58.SingleHashChecksumFunc(hash.GetHasher(hash.Blake2b_512), 2)) + if err != nil { + ErrExit(err) + } + version = v + default: + ErrExit(fmt.Errorf("unknown mode %s", mode)) + } + } + if showDetails { + decoded := base58.Decode(input) + if hasher!="" { + fmt.Printf("hasher : %s\n", hasher) + }else { + fmt.Printf("mode : %s\n", mode) + } + version_d, err := strconv.ParseUint(fmt.Sprintf("%x",version[:]), 16, 64) + version_r := util.CopyBytes(version[:]) + util.ReverseBytes(version_r) + version_d2, err := strconv.ParseUint(fmt.Sprintf("%x",version_r[:]), 16, 64) + if err!=nil { + ErrExit(errors.Wrapf(err,"convert version %x error",version[:])) + } + fmt.Printf("version : %x (hex) %v (BE) %v (LE)\n", version, version_d, version_d2) + fmt.Printf("payload : %x\n", data) + cksum := decoded[len(decoded)-cksumSize:] + cksum_d, err := strconv.ParseUint(fmt.Sprintf("%x",cksum[:]), 16, 64) + if err!=nil { + ErrExit(errors.Wrapf(err,"convert version %x error",cksum[:])) + } + //convere to little endian + cksum_r := util.CopyBytes(cksum[:]) + util.ReverseBytes(cksum_r) + cksum_d2, err := strconv.ParseUint(fmt.Sprintf("%x",cksum_r[:]), 16, 64) + fmt.Printf("checksum: %x (hex) %v (BE) %v (LE)\n", cksum, cksum_d, cksum_d2) + + } else { + fmt.Printf("%x\n", data) + } +} + + +func Base58Encode(input string){ + data, err := hex.DecodeString(input) + if err!=nil { + ErrExit(err) + } + encoded := base58.Encode(data) + fmt.Printf("%s\n",encoded) +} + +func Base58Decode(input string){ + data := base58.Decode(input) + fmt.Printf("%x\n", data) +} + diff --git a/core/handle_base64.go b/core/handle_base64.go new file mode 100644 index 0000000..74dffa5 --- /dev/null +++ b/core/handle_base64.go @@ -0,0 +1,27 @@ +// Copyright 2017-2018 The qitmeer developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. +package core + +import ( + "encoding/base64" + "encoding/hex" + "fmt" +) + +func Base64Encode(input string){ + data, err := hex.DecodeString(input) + if err!=nil { + ErrExit(err) + } + encoded := base64.StdEncoding.EncodeToString(data) + fmt.Printf("%s\n",encoded) +} + +func Base64Decode(input string){ + data, err := base64.StdEncoding.DecodeString(input) + if err != nil { + ErrExit(err) + } + fmt.Printf("%x\n", data) +} diff --git a/core/handle_crypto.go b/core/handle_crypto.go new file mode 100644 index 0000000..69494b3 --- /dev/null +++ b/core/handle_crypto.go @@ -0,0 +1,257 @@ +// Copyright 2017-2018 The qitmeer developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. +package core + +import ( + "encoding/hex" + "fmt" + "github.com/HalalChain/qitmeer-lib/common/encode/base58" + "github.com/HalalChain/qitmeer-lib/common/hash" + "github.com/HalalChain/qitmeer-lib/crypto/bip32" + "github.com/HalalChain/qitmeer-lib/crypto/bip39" + "github.com/HalalChain/qitmeer-lib/crypto/ecc" + "github.com/HalalChain/qitmeer-lib/crypto/seed" + "github.com/HalalChain/qitmeer-lib/qx" + "github.com/HalalChain/qitmeer-lib/wallet" + "strconv" +) + +func NewEntropy(size uint) { + s, err := seed.GenerateSeed(uint16(size)) + if err != nil { + ErrExit(err) + } + fmt.Printf("%x\n", s) +} + +func HdNewMasterPrivateKey(version bip32.Bip32Version, entropyStr string) { + entropy, err := hex.DecodeString(entropyStr) + if err != nil { + ErrExit(err) + } + masterKey, err := bip32.NewMasterKey2(entropy, version) + if err != nil { + ErrExit(err) + } + fmt.Printf("%s\n", masterKey) +} + +func HdPrivateKeyToHdPublicKey(version bip32.Bip32Version, privateKeyStr string) { + data := base58.Decode(privateKeyStr) + masterKey, err := bip32.Deserialize2(data, version) + if err != nil { + ErrExit(err) + } + if !masterKey.IsPrivate { + ErrExit(fmt.Errorf("%s is not a HD (BIP32) private key", privateKeyStr)) + } + pubKey := masterKey.PublicKey() + fmt.Printf("%s\n", pubKey) +} + +func HdKeyToEcKey(version bip32.Bip32Version, keyStr string) { + data := base58.Decode(keyStr) + key, err := bip32.Deserialize2(data, version) + if err != nil { + ErrExit(err) + } + if key.IsPrivate { + fmt.Printf("%x\n", key.Key[:]) + } else { + fmt.Printf("%x\n", key.PublicKey().Key[:]) + } +} + +const bip32_ByteSize = 78 + 4 + +// The Serialization format of BIP32 Key +// https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki#serialization-format +// 4 bytes: version bytes +// mainnet: 0x0488B21E public, 0x0488ADE4 private; testnet: 0x043587CF public, 0x04358394 private +// 1 byte : depth: 0x00 for master nodes, 0x01 for level-1 derived keys, .... +// 4 bytes: the fingerprint of the parent's key (0x00000000 if master key) +// 4 bytes: child number. This is ser32(i) for i in xi = xpar/i, with xi the key being serialized. (0x00000000 if master key) +// index ≥ 0x80000000 to hardened keys +// 32 bytes: the chain code +// 33 bytes: the public key or private key data (serP(K) for public keys, 0x00 || ser256(k) for private keys) +// 4 bytes: checksum +func HdDecode(keyStr string) { + data := base58.Decode(keyStr) + if len(data) != bip32_ByteSize { + ErrExit(fmt.Errorf("invalid bip32 key size (%d), the size hould be %d", len(data), bip32_ByteSize)) + } + fmt.Printf(" version : %x (%s)\n", data[:4], GetBip32NetworkInfo(data[:4])) + fmt.Printf(" depth : %x\n", data[4:4+1]) + fmt.Printf(" parent fp : %x\n", data[5:5+4]) + childNumber, err := strconv.ParseInt(fmt.Sprintf("%x", data[9:9+4]), 16, 64) + if err != nil { + ErrExit(err) + } + hardened := childNumber >= 0x80000000 + fmt.Printf(" hardened : %v\n", hardened) + if hardened { + childNumber -= 0x80000000 + } + fmt.Printf(" child num : %d (%x)\n", childNumber, data[9:9+4]) + fmt.Printf("chain code : %x\n", data[13:13+32]) + if keyStr[1:4] == "pub" { + // the pub key should be 33 bytes, + // the first byte 0x02 means y is even, + // the first byte 0x03 means y is odd + var oldOrEven string + switch data[45] { + case 0x02: + oldOrEven = "even" + case 0x03: + oldOrEven = "odd" + default: + ErrExit(fmt.Errorf("invaid pub key [%x][%x]", data[45:46], data[46:46+32])) + } + fmt.Printf(" pub key : [%x][%x] y=%s\n", data[45:46], data[46:46+32], oldOrEven) + } else { + //the prv key should be 32 bytes, the first byte always 0x00 + fmt.Printf(" prv key : [%x][%x]\n", data[45:46], data[46:46+32]) + } + fmt.Printf(" checksum : %x\n", data[78:78+4]) + fmt.Printf(" hex : %x\n", data[:78+4]) + fmt.Printf(" base58 : %s\n", keyStr) + +} + +func HdDerive(hard bool, index uint32, path wallet.DerivationPath, version bip32.Bip32Version, key string) { + data := base58.Decode(key) + if len(data) != bip32_ByteSize { + ErrExit(fmt.Errorf("invalid bip32 key size (%d), the size hould be %d", len(data), bip32_ByteSize)) + } + mKey, err := bip32.Deserialize2(data, version) + if err != nil { + ErrExit(err) + } + var childKey *bip32.Key + if path.String() != "m" { + var ck = mKey + for _, i := range path { + ck, err = ck.NewChildKey(i) + if err != nil { + ErrExit(err) + } + } + childKey = ck + } else { + if hard { + childKey, err = mKey.NewChildKey(bip32.FirstHardenedChild + index) + } else { + childKey, err = mKey.NewChildKey(index) + } + if err != nil { + ErrExit(err) + } + } + fmt.Printf("%s\n", childKey) +} + +func MnemonicNew(entropyStr string) { + entropy, err := hex.DecodeString(entropyStr) + if err != nil { + ErrExit(err) + } + mnemonic, err := bip39.NewMnemonic(entropy) + if err != nil { + ErrExit(err) + } + fmt.Printf("%s\n", mnemonic) +} + +func MnemonicToEntropy(mnemonicStr string) { + entropy, err := bip39.EntropyFromMnemonic(mnemonicStr) + if err != nil { + ErrExit(err) + } + fmt.Printf("%x\n", entropy) +} + +func MnemonicToSeed(passphrase string, mnemonicStr string) { + seed, err := bip39.NewSeedWithErrorChecking(mnemonicStr, passphrase) + if err != nil { + ErrExit(err) + } + fmt.Printf("%x\n", seed) +} + +func EcNew(curve string, entropyStr string) { + pk, err := qx.EcNew(curve, entropyStr) + if err != nil { + ErrExit(err) + } + fmt.Printf("%s\n", pk) +} + +func EcPrivateKeyToEcPublicKey(uncompressed bool, privateKeyStr string) { + key, err := qx.EcPrivateKeyToEcPublicKey(uncompressed, privateKeyStr) + if err != nil { + ErrExit(err) + } + fmt.Printf("%s\n", key) +} + +func EcPrivateKeyToWif(uncompressed bool, privateKeyStr string) { + data, err := hex.DecodeString(privateKeyStr) + if err != nil { + ErrExit(err) + } + privkey, _ := ecc.Secp256k1.PrivKeyFromBytes(data) + var key []byte + if uncompressed { + key = privkey.Serialize() + } else { + key = privkey.Serialize() + key = append(key, []byte{0x01}...) + } + cksumfunc := base58.DoubleHashChecksumFunc(hash.GetHasher(hash.SHA256), 4) + encoded := base58.CheckEncode(key, []byte{0x80}, 4, cksumfunc) + fmt.Printf("%s\n", encoded) +} + +func WifToEcPrivateKey(wif string) { + decoded, _, err := DecodeWIF(wif) + if err != nil { + ErrExit(err) + } + fmt.Printf("%x\n", decoded) +} + +func DecodeWIF(wif string) ([]byte, bool, error) { + cksumfunc := base58.DoubleHashChecksumFunc(hash.GetHasher(hash.SHA256), 4) + decoded, version, err := base58.CheckDecode(wif, 1, 4, cksumfunc) + compressed := false + if err != nil { + return nil, compressed, err + } + if len(version) != 1 && version[0] != 0x80 { + return nil, compressed, fmt.Errorf("incorrect wif version %x, should be 0x80", version) + } + if len(decoded) == 32 { + return decoded[:], compressed, nil + } else if len(decoded) == 33 && decoded[32] == 0x01 { + compressed = true + return decoded[:32], compressed, nil + } else { + return nil, compressed, fmt.Errorf("incorrect wif length") + } +} + +func WifToEcPubkey(uncompressed bool, wif string) { + decoded, _, err := DecodeWIF(wif) + if err != nil { + ErrExit(err) + } + _, pubKey := ecc.Secp256k1.PrivKeyFromBytes(decoded) + var key []byte + if uncompressed { + key = pubKey.SerializeUncompressed() + } else { + key = pubKey.SerializeCompressed() + } + fmt.Printf("%x\n", key[:]) +} diff --git a/core/handle_hash.go b/core/handle_hash.go new file mode 100644 index 0000000..526f2a3 --- /dev/null +++ b/core/handle_hash.go @@ -0,0 +1,89 @@ +// Copyright 2017-2018 The qitmeer developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. +package core + +import ( + "crypto" + "encoding/hex" + "fmt" + "github.com/HalalChain/qitmeer-lib/common/hash" + "github.com/HalalChain/qitmeer-lib/common/hash/btc" + "github.com/HalalChain/qitmeer-lib/common/hash/dcr" +) + +func Sha256(input string){ + data, err :=hex.DecodeString(input) + if err != nil { + ErrExit(err) + } + fmt.Printf("%x\n",btc.HashB(data)) +} + +func Blake256(input string){ + data, err :=hex.DecodeString(input) + if err != nil { + ErrExit(err) + } + fmt.Printf("%x\n",dcr.HashB(data)) +} + +func Blake2b256(input string){ + data, err :=hex.DecodeString(input) + if err != nil { + ErrExit(err) + } + fmt.Printf("%x\n",hash.HashB(data)) +} + +func Blake2b512(input string){ + data, err :=hex.DecodeString(input) + if err != nil { + ErrExit(err) + } + fmt.Printf("%x\n",hash.Hash512B(data)) +} + +func Sha3_256(input string){ + data, err :=hex.DecodeString(input) + if err != nil { + ErrExit(err) + } + fmt.Printf("%x\n",hash.CalcHash(data,hash.GetHasher(hash.SHA3_256))) +} + +func Keccak256(input string){ + data, err :=hex.DecodeString(input) + if err != nil { + ErrExit(err) + } + fmt.Printf("%x\n",hash.CalcHash(data,hash.GetHasher(hash.Keccak_256))) +} + +func Ripemd160(input string){ + data, err :=hex.DecodeString(input) + if err != nil { + ErrExit(err) + } + hasher := crypto.RIPEMD160.New() + hasher.Write(data) + hash := hasher.Sum(nil) + fmt.Printf("%x\n",hash[:]) +} + +func Bitcoin160(input string){ + data, err :=hex.DecodeString(input) + if err != nil { + ErrExit(err) + } + fmt.Printf("%x\n",btc.Hash160(data)) +} + +func Hash160(input string){ + data, err :=hex.DecodeString(input) + if err != nil { + ErrExit(err) + } + fmt.Printf("%x\n",hash.Hash160(data)) +} + diff --git a/core/handle_message.go b/core/handle_message.go new file mode 100644 index 0000000..5847b60 --- /dev/null +++ b/core/handle_message.go @@ -0,0 +1,143 @@ +package core + +import ( + "bytes" + "encoding/base64" + "fmt" + "github.com/HalalChain/qitmeer-lib/common/encode/base58" + "github.com/HalalChain/qitmeer-lib/common/hash" + "github.com/HalalChain/qitmeer-lib/common/hash/btc" + "github.com/HalalChain/qitmeer-lib/core/serialization" + "github.com/HalalChain/qitmeer-lib/crypto/ecc" + "github.com/HalalChain/qitmeer-lib/crypto/ecc/secp256k1" + "reflect" +) + +const ( + BTCMsgSignaturePrefixMagic = "Bitcoin Signed Message:\n" + QitmeerMsgSignaturePrefixMagic = "Qitmeer Signed Message:\n" +) + +func DecodeSignature(signatureType string, signStr string) { + signHex,err := base64.StdEncoding.DecodeString(signStr) + if err!=nil { + ErrExit(err) + } + signature,err := ecc.Secp256k1.ParseSignature(signHex) + if err!=nil { + ErrExit(err) + } + fmt.Printf("R=%x,S=%x\n",signature.GetR(),signature.GetS()) +} + +func DecodeAddr (mode string, addrStr string) ([]byte, error) { + switch(mode) { + case "btc" : + addrHash160, _, err := base58.BtcCheckDecode(addrStr) + return addrHash160,err + default : + addrHash160,_,err := base58.QitmeerCheckDecode(addrStr) + return addrHash160,err + } +} +func VerifyMsgSignature(mode string, addrStr string, signStr string, msgStr string){ + + msgHash := BuildMsgHash(mode,msgStr) + + addrHash160, err := DecodeAddr(mode,addrStr) + if err!=nil { + ErrExit(err) + } + + sign_c, err :=base64.StdEncoding.DecodeString(signStr) + if err!=nil { + ErrExit(err) + } + + // the recovery mode of btc + // That number between 0 and 3 we call the recovery id, or recid. + // Therefore, we return an extra byte, which also functions as a header byte, + // by using 27+recid (for uncompressed recovered pubkeys) or 31+recid (for compressed recovered pubkeys). + // https://bitcoin.stackexchange.com/a/38909 + // 27 + recId (uncompressed pubkey) + // 31 + recId (compressed pubkey) + pubKey, compressed,err := ecc.Secp256k1.RecoverCompact(sign_c,msgHash) + if err!=nil { + ErrExit(err) + } + + var data []byte + if compressed { + data = pubKey.SerializeCompressed() + }else { + data = pubKey.SerializeUncompressed() + } + + fmt.Printf("%v\n",reflect.DeepEqual(CalcHash160(mode,data),addrHash160)) +} + +func CalcHash160(mode string,data []byte) []byte { + switch(mode) { + case "btc": + return btc.Hash160(data) + default: + return hash.Hash160(data) + } +} + + +func BuildMsgHash(mode string, msg string) []byte { + var msgHash []byte + switch (mode) { + case "btc" : + var buf bytes.Buffer + serialization.WriteVarString(&buf, 0, BTCMsgSignaturePrefixMagic) + serialization.WriteVarString(&buf, 0, msg) + msgHash = btc.DoubleHashB(buf.Bytes()) + + default : + var buf bytes.Buffer + serialization.WriteVarString(&buf, 0, QitmeerMsgSignaturePrefixMagic) + serialization.WriteVarString(&buf, 0, msg) + msgHash = hash.HashB(buf.Bytes()) + } + return msgHash +} + +func MsgSign(mode string, showSignDetail bool, wif string, msg string,showDetails bool){ + decoded,compressed,err := DecodeWIF(wif) + if err!= nil { + ErrExit(err) + } + privateKey,_ := ecc.Secp256k1.PrivKeyFromBytes(decoded) + + msgHash := BuildMsgHash(mode,msg) + // + r,s, err :=ecc.Secp256k1.Sign(privateKey,msgHash) + if err!=nil { + ErrExit(err) + } + // got the der signature + sigHex := ecc.Secp256k1.NewSignature(r,s).Serialize() + + sign_c, err := secp256k1.SignCompact(secp256k1.NewPrivateKey(privateKey.GetD()),msgHash,compressed) + if err!=nil { + ErrExit(err) + } + + if showDetails { + fmt.Printf(" mode: %s\n",mode) + fmt.Printf(" hash: %x\n",msgHash) + fmt.Printf(" signature: %x\n",sigHex) + fmt.Printf(" (base64): %s\n",base64.StdEncoding.EncodeToString(sigHex)) + fmt.Printf(" R: %x\n",r) + fmt.Printf(" S: %x\n",s) + fmt.Printf(" compactsign: %x\n",sign_c[:]) + fmt.Printf(" (base64): %s\n",base64.StdEncoding.EncodeToString(sign_c[:])) + fmt.Printf(" compressed: %v\n",compressed) + + }else{ + fmt.Printf("%s\n",base64.StdEncoding.EncodeToString(sign_c[:])) + } + +} diff --git a/core/handle_rlp.go b/core/handle_rlp.go new file mode 100644 index 0000000..054e4d5 --- /dev/null +++ b/core/handle_rlp.go @@ -0,0 +1,100 @@ +// Copyright 2017-2018 The qitmeer developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. +package core + +import ( + "bytes" + "encoding/hex" + "encoding/json" + "fmt" + "github.com/HalalChain/qitmeer-lib/common/encode/rlp" + "io" + "strings" +) + +func RlpEncode(input string) { + var f interface{} + err := json.Unmarshal([]byte(input), &f) + if err != nil { + ErrExit(err) + } + b, err := rlp.EncodeToBytes(f) + if err != nil { + ErrExit(err) + } + fmt.Printf("%x\n",b) +} + +func RlpDecode(input string) { + data, err := hex.DecodeString(input) + if err!=nil { + ErrExit(err) + } + r := bytes.NewReader(data) + s := rlp.NewStream(r, 0) + var buffer bytes.Buffer + for { + if _, err := Dump(s,0,&buffer); err != nil { + if err != io.EOF { + ErrExit(err) + } + break + } + fmt.Println(buffer.String()) + } +} + +func Dump(s *rlp.Stream, depth int, buffer *bytes.Buffer) (*bytes.Buffer, error) { + kind, size, err := s.Kind() + if err != nil { + return buffer,err + } + switch kind { + case rlp.Byte, rlp.String: + str, err := s.Bytes() + if err != nil { + return buffer,err + } + if len(str) == 0 || IsASCII(str) { + + buffer.WriteString(fmt.Sprintf("%s%q", Ws(depth), str)) + } else { + buffer.WriteString(fmt.Sprintf("%s%x", Ws(depth), str)) + } + case rlp.List: + s.List() + defer s.ListEnd() + if size == 0 { + buffer.WriteString(fmt.Sprint(Ws(depth) + "[]")) + } else { + buffer.WriteString(fmt.Sprintln(Ws(depth) + "[")) + for i := 0; ; i++ { + if buff, err := Dump(s, depth+1,buffer); err == rlp.EOL { + buff.Truncate(buff.Len() - 2) //remove the last comma + buff.WriteString("\n") + break + } else if err != nil { + return buff,err + } else { + buff.WriteString(",\n") + } + } + buffer.WriteString(fmt.Sprint(Ws(depth) + "]")) + } + } + return buffer,nil +} + +func IsASCII(b []byte) bool { + for _, c := range b { + if c < 32 || c > 126 { + return false + } + } + return true +} + +func Ws(n int) string { + return strings.Repeat(" ", n) +} diff --git a/core/handle_tx.go b/core/handle_tx.go new file mode 100644 index 0000000..01abd35 --- /dev/null +++ b/core/handle_tx.go @@ -0,0 +1,130 @@ +// Copyright 2017-2018 The qitmeer developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. +package core + +import ( + "bytes" + "encoding/hex" + "fmt" + "github.com/HalalChain/qitmeer-lib/common/hash" + "github.com/HalalChain/qitmeer-lib/common/marshal" + "github.com/HalalChain/qitmeer-lib/core/address" + "github.com/HalalChain/qitmeer-lib/core/json" + "github.com/HalalChain/qitmeer-lib/core/types" + "github.com/HalalChain/qitmeer-lib/engine/txscript" + "github.com/HalalChain/qitmeer-lib/params" + "github.com/HalalChain/qitmeer-lib/qx" + "github.com/pkg/errors" +) + +func TxDecode(network string, rawTxStr string) { + var param *params.Params + switch network { + case "mainnet": + param = ¶ms.MainNetParams + case "testnet": + param = ¶ms.TestNetParams + case "privnet": + param = ¶ms.PrivNetParams + } + if len(rawTxStr)%2 != 0 { + ErrExit(fmt.Errorf("invaild raw transaction : %s", rawTxStr)) + } + serializedTx, err := hex.DecodeString(rawTxStr) + if err != nil { + ErrExit(err) + } + var tx types.Transaction + err = tx.Deserialize(bytes.NewReader(serializedTx)) + if err != nil { + ErrExit(err) + } + + jsonTx := &json.OrderedResult{ + {Key: "txid", Val: tx.TxHash().String()}, + {Key: "txhash", Val: tx.TxHashFull().String()}, + {Key: "version", Val: int32(tx.Version)}, + {Key: "locktime", Val: tx.LockTime}, + {Key: "expire", Val: tx.Expire}, + {Key: "vin", Val: marshal.MarshJsonVin(&tx)}, + {Key: "vout", Val: marshal.MarshJsonVout(&tx, nil, param)}, + } + marshaledTx, err := jsonTx.MarshalJSON() + if err != nil { + ErrExit(err) + } + + fmt.Printf("%s", marshaledTx) +} + +func TxEncode(version TxVersionFlag, lockTime TxLockTimeFlag, txIn TxInputsFlag, txOut TxOutputsFlag) { + + mtx := types.NewTransaction() + + mtx.Version = uint32(version) + + if lockTime != 0 { + mtx.LockTime = uint32(lockTime) + } + + for _, input := range txIn.inputs { + txHash, err := hash.NewHashFromStr(hex.EncodeToString(input.txhash)) + if err != nil { + ErrExit(err) + } + prevOut := types.NewOutPoint(txHash, input.index) + txIn := types.NewTxInput(prevOut, types.NullValueIn, []byte{}) + txIn.Sequence = input.sequence + if lockTime != 0 { + txIn.Sequence = types.MaxTxInSequenceNum - 1 + } + mtx.AddTxIn(txIn) + } + + for _, output := range txOut.outputs { + + // Decode the provided address. + addr, err := address.DecodeAddress(output.target) + if err != nil { + ErrExit(errors.Wrapf(err, "fail to decode address %s", output.target)) + } + + // Ensure the address is one of the supported types and that + // the network encoded with the address matches the network the + // server is currently on. + switch addr.(type) { + case *address.PubKeyHashAddress: + case *address.ScriptHashAddress: + default: + ErrExit(errors.Wrapf(err, "invalid type: %T", addr)) + } + // Create a new script which pays to the provided address. + pkScript, err := txscript.PayToAddrScript(addr) + if err != nil { + ErrExit(errors.Wrapf(err, "fail to create pk script for addr %s", addr)) + } + + atomic, err := types.NewAmount(output.amount) + if err != nil { + ErrExit(errors.Wrapf(err, "fail to create the currency amount from a "+ + "floating point value %f", output.amount)) + } + //TODO fix type conversion + txOut := types.NewTxOutput(uint64(atomic), pkScript) + mtx.AddTxOut(txOut) + } + mtxHex, err := mtx.Serialize(types.TxSerializeNoWitness) + if err != nil { + ErrExit(err) + } + fmt.Printf("%x\n", mtxHex) +} + +func TxSign(privkeyStr string, rawTxStr string,network string) { + mtxHex, err := qx.TxSign(privkeyStr, rawTxStr, network) + if err != nil { + ErrExit(err) + } + fmt.Printf("%s\n", mtxHex) +} diff --git a/core/qitmeer58checkflag.go b/core/qitmeer58checkflag.go new file mode 100644 index 0000000..dc86233 --- /dev/null +++ b/core/qitmeer58checkflag.go @@ -0,0 +1,37 @@ +// Copyright 2017-2018 The qitmeer developers +// Use of this source code is governed by an ISC +// license that can be found in the LICENSE file. +package core + +import ( + "encoding/hex" + "github.com/HalalChain/qitmeer-lib/params" +) + +type QitmeerBase58checkVersionFlag struct { + Ver []byte + flag string +} +func (n *QitmeerBase58checkVersionFlag) Set(s string) error { + n.Ver = []byte{} + switch (s) { + case "mainnet": + n.Ver = append(n.Ver, params.MainNetParams.PubKeyHashAddrID[0:]...) + case "privnet": + n.Ver = append(n.Ver, params.PrivNetParams.PubKeyHashAddrID[0:]...) + case "testnet": + n.Ver = append(n.Ver, params.TestNetParams.PubKeyHashAddrID[0:]...) + default: + v, err := hex.DecodeString(s) + if err!=nil { + return err + } + n.Ver = append(n.Ver,v...) + } + n.flag = s + return nil +} + +func (n *QitmeerBase58checkVersionFlag) String() string{ + return n.flag +} diff --git a/core/txflag.go b/core/txflag.go new file mode 100644 index 0000000..9628070 --- /dev/null +++ b/core/txflag.go @@ -0,0 +1,130 @@ +package core + +import ( + "bytes" + "encoding/hex" + "fmt" + "math" + "strconv" + "strings" +) +type TxVersionFlag uint32 +type TxLockTimeFlag uint32 + +func (ver TxVersionFlag) String() string { + return fmt.Sprintf("%d",ver) +} +func (ver *TxVersionFlag) Set(s string) error { + v, err :=strconv.ParseUint(s, 10, 32) + if err !=nil { + return err + } + *ver = TxVersionFlag(uint32(v)) + return nil +} + +func (lt TxLockTimeFlag) String() string { + return fmt.Sprintf("%d",lt) +} +func (lt *TxLockTimeFlag) Set(s string) error { + v, err :=strconv.ParseUint(s, 10, 32) + if err !=nil { + return err + } + *lt = TxLockTimeFlag(uint32(v)) + return nil +} + +type TxInputsFlag struct{ + inputs []txInput +} +type TxOutputsFlag struct{ + outputs []txOutput +} + +type txInput struct { + txhash []byte + index uint32 + sequence uint32 +} +type txOutput struct { + target string + amount float64 +} + +func (i txInput) String() string { + return fmt.Sprintf("%x:%d:%d",i.txhash[:],i.index,i.sequence) +} +func (o txOutput) String() string { + return fmt.Sprintf("%s:%f",o.target,o.amount) +} + +func (v TxInputsFlag) String() string { + var buffer bytes.Buffer + buffer.WriteString("{") + for _,input := range v.inputs{ + buffer.WriteString(input.String()) + } + buffer.WriteString("}") + return buffer.String() +} + +func(of TxOutputsFlag) String() string { + var buffer bytes.Buffer + buffer.WriteString("{") + for _,o := range of.outputs{ + buffer.WriteString(o.String()) + } + buffer.WriteString("}") + return buffer.String() +} + +func (v *TxInputsFlag) Set(s string) error { + input := strings.Split(s,":") + if len(input) < 2 { + return fmt.Errorf("error to parse tx input : %s",s) + } + data, err :=hex.DecodeString(input[0]) + if err!=nil{ + return err + } + if len(data) != 32 { + return fmt.Errorf("tx hash should be 32 bytes") + } + + index, err := strconv.ParseUint(input[1], 10, 64) + if err != nil { + return err + } + var seq = uint32(math.MaxUint32) + if len(input) == 3 { + s, err := strconv.ParseUint(input[2], 10, 32) + if err!= nil { + return err + } + seq = uint32(s) + } + i := txInput{ + data, + uint32(index), + uint32(seq), + } + v.inputs = append(v.inputs,i) + return nil +} + +func (of *TxOutputsFlag) Set(s string) error { + output := strings.Split(s,":") + if len(output) < 2 { + return fmt.Errorf("error to parse tx output : %s",s) + } + target:=output[0] + amount, err := strconv.ParseFloat(output[1],64) + if err != nil { + return err + } + of.outputs = append(of.outputs,txOutput{ + target,amount }) + return nil +} + diff --git a/qx.go b/qx.go index 04f43d9..03f6d48 100644 --- a/qx.go +++ b/qx.go @@ -8,7 +8,7 @@ import ( "fmt" "github.com/HalalChain/qitmeer-lib/crypto/seed" "github.com/HalalChain/qitmeer-lib/wallet" - "github.com/HalalChain/qx/qx" + "github.com/HalalChain/qx/core" "io/ioutil" "os" "strings" @@ -87,25 +87,25 @@ func errExit(err error){ os.Exit(1) } -var base58checkVersion qx.QitmeerBase58checkVersionFlag +var base58checkVersion core.QitmeerBase58checkVersionFlag var base58checkVersionSize int var base58checkMode string var showDetails bool var base58checkHasher string var base58checkCksumSize int var seedSize uint -var hdVer qx.Bip32VersionFlag +var hdVer core.Bip32VersionFlag var hdHarden bool var hdIndex uint -var derivePath qx.DerivePathFlag +var derivePath core.DerivePathFlag var mnemoicSeedPassphrase string var curve string var uncompressedPKFormat bool var network string -var txInputs qx.TxInputsFlag -var txOutputs qx.TxOutputsFlag -var txVersion qx.TxVersionFlag -var txLockTime qx.TxLockTimeFlag +var txInputs core.TxInputsFlag +var txOutputs core.TxOutputsFlag +var txVersion core.TxVersionFlag +var txLockTime core.TxLockTimeFlag var privateKey string var msgSignatureMode string @@ -116,7 +116,7 @@ func main() { // ---------------------------- base58CheckEncodeCommand := flag.NewFlagSet("base58check-encode", flag.ExitOnError) - base58checkVersion = qx.QitmeerBase58checkVersionFlag{} + base58checkVersion = core.QitmeerBase58checkVersionFlag{} base58checkVersion.Set("testnet") base58CheckEncodeCommand.Var(&base58checkVersion, "v","base58check `version` [mainnet|testnet|privnet") base58CheckEncodeCommand.StringVar(&base58checkMode, "m", "qitmeer", "base58check encode mode : [qitmeer|btc]") @@ -255,7 +255,7 @@ func main() { } hdDeriveCmd.UintVar(&hdIndex,"i",0,"The HD `index`") hdDeriveCmd.BoolVar(&hdHarden,"d",false,"create a hardened key") - derivePath = qx.DerivePathFlag{Path:wallet.DerivationPath{}} + derivePath = core.DerivePathFlag{Path: wallet.DerivationPath{}} hdDeriveCmd.Var(&derivePath,"p","hd derive `path`. ex: m/44'/0'/0'/0") hdDeriveCmd.Var(&hdVer, "v","The HD(BIP32) `version` [mainnet|testnet|privnet|bip32]") @@ -329,7 +329,7 @@ func main() { txEncodeCmd.Usage = func() { cmdUsage(txEncodeCmd, "Usage: qx tx-encode [-i tx-input] [-l tx-lock-time] [-o tx-output] [-v tx-version] \n") } - txVersion = qx.TxVersionFlag(TX_VERION) //set default tx version + txVersion = core.TxVersionFlag(TX_VERION) //set default tx version txEncodeCmd.Var(&txVersion,"v","the transaction version") txEncodeCmd.Var(&txLockTime,"l","the transaction lock time") txEncodeCmd.Var(&txInputs,"i",`The set of transaction input points encoded as TXHASH:INDEX:SEQUENCE. @@ -433,7 +433,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { base58CheckEncodeCommand.Usage() }else{ - qx.Base58CheckEncode(base58checkVersion.Ver,base58checkMode,base58checkHasher,base58checkCksumSize,os.Args[len(os.Args)-1]) + core.Base58CheckEncode(base58checkVersion.Ver,base58checkMode,base58checkHasher,base58checkCksumSize,os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -441,7 +441,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.Base58CheckEncode(base58checkVersion.Ver,base58checkMode,base58checkHasher,base58checkCksumSize,str) + core.Base58CheckEncode(base58checkVersion.Ver,base58checkMode,base58checkHasher,base58checkCksumSize,str) } } @@ -452,7 +452,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { base58CheckDecodeCommand.Usage() }else{ - qx.Base58CheckDecode(base58checkMode,base58checkHasher,base58checkVersionSize,base58checkCksumSize,os.Args[len(os.Args)-1],showDetails) + core.Base58CheckDecode(base58checkMode,base58checkHasher,base58checkVersionSize,base58checkCksumSize,os.Args[len(os.Args)-1],showDetails) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -460,7 +460,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.Base58CheckDecode(base58checkMode,base58checkHasher,base58checkVersionSize,base58checkCksumSize,str,showDetails) + core.Base58CheckDecode(base58checkMode,base58checkHasher,base58checkVersionSize,base58checkCksumSize,str,showDetails) } } @@ -471,7 +471,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { base58EncodeCmd.Usage() }else{ - qx.Base58Encode(os.Args[len(os.Args)-1]) + core.Base58Encode(os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -479,7 +479,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.Base58Encode(str) + core.Base58Encode(str) } } // Handle base58-decode @@ -489,7 +489,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { base58DecodeCmd.Usage() }else{ - qx.Base58Decode(os.Args[len(os.Args)-1]) + core.Base58Decode(os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -497,7 +497,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.Base58Decode(str) + core.Base58Decode(str) } } @@ -507,7 +507,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { base64EncodeCmd.Usage() }else{ - qx.Base64Encode(os.Args[len(os.Args)-1]) + core.Base64Encode(os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -515,7 +515,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.Base64Encode(str) + core.Base64Encode(str) } } @@ -525,7 +525,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { base64DecodeCmd.Usage() }else{ - qx.Base64Decode(os.Args[len(os.Args)-1]) + core.Base64Decode(os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -533,7 +533,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.Base64Decode(str) + core.Base64Decode(str) } } @@ -543,7 +543,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { rlpEncodeCmd.Usage() }else{ - qx.RlpEncode(os.Args[len(os.Args)-1]) + core.RlpEncode(os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -551,7 +551,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.RlpEncode(str) + core.RlpEncode(str) } } if rlpDecodeCmd.Parsed(){ @@ -560,7 +560,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { rlpDecodeCmd.Usage() }else{ - qx.RlpDecode(os.Args[len(os.Args)-1]) + core.RlpDecode(os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -568,7 +568,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.RlpDecode(str) + core.RlpDecode(str) } } @@ -578,7 +578,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { sha256cmd.Usage() }else{ - qx.Sha256(os.Args[len(os.Args)-1]) + core.Sha256(os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -586,7 +586,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.Sha256(str) + core.Sha256(str) } } @@ -596,7 +596,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { blake256cmd.Usage() }else{ - qx.Blake256(os.Args[len(os.Args)-1]) + core.Blake256(os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -604,7 +604,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.Blake256(str) + core.Blake256(str) } } @@ -614,7 +614,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { blake2b256cmd.Usage() }else{ - qx.Blake2b256(os.Args[len(os.Args)-1]) + core.Blake2b256(os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -622,7 +622,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.Blake2b256(str) + core.Blake2b256(str) } } @@ -632,7 +632,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { blake2b512cmd.Usage() }else{ - qx.Blake2b512(os.Args[len(os.Args)-1]) + core.Blake2b512(os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -640,7 +640,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.Blake2b512(str) + core.Blake2b512(str) } } @@ -650,7 +650,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { sha3_256cmd.Usage() }else{ - qx.Sha3_256(os.Args[len(os.Args)-1]) + core.Sha3_256(os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -658,7 +658,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.Sha3_256(str) + core.Sha3_256(str) } } @@ -668,7 +668,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { keccak256cmd.Usage() }else{ - qx.Keccak256(os.Args[len(os.Args)-1]) + core.Keccak256(os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -676,7 +676,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.Keccak256(str) + core.Keccak256(str) } } @@ -686,7 +686,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { ripemd160Cmd.Usage() }else{ - qx.Ripemd160(os.Args[len(os.Args)-1]) + core.Ripemd160(os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -694,7 +694,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.Ripemd160(str) + core.Ripemd160(str) } } @@ -704,7 +704,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { bitcion160Cmd.Usage() }else{ - qx.Bitcoin160(os.Args[len(os.Args)-1]) + core.Bitcoin160(os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -712,7 +712,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.Bitcoin160(str) + core.Bitcoin160(str) } } @@ -722,7 +722,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { hash160Cmd.Usage() }else{ - qx.Hash160(os.Args[len(os.Args)-1]) + core.Hash160(os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -730,7 +730,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.Hash160(str) + core.Hash160(str) } } @@ -743,7 +743,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if seedSize % 8 > 0 { errExit(fmt.Errorf("seed (entropy) length must be Must be divisible by 8")) } - qx.NewEntropy(seedSize/8) + core.NewEntropy(seedSize/8) } }else { entropyCmd.Usage() @@ -756,7 +756,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { hdNewCmd.Usage() }else{ - qx.HdNewMasterPrivateKey(hdVer.Version,os.Args[len(os.Args)-1]) + core.HdNewMasterPrivateKey(hdVer.Version,os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -764,7 +764,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.HdNewMasterPrivateKey(hdVer.Version,str) + core.HdNewMasterPrivateKey(hdVer.Version,str) } } @@ -774,7 +774,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { hdToPubCmd.Usage() }else{ - qx.HdPrivateKeyToHdPublicKey(hdVer.Version,os.Args[len(os.Args)-1]) + core.HdPrivateKeyToHdPublicKey(hdVer.Version,os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -782,7 +782,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.HdPrivateKeyToHdPublicKey(hdVer.Version,str) + core.HdPrivateKeyToHdPublicKey(hdVer.Version,str) } } @@ -792,7 +792,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { hdToEcCmd.Usage() }else{ - qx.HdKeyToEcKey(hdVer.Version,os.Args[len(os.Args)-1]) + core.HdKeyToEcKey(hdVer.Version,os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -800,7 +800,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.HdKeyToEcKey(hdVer.Version,str) + core.HdKeyToEcKey(hdVer.Version,str) } } @@ -810,7 +810,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { hdDecodeCmd.Usage() }else{ - qx.HdDecode(os.Args[len(os.Args)-1]) + core.HdDecode(os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -818,7 +818,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.HdDecode(str) + core.HdDecode(str) } } @@ -828,7 +828,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { hdDeriveCmd.Usage() }else{ - qx.HdDerive(hdHarden,uint32(hdIndex),derivePath.Path,hdVer.Version,os.Args[len(os.Args)-1]) + core.HdDerive(hdHarden,uint32(hdIndex),derivePath.Path,hdVer.Version,os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -836,7 +836,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.HdDerive(hdHarden,uint32(hdIndex),derivePath.Path,hdVer.Version,str) + core.HdDerive(hdHarden,uint32(hdIndex),derivePath.Path,hdVer.Version,str) } } @@ -846,7 +846,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { mnemonicNewCmd.Usage() }else{ - qx.MnemonicNew(os.Args[len(os.Args)-1]) + core.MnemonicNew(os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -854,7 +854,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.MnemonicNew(str) + core.MnemonicNew(str) } } @@ -864,7 +864,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { mnemonicToEntropyCmd.Usage() }else{ - qx.MnemonicToEntropy(os.Args[len(os.Args)-1]) + core.MnemonicToEntropy(os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -872,7 +872,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.MnemonicToEntropy(str) + core.MnemonicToEntropy(str) } } @@ -882,7 +882,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { mnemonicToSeedCmd.Usage() }else{ - qx.MnemonicToSeed(mnemoicSeedPassphrase, os.Args[len(os.Args)-1]) + core.MnemonicToSeed(mnemoicSeedPassphrase, os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -890,7 +890,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.MnemonicToSeed(mnemoicSeedPassphrase,str) + core.MnemonicToSeed(mnemoicSeedPassphrase,str) } } @@ -900,7 +900,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { ecNewCmd.Usage() }else{ - qx.EcNew(curve,os.Args[len(os.Args)-1]) + core.EcNew(curve,os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -908,7 +908,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.EcNew(curve, str) + core.EcNew(curve, str) } } @@ -918,7 +918,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { ecToPubCmd.Usage() }else{ - qx.EcPrivateKeyToEcPublicKey(uncompressedPKFormat,os.Args[len(os.Args)-1]) + core.EcPrivateKeyToEcPublicKey(uncompressedPKFormat,os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -926,7 +926,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.EcPrivateKeyToEcPublicKey(uncompressedPKFormat,str) + core.EcPrivateKeyToEcPublicKey(uncompressedPKFormat,str) } } @@ -936,7 +936,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { ecToWifCmd.Usage() }else{ - qx.EcPrivateKeyToWif(uncompressedPKFormat,os.Args[len(os.Args)-1]) + core.EcPrivateKeyToWif(uncompressedPKFormat,os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -944,7 +944,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.EcPrivateKeyToWif(uncompressedPKFormat,str) + core.EcPrivateKeyToWif(uncompressedPKFormat,str) } } @@ -954,7 +954,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { wifToEcCmd.Usage() }else{ - qx.WifToEcPrivateKey(os.Args[len(os.Args)-1]) + core.WifToEcPrivateKey(os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -962,7 +962,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.WifToEcPrivateKey(str) + core.WifToEcPrivateKey(str) } } @@ -972,7 +972,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { wifToPubCmd.Usage() }else{ - qx.WifToEcPubkey(uncompressedPKFormat,os.Args[len(os.Args)-1]) + core.WifToEcPubkey(uncompressedPKFormat,os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -980,7 +980,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.WifToEcPubkey(uncompressedPKFormat,str) + core.WifToEcPubkey(uncompressedPKFormat,str) } } @@ -990,7 +990,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { ecToAddrCmd.Usage() }else{ - qx.EcPubKeyToAddress(base58checkVersion.Ver,os.Args[len(os.Args)-1]) + core.EcPubKeyToAddress(base58checkVersion.Ver,os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -998,7 +998,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.EcPubKeyToAddress(base58checkVersion.Ver,str) + core.EcPubKeyToAddress(base58checkVersion.Ver,str) } } @@ -1008,7 +1008,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { txDecodeCmd.Usage() }else{ - qx.TxDecode(network,os.Args[len(os.Args)-1]) + core.TxDecode(network,os.Args[len(os.Args)-1]) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -1016,7 +1016,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.TxDecode(network, str) + core.TxDecode(network, str) } } @@ -1026,7 +1026,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { txEncodeCmd.Usage() }else{ - qx.TxEncode(txVersion,txLockTime,txInputs,txOutputs) + core.TxEncode(txVersion,txLockTime,txInputs,txOutputs) } } } @@ -1037,7 +1037,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { txSignCmd.Usage() }else{ - qx.TxSign(privateKey,os.Args[len(os.Args)-1],network) + core.TxSign(privateKey,os.Args[len(os.Args)-1],network) } }else { //try from STDIN src, err := ioutil.ReadAll(os.Stdin) @@ -1045,7 +1045,7 @@ MEER is the 64 bit spend amount in qitmeer.`) errExit(err) } str := strings.TrimSpace(string(src)) - qx.TxSign(privateKey, str,network) + core.TxSign(privateKey, str,network) } } @@ -1055,7 +1055,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { msgSignCmd.Usage() }else{ - qx.MsgSign(msgSignatureMode,showDetails,os.Args[len(os.Args)-2],os.Args[len(os.Args)-1],showDetails) + core.MsgSign(msgSignatureMode,showDetails,os.Args[len(os.Args)-2],os.Args[len(os.Args)-1],showDetails) } } } @@ -1066,7 +1066,7 @@ MEER is the 64 bit spend amount in qitmeer.`) if len(os.Args) == 2 || os.Args[2] == "help" || os.Args[2] == "--help" { msgVerifyCmd.Usage() }else{ - qx.VerifyMsgSignature(msgSignatureMode,os.Args[len(os.Args)-3],os.Args[len(os.Args)-2],os.Args[len(os.Args)-1]) + core.VerifyMsgSignature(msgSignatureMode,os.Args[len(os.Args)-3],os.Args[len(os.Args)-2],os.Args[len(os.Args)-1]) } } } From 8a378b7bd8a060b2f75cd06812005a3ee5353283 Mon Sep 17 00:00:00 2001 From: Jin Date: Mon, 12 Aug 2019 08:10:07 +0800 Subject: [PATCH 2/2] README.md --- README.md | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index b655501..cb3f394 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,11 @@ qx is a command-line tool that provides a variety of commands for key management and transaction construction, such as random "seed" generation, public/private key encoding etc. qx cab be built and distributed as a single file binary, which works like the swiss army knife of qitmeer -## How to build +## Installation +### Binary archives +* Binary archives are published at [releases](https://github.com/HalalChain/qx/releases "releases"). + +### How to build ```shell ~ git clone https://github.com/HalalChain/qx.git