func WalletEntryFromReadable(w *ReadableWalletEntry) WalletEntry { // SimpleWallet entries are shared as a form of identification, the secret key // is not required // TODO -- fix lib/base58 to not panic on invalid input -- should // return error, so we can detect a broken wallet. if w.Address == "" { //log.Panic("ReadableWalletEntry has no Address") } var s cipher.SecKey if w.Secret != "" { s = cipher.MustSecKeyFromHex(w.Secret) } //regen from the private key //redundant/ if w.Address == "" { addr := cipher.AddressFromSecKey(s) pub := cipher.PubKeyFromSecKey(s) return WalletEntry{ Address: addr, Public: pub, Secret: s, } } return WalletEntry{ Address: cipher.MustDecodeBase58Address(w.Address), Public: cipher.MustPubKeyFromHex(w.Public), Secret: s, } }
// Creates a ReadableWalletEntry given a pubkey hex string. The Secret field // is left empty. func ReadableWalletEntryFromPubkey(pub string) ReadableWalletEntry { pubkey := cipher.MustPubKeyFromHex(pub) addr := cipher.AddressFromPubKey(pubkey) return ReadableWalletEntry{ Address: addr.String(), Public: pub, } }
func Decrypt(in []byte, nonce []byte, pubkey string, seckey string) (data []byte, err error) { defer func() { if r := recover(); r != nil { err = errors.New("encrypt faild") } }() p := cipher.MustPubKeyFromHex(pubkey) s := cipher.MustSecKeyFromHex(seckey) key := cipher.ECDH(p, s) data, err = cipher.Chacha20Decrypt(in, key, nonce) return }
func Encrypt(r interface{}, pubkey string, seckey string) (data []byte, nonce []byte, err error) { defer func() { if r := recover(); r != nil { err = errors.New("encrypt faild") } }() d, err := json.Marshal(r) if err != nil { return } p := cipher.MustPubKeyFromHex(pubkey) s := cipher.MustSecKeyFromHex(seckey) nonce = cipher.RandByte(chacha20.NonceSize) key := cipher.ECDH(p, s) data, err = cipher.Chacha20Encrypt([]byte(d), key, nonce) return }