github.com/sixexorg/magnetic-ring@v0.0.0-20191119090307-31705a21e419/gomobile/walletutil/ecdsa.go (about) 1 2 package walletutil 3 4 import ( 5 "bytes" 6 "crypto/ecdsa" 7 "crypto/elliptic" 8 "crypto/rand" 9 "crypto/sha256" 10 "fmt" 11 "github.com/ethereum/go-ethereum/common/math" 12 "github.com/tyler-smith/go-bip39" 13 "github.com/sixexorg/magnetic-ring/common" 14 "github.com/sixexorg/magnetic-ring/crypto" 15 "github.com/sixexorg/magnetic-ring/crypto/secp256k1/bitelliptic" 16 "math/big" 17 ) 18 19 var ( 20 curve = bitelliptic.S256() 21 params = curve.Params() 22 one = big.NewInt(1) 23 24 ) 25 26 func GenerateRandomPrvKey(seed string) (string) { 27 buffer := new(bytes.Buffer) 28 29 seedbuf,err := common.Hex2Bytes(seed) 30 31 if err != nil { 32 return "hex 2 bytes error" 33 } 34 35 buffer.Write(seedbuf) 36 key, err := ecdsa.GenerateKey(bitelliptic.S256(), buffer) 37 if err != nil { 38 return "" 39 } 40 prvBytes := math.PaddedBigBytes(key.D, 32) 41 42 return common.Bytes2Hex(prvBytes) 43 } 44 45 func GetPubKey(prv string) (string) { 46 prvBytes,err:=common.Hex2Bytes(prv) 47 if err != nil { 48 return "hex 2 bytes error" 49 } 50 key, err := crypto.ToECDSAPrivateKey(prvBytes[:]) 51 if err != nil { 52 return "" 53 } 54 pubBytes := elliptic.Marshal(bitelliptic.S256(), key.X, key.Y) 55 return common.Bytes2Hex(pubBytes) 56 } 57 58 59 func Sum256(data []byte) []byte { 60 hash := sha256.Sum256(data) 61 return hash[:] 62 } 63 64 func Sign(prvkstr string, msg string) (string) { 65 66 67 prvBytes,err := common.Hex2Bytes(prvkstr) 68 if err != nil { 69 return "hex 2 bytes error" 70 } 71 prvk,err := crypto.ToECDSAPrivateKey(prvBytes) 72 if err != nil { 73 return "" 74 } 75 r, s, err := ecdsa.Sign(rand.Reader, prvk, []byte(msg)) 76 if err != nil { 77 return "" 78 } 79 80 sigbuf := serializeSignature(r, s) 81 82 return common.Bytes2Hex(sigbuf) 83 } 84 85 func serializeSignature(r, s *big.Int) []byte { 86 size := (params.BitSize + 7) >> 3 87 res := make([]byte, size*2) 88 89 rBytes := r.Bytes() 90 sBytes := s.Bytes() 91 copy(res[size-len(rBytes):], rBytes) 92 copy(res[size*2-len(sBytes):], sBytes) 93 return res 94 } 95 96 func Verify(pubstr string, msg string, sig string) bool { 97 pubBytes,err := common.Hex2Bytes(pubstr) 98 if err != nil { 99 return false 100 } 101 if len(sig) < 64 || len(pubBytes) == 0 { 102 return false 103 } 104 105 pubk,err := crypto.UnmarshalPubkey(pubBytes) 106 107 if err != nil { 108 return false 109 } 110 sigbuf,err:= common.Hex2Bytes(sig) 111 if err != nil { 112 return false 113 } 114 result,err := pubk.Verify([]byte(msg),sigbuf) 115 if err != nil { 116 return false 117 } 118 119 return result 120 } 121 122 func GenerateSeed(pasw string,bitSize int) string { 123 //entropy, _ := bip39.NewEntropy(256) 124 mnemonic := NewMnemonic(bitSize) 125 126 fmt.Printf("mnemonic-->%s\n",mnemonic) 127 //mnemonic = "choice swamp work idle crisp donor all raven perfect dance hedgehog" 128 129 seed := bip39.NewSeed(mnemonic, pasw) 130 return common.Bytes2Hex(seed) 131 } 132 133 func GenerateSeedFromMnemonic(mnemonic,pasw string) string { 134 //entropy, _ := bip39.NewEntropy(256) 135 //mnemonic := NewMnemonic(bitSize) 136 137 fmt.Printf("mnemonic-->%s\n",mnemonic) 138 //mnemonic = "choice swamp work idle crisp donor all raven perfect dance hedgehog" 139 140 seed := bip39.NewSeed(mnemonic, pasw) 141 return common.Bytes2Hex(seed) 142 } 143 144 func NewMnemonic(bitsize int) string { 145 146 entropy, _ := bip39.NewEntropy(bitsize)//bitsize suggest 256 147 mnemonic, err := bip39.NewMnemonic(entropy) 148 if err != nil { 149 return err.Error() 150 } 151 //mnemonic = "choice swamp work idle crisp donor all raven perfect dance hedgehog" 152 return mnemonic 153 } 154 155 func IsMnemonicValid(nem string) bool { 156 gui := bip39.IsMnemonicValid(nem) 157 return gui 158 } 159