This repository has been archived by the owner on Oct 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from lochjin/master
Modify package name
- Loading branch information
Showing
15 changed files
with
1,283 additions
and
82 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
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,11 @@ | ||
package core | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func ErrExit(err error){ | ||
fmt.Fprintf(os.Stderr, "Qx Error : %q\n",err) | ||
os.Exit(1) | ||
} |
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,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() | ||
} | ||
|
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,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) | ||
} |
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,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) | ||
} | ||
|
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,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) | ||
} |
Oops, something went wrong.