/
cryptoClient.go
49 lines (42 loc) · 1.26 KB
/
cryptoClient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package crypto
import (
"crypto/ecdsa"
"github.com/DhunterAO/goAuthChain/crypto/types"
)
func CalcHash(data []byte) *types.Hash {
hash := types.Keccak256Hash(data)
return &hash
}
func recoverEcdsaFromSig(hash *types.Hash, sig *types.Signature) (*ecdsa.PublicKey, error) {
uncompressedPk, err := types.Ecrecover(hash.Bytes(), sig.Bytes())
if err != nil {
return nil, err
}
pk, err := types.UnmarshalPubkey(uncompressedPk)
if err != nil {
return nil, err
}
return pk, err
}
func RecoverPkFromSig(hash *types.Hash, sig *types.Signature) (*types.Pubkey, error) {
pk, err := recoverEcdsaFromSig(hash, sig)
if err != nil {
return &types.Pubkey{}, err
}
return types.BytesToPubkey(types.CompressPubkey(pk)), err
}
func PubkeyToAddress(pk *types.Pubkey) (*types.Address, error) {
ecdsaPk, err := types.DecompressPubkey(pk.Bytes())
if err != nil {
return nil, err
}
addr := types.EcdsaPubkeyToAddress(*ecdsaPk)
return addr, nil
}
func Sign(hash *types.Hash, prv *ecdsa.PrivateKey) (*types.Signature, error) {
sig, err := types.Sign(hash.Bytes(), prv)
return types.BytesToSignature(sig), err
}
func VerifySignature(pubkey *types.Pubkey, hash *types.Hash, sig *types.Signature) bool {
return types.VerifySignature(pubkey[:], hash[:], sig[:types.SignatureLength-1])
}