github.com/annchain/OG@v0.0.9/deprecated/utils.go (about) 1 package deprecated 2 3 import ( 4 "crypto/sha256" 5 "errors" 6 "fmt" 7 "github.com/annchain/OG/arefactor/common/hexutil" 8 "github.com/annchain/OG/arefactor/og_interface" 9 "github.com/annchain/OG/deprecated/ogcrypto" 10 "github.com/annchain/OG/deprecated/ogcrypto_interface" 11 "github.com/sirupsen/logrus" 12 "golang.org/x/crypto/ripemd160" 13 "math/big" 14 ) 15 16 func PrivateKeyFromBytes(typev ogcrypto_interface.CryptoType, bytes []byte) ogcrypto_interface.PrivateKey { 17 return ogcrypto_interface.PrivateKey{Type: typev, KeyBytes: bytes} 18 } 19 func PublicKeyFromBytes(typev ogcrypto_interface.CryptoType, bytes []byte) ogcrypto_interface.PublicKey { 20 return ogcrypto_interface.PublicKey{Type: typev, KeyBytes: bytes} 21 } 22 23 func SignatureValues(sig []byte) (r, s, v *big.Int, err error) { 24 if len(sig) != 65 { 25 return r, s, v, fmt.Errorf("wrong size for signature: got %d, want 65", len(sig)) 26 } 27 r = new(big.Int).SetBytes(sig[:32]) 28 s = new(big.Int).SetBytes(sig[32:64]) 29 v = new(big.Int).SetBytes([]byte{sig[64] + 27}) 30 return r, s, v, nil 31 } 32 33 func PublicKeyFromSignature(sighash og_interface.Hash, signature *ogcrypto_interface.Signature) (pubKey ogcrypto_interface.PublicKey, err error) { 34 // only some signature types can be used to recover pubkey 35 R, S, Vb, err := SignatureValues(signature.SignatureBytes) 36 if err != nil { 37 logrus.WithError(err).Debug("verify sigBytes failed") 38 return 39 } 40 if Vb.BitLen() > 8 { 41 err = errors.New("v len error") 42 logrus.WithError(err).Debug("v len error") 43 return 44 } 45 V := byte(Vb.Uint64() - 27) 46 if !ogcrypto.ValidateSignatureValues(V, R, S, false) { 47 err = errors.New("vrs error") 48 logrus.WithError(err).Debug("validate signature error") 49 return 50 } 51 // encode the signature in uncompressed format 52 r, s := R.Bytes(), S.Bytes() 53 sigBytes := make([]byte, 65) 54 copy(sigBytes[32-len(r):32], r) 55 copy(sigBytes[64-len(s):64], s) 56 sigBytes[64] = V 57 // recover the public key from the signature 58 //pub, err := Ecrecover(sighash.Bytes[:], sigBytes) 59 pub, err := ogcrypto.Ecrecover(sighash.Bytes(), sigBytes) 60 if err != nil { 61 logrus.WithError(err).Debug("sigBytes verify failed") 62 } 63 if len(pub) == 0 || pub[0] != 4 { 64 err := errors.New("invalid public key") 65 logrus.WithError(err).Debug("verify sigBytes failed") 66 } 67 return PublicKeyFromRawBytes(pub), nil 68 } 69 70 func SignatureFromBytes(typev ogcrypto_interface.CryptoType, bytes []byte) ogcrypto_interface.Signature { 71 return ogcrypto_interface.Signature{Type: typev, SignatureBytes: bytes} 72 } 73 74 func PrivateKeyFromRawBytes(bytes []byte) ogcrypto_interface.PrivateKey { 75 cryptoType := ogcrypto_interface.CryptoTypeSecp256k1 76 if len(bytes) == 33 { 77 cryptoType = ogcrypto_interface.CryptoType(bytes[0]) 78 bytes = bytes[1:] 79 } 80 return PrivateKeyFromBytes(cryptoType, bytes) 81 } 82 func PublicKeyFromRawBytes(bytes []byte) ogcrypto_interface.PublicKey { 83 cryptoType := ogcrypto_interface.CryptoTypeSecp256k1 84 if len(bytes) == 33 { 85 cryptoType = ogcrypto_interface.CryptoType(bytes[0]) 86 bytes = bytes[1:] 87 } 88 return PublicKeyFromBytes(cryptoType, bytes) 89 } 90 func SignatureFromRawBytes(bytes []byte) ogcrypto_interface.Signature { 91 cryptoType := ogcrypto_interface.CryptoTypeSecp256k1 92 if len(bytes) == 33 { 93 cryptoType = ogcrypto_interface.CryptoType(bytes[0]) 94 bytes = bytes[1:] 95 } 96 return SignatureFromBytes(cryptoType, bytes) 97 } 98 99 func PrivateKeyFromString(value string) (priv ogcrypto_interface.PrivateKey, err error) { 100 bytes, err := hexutil.FromHex(value) 101 if err != nil { 102 return 103 } 104 priv = PrivateKeyFromRawBytes(bytes) 105 return priv, err 106 } 107 108 func PublicKeyFromString(value string) (pub ogcrypto_interface.PublicKey, err error) { 109 bytes, err := hexutil.FromHex(value) 110 if err != nil { 111 return 112 } 113 pub = PublicKeyFromRawBytes(bytes) 114 return pub, err 115 } 116 117 func PublicKeyFromStringWithCryptoType(ct, pkstr string) (pub ogcrypto_interface.PublicKey, err error) { 118 cryptoType, ok := ogcrypto_interface.CryptoNameMap[ct] 119 if !ok { 120 err = fmt.Errorf("unknown ogcrypto type: %s", ct) 121 return 122 } 123 pk, err := hexutil.FromHex(pkstr) 124 if err != nil { 125 return 126 } 127 pub = ogcrypto_interface.PublicKey{ 128 Type: cryptoType, 129 KeyBytes: pk, 130 } 131 return 132 } 133 134 func Sha256(bytes []byte) []byte { 135 hasher := sha256.New() 136 hasher.Write(bytes) 137 return hasher.Sum(nil) 138 } 139 140 func Ripemd160(bytes []byte) []byte { 141 hasher := ripemd160.New() 142 hasher.Write(bytes) 143 return hasher.Sum(nil) 144 }