1
1
package crypto
2
2
3
3
import (
4
+ "hash"
5
+
4
6
"github.com/btcsuite/btcd/btcec"
5
7
"github.com/btcsuite/btcd/chaincfg"
6
8
"github.com/btcsuite/btcutil"
9
+ "github.com/zoobc/zoobc-core/common/blocker"
10
+ "github.com/zoobc/zoobc-core/common/model"
7
11
"golang.org/x/crypto/sha3"
8
12
)
9
13
@@ -30,9 +34,14 @@ func DefaultBitcoinCurve() *btcec.KoblitzCurve {
30
34
}
31
35
32
36
// DefaultBitcoinPublicKeyFormat return recommended public key format
33
- func DefaultBitcoinPublicKeyFormat () btcutil. PubKeyFormat {
37
+ func DefaultBitcoinPublicKeyFormat () model. BitcoinPublicKeyFormat {
34
38
// https://bitcoin.org/en/glossary/compressed-public-key
35
- return btcutil .PKFCompressed
39
+ return model .BitcoinPublicKeyFormat_PublicKeyFormatCompressed
40
+ }
41
+
42
+ // DefaultBitcoinPrivateKeyLength to
43
+ func DefaultBitcoinPrivateKeyLength () model.PrivateKeyBytesLength {
44
+ return model .PrivateKeyBytesLength_PrivateKey256Bits
36
45
}
37
46
38
47
// NewBitcoinSignature is new instance of bitcoin signature
@@ -47,7 +56,7 @@ func NewBitcoinSignature(netParams *chaincfg.Params, curve *btcec.KoblitzCurve)
47
56
func (* BitcoinSignature ) Sign (privateKey * btcec.PrivateKey , payload []byte ) ([]byte , error ) {
48
57
var sig , err = privateKey .Sign (payload )
49
58
if err != nil {
50
- return nil , err
59
+ return nil , blocker . NewBlocker ( blocker . AuthErr , err . Error ())
51
60
}
52
61
return sig .Serialize (), nil
53
62
}
@@ -67,70 +76,101 @@ func (b *BitcoinSignature) GetNetworkParams() *chaincfg.Params {
67
76
}
68
77
69
78
// GetPrivateKeyFromSeed to get private key form seed
70
- func (b * BitcoinSignature ) GetPrivateKeyFromSeed (seed string ) * btcec.PrivateKey {
79
+ func (b * BitcoinSignature ) GetPrivateKeyFromSeed (
80
+ seed string ,
81
+ privkeyLength model.PrivateKeyBytesLength ,
82
+ ) (* btcec.PrivateKey , error ) {
71
83
var (
72
84
// Convert seed (secret phrase) to byte array
73
85
seedBuffer = []byte (seed )
74
- // Compute SHA3-256 hash of seed (secret phrase)
75
- seedHash = sha3 .Sum256 (seedBuffer )
76
- privateKey , _ = btcec .PrivKeyFromBytes (b .Curve , seedHash [:])
86
+ hasher hash.Hash
87
+ privateKey * btcec.PrivateKey
77
88
)
78
- return privateKey
89
+ switch privkeyLength {
90
+ case model .PrivateKeyBytesLength_PrivateKey256Bits :
91
+ hasher = sha3 .New256 ()
92
+ case model .PrivateKeyBytesLength_PrivateKey384Bits :
93
+ hasher = sha3 .New384 ()
94
+ case model .PrivateKeyBytesLength_PrivateKey512Bits :
95
+ hasher = sha3 .New512 ()
96
+ default :
97
+ return nil , blocker .NewBlocker (blocker .AppErr , "invalidPrivateKeyLength" )
98
+ }
99
+ hasher .Write (seedBuffer )
100
+ privateKey , _ = btcec .PrivKeyFromBytes (b .Curve , hasher .Sum (nil ))
101
+ return privateKey , nil
79
102
}
80
103
81
104
// GetPublicKeyFromSeed Get the raw public key corresponding to a seed (secret phrase)
105
+ func (b * BitcoinSignature ) GetPublicKeyFromSeed (
106
+ seed string ,
107
+ format model.BitcoinPublicKeyFormat ,
108
+ privkeyLength model.PrivateKeyBytesLength ,
109
+ ) ([]byte , error ) {
110
+ var privateKey , err = b .GetPrivateKeyFromSeed (seed , privkeyLength )
111
+ if err != nil {
112
+ return nil , err
113
+ }
114
+ publicKey , err := b .GetPublicKeyFromPrivateKey (privateKey , format )
115
+ if err != nil {
116
+ return nil , err
117
+ }
118
+ return publicKey , nil
119
+ }
120
+
121
+ // GetPublicKeyFromPrivateKey get raw public key from private key
82
122
// public key format : https://bitcoin.org/en/wallets-guide#public-key-formats
83
- func (b * BitcoinSignature ) GetPublicKeyFromSeed (seed string , format btcutil.PubKeyFormat ) []byte {
84
- var privateKey = b .GetPrivateKeyFromSeed (seed )
123
+ func (* BitcoinSignature ) GetPublicKeyFromPrivateKey (
124
+ privateKey * btcec.PrivateKey ,
125
+ format model.BitcoinPublicKeyFormat ,
126
+ ) ([]byte , error ) {
85
127
switch format {
86
- case btcutil .PKFUncompressed :
87
- return privateKey .PubKey ().SerializeUncompressed ()
88
- case btcutil .PKFCompressed :
89
- return privateKey .PubKey ().SerializeCompressed ()
90
- case btcutil .PKFHybrid :
91
- return privateKey .PubKey ().SerializeHybrid ()
128
+ case model .BitcoinPublicKeyFormat_PublicKeyFormatUncompressed :
129
+ return privateKey .PubKey ().SerializeUncompressed (), nil
130
+ case model .BitcoinPublicKeyFormat_PublicKeyFormatCompressed :
131
+ return privateKey .PubKey ().SerializeCompressed (), nil
92
132
default :
93
- return nil
133
+ return nil , blocker . NewBlocker ( blocker . AppErr , "invalidPublicKeyFormat" )
94
134
}
95
135
}
96
136
97
- // GetAddressPublicKey to get address public key from seed
98
- // NOTE: Currently the address is the hex-encoded from serialized public key (pay-to-pubkey)
99
- func (b * BitcoinSignature ) GetAddressPublicKey (publicKey []byte ) (string , error ) {
137
+ // GetPublicKeyFromBytes to get public key from raw bytes public key
138
+ func (b * BitcoinSignature ) GetPublicKeyFromBytes (pubkey []byte ) (* btcec.PublicKey , error ) {
139
+ return btcec .ParsePubKey (pubkey , b .Curve )
140
+ }
141
+
142
+ // GetPublicKeyString will return hex string from bytes public key
143
+ func (b * BitcoinSignature ) GetPublicKeyString (publicKey []byte ) (string , error ) {
100
144
var address , err = btcutil .NewAddressPubKey (publicKey , b .GetNetworkParams ())
101
145
if err != nil {
102
- return "" , err
146
+ return "" , blocker . NewBlocker ( blocker . ParserErr , err . Error ())
103
147
}
104
148
return address .String (), nil
105
149
}
106
150
107
- // GetBytesAddressPublicKey Get raw bytes of address
108
- func (b * BitcoinSignature ) GetBytesAddressPublicKey ( address string ) ( []byte , error ) {
109
- var decodedAddress , err = btcutil .DecodeAddress ( address , b .GetNetworkParams ())
151
+ // GetAddressFromPublicKey to get address public key from seed
152
+ func (b * BitcoinSignature ) GetAddressFromPublicKey ( publicKey []byte ) ( string , error ) {
153
+ var address , err = btcutil .NewAddressPubKey ( publicKey , b .GetNetworkParams ())
110
154
if err != nil {
111
- return nil , err
155
+ return "" , blocker . NewBlocker ( blocker . ParserErr , err . Error ())
112
156
}
113
- return decodedAddress . ScriptAddress (), nil
157
+ return address . EncodeAddress (), nil
114
158
}
115
159
116
- // GetPublicKeyFromAddress to get public key from address
117
- func (b * BitcoinSignature ) GetPublicKeyFromAddress (address string ) (* btcec.PublicKey , error ) {
118
- rowBytesAddress , err := b .GetBytesAddressPublicKey (address )
119
- if err != nil {
120
- return nil , err
121
- }
122
- publicKey , err := btcec .ParsePubKey (rowBytesAddress , b .Curve )
160
+ // GetAddressBytes Get raw bytes of address
161
+ func (b * BitcoinSignature ) GetAddressBytes (address string ) ([]byte , error ) {
162
+ var decodedAddress , err = btcutil .DecodeAddress (address , b .GetNetworkParams ())
123
163
if err != nil {
124
- return nil , err
164
+ return nil , blocker . NewBlocker ( blocker . ParserErr , err . Error ())
125
165
}
126
- return publicKey , nil
166
+ return decodedAddress . ScriptAddress () , nil
127
167
}
128
168
129
169
// GetSignatureFromBytes to get signature type from signature raw bytes
130
170
func (b * BitcoinSignature ) GetSignatureFromBytes (signatureBytes []byte ) (* btcec.Signature , error ) {
131
171
var signature , err = btcec .ParseSignature (signatureBytes , b .Curve )
132
172
if err != nil {
133
- return nil , err
173
+ return nil , blocker . NewBlocker ( blocker . ParserErr , err . Error ())
134
174
}
135
175
return signature , nil
136
176
}
0 commit comments