2019-07-25 22:15:33 +00:00
|
|
|
package wallet
|
2019-07-05 14:29:17 +00:00
|
|
|
|
|
|
|
import (
|
2019-07-13 00:41:32 +00:00
|
|
|
"sort"
|
2019-07-18 15:41:30 +00:00
|
|
|
"strings"
|
2019-07-08 12:51:45 +00:00
|
|
|
|
2019-07-19 11:56:18 +00:00
|
|
|
"github.com/filecoin-project/go-bls-sigs"
|
|
|
|
"github.com/minio/blake2b-simd"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
2019-07-18 15:41:30 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
2019-07-08 12:51:45 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/lib/crypto"
|
2019-07-05 14:29:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-07-18 15:41:30 +00:00
|
|
|
KNamePrefix = "wallet-"
|
2019-07-05 14:29:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Wallet struct {
|
2019-07-18 15:41:30 +00:00
|
|
|
keys map[address.Address]*Key
|
|
|
|
keystore types.KeyStore
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:41:30 +00:00
|
|
|
func NewWallet(keystore types.KeyStore) (*Wallet, error) {
|
|
|
|
w := &Wallet{
|
|
|
|
keys: make(map[address.Address]*Key),
|
|
|
|
keystore: keystore,
|
|
|
|
}
|
|
|
|
|
|
|
|
return w, nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 05:38:16 +00:00
|
|
|
func (w *Wallet) Sign(addr address.Address, msg []byte) (*types.Signature, error) {
|
2019-07-05 14:29:17 +00:00
|
|
|
ki, err := w.findKey(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ki.Type {
|
2019-07-24 05:38:16 +00:00
|
|
|
case types.KTSecp256k1:
|
2019-07-05 14:29:17 +00:00
|
|
|
b2sum := blake2b.Sum256(msg)
|
|
|
|
sig, err := crypto.Sign(ki.PrivateKey, b2sum[:])
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-07-24 05:38:16 +00:00
|
|
|
return &types.Signature{
|
|
|
|
Type: types.KTSecp256k1,
|
2019-07-05 14:29:17 +00:00
|
|
|
Data: sig,
|
|
|
|
}, nil
|
2019-07-24 05:38:16 +00:00
|
|
|
case types.KTBLS:
|
2019-07-05 14:29:17 +00:00
|
|
|
var pk bls.PrivateKey
|
|
|
|
copy(pk[:], ki.PrivateKey)
|
|
|
|
sig := bls.PrivateKeySign(pk, msg)
|
|
|
|
|
2019-07-24 05:38:16 +00:00
|
|
|
return &types.Signature{
|
|
|
|
Type: types.KTBLS,
|
2019-07-05 14:29:17 +00:00
|
|
|
Data: sig[:],
|
|
|
|
}, nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic("cant do it sir")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:41:30 +00:00
|
|
|
func (w *Wallet) findKey(addr address.Address) (*Key, error) {
|
|
|
|
k, ok := w.keys[addr]
|
|
|
|
if ok {
|
|
|
|
return k, nil
|
|
|
|
}
|
|
|
|
ki, err := w.keystore.Get(KNamePrefix + addr.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting from keystore: %w", err)
|
|
|
|
}
|
|
|
|
k, err = NewKey(ki)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("decoding from keystore: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
2019-07-18 15:41:30 +00:00
|
|
|
w.keys[k.Address] = k
|
|
|
|
return k, nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Wallet) Export(addr address.Address) ([]byte, error) {
|
|
|
|
panic("nyi")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Wallet) Import(kdata []byte) (address.Address, error) {
|
|
|
|
panic("nyi")
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:41:30 +00:00
|
|
|
func (w *Wallet) ListAddrs() ([]address.Address, error) {
|
|
|
|
all, err := w.keystore.List()
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("listing keystore: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(all)
|
|
|
|
|
|
|
|
out := make([]address.Address, 0, len(all))
|
|
|
|
for _, a := range all {
|
|
|
|
if strings.HasPrefix(a, KNamePrefix) {
|
|
|
|
name := strings.TrimPrefix(a, KNamePrefix)
|
|
|
|
addr, err := address.NewFromString(name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("converting name to address: %w", err)
|
|
|
|
}
|
|
|
|
out = append(out, addr)
|
|
|
|
}
|
2019-07-13 00:41:32 +00:00
|
|
|
}
|
2019-07-18 15:41:30 +00:00
|
|
|
|
|
|
|
return out, nil
|
2019-07-13 00:41:32 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
func (w *Wallet) GenerateKey(typ string) (address.Address, error) {
|
2019-07-18 15:41:30 +00:00
|
|
|
var k *Key
|
2019-07-05 14:29:17 +00:00
|
|
|
switch typ {
|
2019-07-24 05:38:16 +00:00
|
|
|
case types.KTSecp256k1:
|
2019-07-18 15:41:30 +00:00
|
|
|
priv, err := crypto.GenerateKey()
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
2019-07-18 15:44:10 +00:00
|
|
|
ki := types.KeyInfo{
|
|
|
|
Type: typ,
|
|
|
|
PrivateKey: priv,
|
|
|
|
}
|
2019-07-05 14:29:17 +00:00
|
|
|
|
2019-07-18 15:44:10 +00:00
|
|
|
k, err = NewKey(ki)
|
2019-07-18 15:41:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
2019-07-24 05:38:16 +00:00
|
|
|
case types.KTBLS:
|
2019-07-05 14:29:17 +00:00
|
|
|
priv := bls.PrivateKeyGenerate()
|
2019-07-18 15:44:10 +00:00
|
|
|
ki := types.KeyInfo{
|
|
|
|
Type: typ,
|
|
|
|
PrivateKey: priv[:],
|
|
|
|
}
|
2019-07-05 14:29:17 +00:00
|
|
|
|
2019-07-18 15:41:30 +00:00
|
|
|
var err error
|
2019-07-18 15:44:10 +00:00
|
|
|
k, err = NewKey(ki)
|
2019-07-18 15:41:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
default:
|
2019-07-18 15:41:30 +00:00
|
|
|
return address.Undef, xerrors.Errorf("invalid key type: %s", typ)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := w.keystore.Put(KNamePrefix+k.Address.String(), k.KeyInfo)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, xerrors.Errorf("saving to keystore: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
2019-07-18 15:41:30 +00:00
|
|
|
w.keys[k.Address] = k
|
|
|
|
return k.Address, nil
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 00:46:56 +00:00
|
|
|
func (w *Wallet) HasKey(addr address.Address) (bool, error) {
|
|
|
|
k, err := w.findKey(addr)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return k != nil, nil
|
|
|
|
}
|
|
|
|
|
2019-07-18 15:41:30 +00:00
|
|
|
type Key struct {
|
|
|
|
types.KeyInfo
|
2019-07-05 14:29:17 +00:00
|
|
|
|
2019-07-18 15:41:30 +00:00
|
|
|
PublicKey []byte
|
|
|
|
Address address.Address
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-18 15:41:30 +00:00
|
|
|
func NewKey(keyinfo types.KeyInfo) (*Key, error) {
|
|
|
|
k := &Key{
|
|
|
|
KeyInfo: keyinfo,
|
|
|
|
}
|
|
|
|
|
|
|
|
switch k.Type {
|
2019-07-24 05:38:16 +00:00
|
|
|
case types.KTSecp256k1:
|
2019-07-18 15:41:30 +00:00
|
|
|
k.PublicKey = crypto.PublicKey(k.PrivateKey)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
k.Address, err = address.NewSecp256k1Address(k.PublicKey)
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != nil {
|
2019-07-18 15:41:30 +00:00
|
|
|
return nil, xerrors.Errorf("converting Secp256k1 to address: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
|
|
|
|
2019-07-24 05:38:16 +00:00
|
|
|
case types.KTBLS:
|
2019-07-05 14:29:17 +00:00
|
|
|
var pk bls.PrivateKey
|
2019-07-18 15:41:30 +00:00
|
|
|
copy(pk[:], k.PrivateKey)
|
2019-07-05 14:29:17 +00:00
|
|
|
pub := bls.PrivateKeyPublicKey(pk)
|
2019-07-18 15:41:30 +00:00
|
|
|
k.PublicKey = pub[:]
|
|
|
|
|
|
|
|
var err error
|
|
|
|
k.Address, err = address.NewBLSAddress(k.PublicKey)
|
2019-07-05 14:29:17 +00:00
|
|
|
if err != nil {
|
2019-07-18 15:41:30 +00:00
|
|
|
return nil, xerrors.Errorf("converting BLS to address: %w", err)
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
2019-07-18 15:41:30 +00:00
|
|
|
|
2019-07-05 14:29:17 +00:00
|
|
|
default:
|
2019-07-18 15:41:30 +00:00
|
|
|
return nil, xerrors.Errorf("unknown key type")
|
2019-07-05 14:29:17 +00:00
|
|
|
}
|
2019-07-18 15:41:30 +00:00
|
|
|
return k, nil
|
|
|
|
|
2019-07-05 14:36:08 +00:00
|
|
|
}
|