Skip to content

Commit

Permalink
begin work on generifying keys
Browse files Browse the repository at this point in the history
  • Loading branch information
QuestofIranon committed Jan 14, 2020
1 parent 6c9c77c commit 21bfdaa
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 32 deletions.
6 changes: 4 additions & 2 deletions account_info_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type AccountInfo struct {
Deleted bool
ProxyAccountID AccountID
ProxyReceived int64
Key Ed25519PublicKey
Key PublicKey
Balance uint64
GenerateSendRecordThreshold uint64
GenerateReceiveRecordThreshold uint64
Expand Down Expand Up @@ -45,13 +45,15 @@ func (builder *AccountInfoQuery) Execute(client *Client) (AccountInfo, error) {
return AccountInfo{}, err
}

pubKey, err := PublicKeyFromProto(resp.GetCryptoGetInfo().AccountInfo.Key)

return AccountInfo{
AccountID: accountIDFromProto(resp.GetCryptoGetInfo().AccountInfo.AccountID),
ContractAccountID: resp.GetCryptoGetInfo().AccountInfo.ContractAccountID,
Deleted: resp.GetCryptoGetInfo().AccountInfo.Deleted,
ProxyAccountID: accountIDFromProto(resp.GetCryptoGetInfo().AccountInfo.ProxyAccountID),
ProxyReceived: resp.GetCryptoGetInfo().AccountInfo.ProxyReceived,
Key: Ed25519PublicKey{keyData: resp.GetCryptoGetInfo().AccountInfo.Key.GetEd25519()},
Key: pubKey,
Balance: resp.GetCryptoGetInfo().AccountInfo.Balance,
GenerateSendRecordThreshold: resp.GetCryptoGetInfo().AccountInfo.GenerateSendRecordThreshold,
GenerateReceiveRecordThreshold: resp.GetCryptoGetInfo().AccountInfo.GenerateReceiveRecordThreshold,
Expand Down
93 changes: 75 additions & 18 deletions crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,73 @@ import (
const ed25519PrivateKeyPrefix = "302e020100300506032b657004220420"
const ed25519PubKeyPrefix = "302a300506032b6570032100"

type PrivateKey interface {
String() string
Bytes() []byte
Sign(message []byte) []byte
Keystore(passphrase string) ([]byte, error)
WriteKeystore(destination io.Writer, passphrase string) error
}

type PublicKey interface {
Bytes() []byte
toProto() *proto.Key
}

type keyList struct {
list []PublicKey
}
func (kl keyList) toProto() *proto.Key {
var pbList = make([]*proto.Key, len(kl.list))

for i, key := range kl.list {
pbList[i] = key.toProto()
}

return &proto.Key{Key: &proto.Key_KeyList{KeyList: &proto.KeyList{Keys: pbList}}}
}

func (kl keyList) Bytes() []byte {
// todo: fill this in
return []byte{}
}

func PublicKeyFromProto(pbKey *proto.Key) (PublicKey, error) {
switch key := pbKey.GetKey().(type){
case *proto.Key_Ed25519:
return Ed25519PublicKeyFromBytes(key.Ed25519)
case *proto.Key_ThresholdKey:
return nil, fmt.Errorf("threshold key not yet implemented")
case *proto.Key_KeyList:
return keyListFromProto(key.KeyList)

case *proto.Key_ContractID:
return nil, fmt.Errorf("contractID key is deprecated")
case *proto.Key_RSA_3072:
return nil, fmt.Errorf("RSA_3072 key not yet supported")
case *proto.Key_ECDSA_384:
return nil, fmt.Errorf("ECDSA_384 key not yet supported")
default:
return nil, fmt.Errorf("invalid key provided")
}
}

func keyListFromProto(pb *proto.KeyList) (keyList, error) {
var keys keyList = make([]PublicKey, len(pb.Keys))

for i, pbKey := range pb.Keys {
key, err := PublicKeyFromProto(pbKey)

if err != nil {
return keyList{}, err
}

keys[i] = key
}

return keys, nil
}

type Ed25519PrivateKey struct {
keyData []byte
chainCode []byte
Expand Down Expand Up @@ -150,16 +217,6 @@ func Ed25519PublicKeyFromBytes(bytes []byte) (Ed25519PublicKey, error) {
}, nil
}

func ed25519PublicKeyFromProto(proto proto.Key) (Ed25519PublicKey, error) {
rawKey := proto.GetEd25519()

if rawKey == nil {
return Ed25519PublicKey{}, fmt.Errorf("provided proto key did not represent an ed25519 key")
}

return Ed25519PublicKeyFromBytes(rawKey)
}

// SLIP-10/BIP-32 Child Key derivation
func deriveChildKey(parentKey []byte, chainCode []byte, index uint32) ([]byte, []byte) {
h := hmac.New(sha512.New, chainCode)
Expand Down Expand Up @@ -215,14 +272,6 @@ func (sk Ed25519PrivateKey) WriteKeystore(destination io.Writer, passphrase stri
return err
}

func (pk Ed25519PublicKey) Bytes() []byte {
return pk.keyData
}

func (pk Ed25519PublicKey) toProto() *proto.Key {
return &proto.Key{Key: &proto.Key_Ed25519{Ed25519: pk.keyData}}
}

func (sk Ed25519PrivateKey) Sign(message []byte) []byte {
return ed25519.Sign(sk.keyData, message)
}
Expand Down Expand Up @@ -252,3 +301,11 @@ func (sk Ed25519PrivateKey) Derive(index uint32) (Ed25519PrivateKey, error) {

return derivedKey, nil
}

func (pk Ed25519PublicKey) Bytes() []byte {
return pk.keyData
}

func (pk Ed25519PublicKey) toProto() *proto.Key {
return &proto.Key{Key: &proto.Key_Ed25519{Ed25519: pk.keyData}}
}
2 changes: 1 addition & 1 deletion file_create_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func NewFileCreateTransaction() FileCreateTransaction {
return builder
}

func (builder FileCreateTransaction) AddKey(publicKey Ed25519PublicKey) FileCreateTransaction {
func (builder FileCreateTransaction) AddKey(publicKey PublicKey) FileCreateTransaction {
builder.pb.Keys.Keys = append(builder.pb.Keys.Keys, publicKey.toProto())
return builder
}
Expand Down
6 changes: 3 additions & 3 deletions file_info_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type FileInfo struct {
Size int64
ExpirationTime time.Time
Deleted bool
Keys []Ed25519PublicKey
Keys []PublicKey
}

func NewFileInfoQuery() *FileInfoQuery {
Expand All @@ -40,9 +40,9 @@ func (builder *FileInfoQuery) Execute(client *Client) (FileInfo, error) {

pbKeyList := resp.GetFileGetInfo().FileInfo.Keys.Keys

keyList := make([]Ed25519PublicKey, len(pbKeyList))
keyList := make([]PublicKey, len(pbKeyList))

for i, key := range(pbKeyList) {
for i, key := range pbKeyList {

// todo: support more than ed25519keys
keyList[i], err = Ed25519PublicKeyFromBytes(key.GetEd25519())
Expand Down
16 changes: 8 additions & 8 deletions file_update_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,23 +27,23 @@ func (builder FileUpdateTransaction) SetFileID(id FileID) FileUpdateTransaction
return builder
}

func (builder FileUpdateTransaction) AddKey(publicKey Ed25519PublicKey) FileUpdateTransaction {
var keylist *proto.KeyList
func (builder FileUpdateTransaction) AddKey(publicKey PublicKey) FileUpdateTransaction {
var keyList *proto.KeyList
if builder.pb.Keys != nil {
keylist = builder.pb.Keys
keyList = builder.pb.Keys
} else {
keylist = &proto.KeyList{}
keyList = &proto.KeyList{}
}

var keyarray []*proto.Key
if keylist.Keys != nil {
keyarray = keylist.GetKeys()
if keyList.Keys != nil {
keyarray = keyList.GetKeys()
} else {
keyarray = []*proto.Key{}
}

keylist.Keys = append(keyarray, publicKey.toProto())
builder.pb.Keys = keylist
keyList.Keys = append(keyarray, publicKey.toProto())
builder.pb.Keys = keyList

return builder
}
Expand Down

0 comments on commit 21bfdaa

Please sign in to comment.