예제 #1
0
파일: entry.go 프로젝트: skycoin/skycoin
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,
	}
}
예제 #2
0
파일: readable.go 프로젝트: skycoin/skycoin
// 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,
	}
}
예제 #3
0
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
}
예제 #4
0
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
}