-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[account] Improve account experience #125
base: main
Are you sure you want to change the base?
Changes from all commits
4c7f797
60b12a7
0765f99
3deec23
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,11 +17,11 @@ type Account struct { | |
} | ||
|
||
// NewAccountFromSigner creates an account from a [crypto.Signer] with an optional [crypto.AuthenticationKey] | ||
func NewAccountFromSigner(signer crypto.Signer, authKey ...crypto.AuthenticationKey) (*Account, error) { | ||
func NewAccountFromSigner(signer crypto.Signer, address ...AccountAddress) (*Account, error) { | ||
out := &Account{} | ||
if len(authKey) == 1 { | ||
copy(out.Address[:], authKey[0][:]) | ||
} else if len(authKey) > 1 { | ||
if len(address) == 1 { | ||
copy(out.Address[:], address[0][:]) | ||
} else if len(address) > 1 { | ||
// Throw error | ||
return nil, errors.New("must only provide one auth key") | ||
} else { | ||
|
@@ -60,6 +60,42 @@ func NewSecp256k1Account() (*Account, error) { | |
return NewAccountFromSigner(signer) | ||
} | ||
|
||
// ExtractMessageSigner extracts the message signer from the account for | ||
func (account *Account) ExtractMessageSigner() (crypto.MessageSigner, bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth returning There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. On the same consistency topic, do we need the |
||
ed25519PrivateKey, ok := account.Signer.(*crypto.Ed25519PrivateKey) | ||
if ok { | ||
return ed25519PrivateKey, ok | ||
} | ||
singleSigner, ok := account.Signer.(*crypto.SingleSigner) | ||
if ok { | ||
return singleSigner.Signer, ok | ||
} | ||
return nil, false | ||
} | ||
|
||
// ExtractPrivateKeyString extracts the private key string | ||
func (account *Account) ExtractPrivateKeyString() (string, error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it worth exposing the PrivateKey as a byte slice of the raw bytes instead of |
||
// Handle the key by itself | ||
ed25519PrivateKey, ok := account.Signer.(*crypto.Ed25519PrivateKey) | ||
if ok { | ||
return ed25519PrivateKey.ToAIP80() | ||
} | ||
|
||
// Handle key in single signer | ||
singleSigner, ok := account.Signer.(*crypto.SingleSigner) | ||
if ok { | ||
innerSigner := singleSigner.Signer | ||
switch innerSigner.(type) { | ||
case *crypto.Ed25519PrivateKey: | ||
return innerSigner.(*crypto.Ed25519PrivateKey).ToAIP80() | ||
case *crypto.Secp256k1PrivateKey: | ||
return innerSigner.(*crypto.Secp256k1PrivateKey).ToAIP80() | ||
} | ||
} | ||
|
||
return "", errors.New("signer is not a private key") | ||
} | ||
|
||
// Sign signs a message, returning an appropriate authenticator for the signer | ||
func (account *Account) Sign(message []byte) (authenticator *crypto.AccountAuthenticator, err error) { | ||
return account.Signer.Sign(message) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package types | ||
|
||
import ( | ||
"errors" | ||
"github.com/aptos-labs/aptos-go-sdk/crypto" | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
|
@@ -22,6 +23,26 @@ func TestGenerateEd25519Account(t *testing.T) { | |
assert.True(t, output.Auth.Verify(message)) | ||
} | ||
|
||
func TestGenerateSingleSignerEd25519Account(t *testing.T) { | ||
message := []byte{0x12, 0x34} | ||
account, err := NewEd25519SingleSignerAccount() | ||
assert.NoError(t, err) | ||
output, err := account.Sign(message) | ||
assert.NoError(t, err) | ||
assert.Equal(t, crypto.AccountAuthenticatorSingleSender, output.Variant) | ||
assert.True(t, output.Auth.Verify(message)) | ||
} | ||
|
||
func TestGenerateSecp256k1Account(t *testing.T) { | ||
message := []byte{0x12, 0x34} | ||
account, err := NewSecp256k1Account() | ||
assert.NoError(t, err) | ||
output, err := account.Sign(message) | ||
assert.NoError(t, err) | ||
assert.Equal(t, crypto.AccountAuthenticatorSingleSender, output.Variant) | ||
assert.True(t, output.Auth.Verify(message)) | ||
} | ||
|
||
func TestNewAccountFromSigner(t *testing.T) { | ||
message := []byte{0x12, 0x34} | ||
key, err := crypto.GenerateEd25519PrivateKey() | ||
|
@@ -43,24 +64,120 @@ func TestNewAccountFromSignerWithAddress(t *testing.T) { | |
key, err := crypto.GenerateEd25519PrivateKey() | ||
assert.NoError(t, err) | ||
|
||
authenticationKey := crypto.AuthenticationKey{} | ||
|
||
account, err := NewAccountFromSigner(key, authenticationKey) | ||
account, err := NewAccountFromSigner(key, AccountZero) | ||
assert.NoError(t, err) | ||
output, err := account.Sign(message) | ||
assert.NoError(t, err) | ||
assert.Equal(t, crypto.AccountAuthenticatorEd25519, output.Variant) | ||
assert.True(t, output.Auth.Verify(message)) | ||
|
||
outputSig, err := account.SignMessage(message) | ||
assert.NoError(t, err) | ||
assert.True(t, account.Signer.PubKey().Verify(message, outputSig)) | ||
|
||
assert.Equal(t, AccountZero, account.Address) | ||
assert.Equal(t, AccountZero, account.AccountAddress()) | ||
assert.Equal(t, key.AuthKey(), account.AuthKey()) | ||
assert.Equal(t, key.PubKey(), account.PubKey()) | ||
} | ||
|
||
func TestNewAccountFromSignerWithAddressMulti(t *testing.T) { | ||
key, err := crypto.GenerateEd25519PrivateKey() | ||
assert.NoError(t, err) | ||
|
||
authenticationKey := crypto.AuthenticationKey{} | ||
_, err = NewAccountFromSigner(key, AccountZero, AccountOne) | ||
assert.Error(t, err) | ||
} | ||
|
||
type WrapperSigner struct { | ||
signer crypto.Signer | ||
} | ||
|
||
func (w *WrapperSigner) Sign(_ []byte) (*crypto.AccountAuthenticator, error) { | ||
return nil, errors.New("not implemented") | ||
} | ||
func (w *WrapperSigner) SignMessage(_ []byte) (crypto.Signature, error) { | ||
return nil, errors.New("not implemented") | ||
} | ||
func (w *WrapperSigner) SimulationAuthenticator() *crypto.AccountAuthenticator { | ||
return nil | ||
} | ||
func (w *WrapperSigner) AuthKey() *crypto.AuthenticationKey { | ||
return &crypto.AuthenticationKey{} | ||
} | ||
func (w *WrapperSigner) PubKey() crypto.PublicKey { | ||
// Note this is just for testing | ||
return &crypto.Ed25519PublicKey{} | ||
} | ||
|
||
func TestAccount_ExtractMessageSigner(t *testing.T) { | ||
ed25519PrivateKey, err := crypto.GenerateEd25519PrivateKey() | ||
assert.NoError(t, err) | ||
ed25519Account, err := NewAccountFromSigner(ed25519PrivateKey) | ||
assert.NoError(t, err) | ||
|
||
ed25519Out, ok := ed25519Account.ExtractMessageSigner() | ||
assert.True(t, ok) | ||
assert.Equal(t, ed25519PrivateKey, ed25519Out) | ||
|
||
ed25519SingleSignerAccount, err := NewAccountFromSigner(crypto.NewSingleSigner(ed25519PrivateKey)) | ||
assert.NoError(t, err) | ||
|
||
ed25519Out, ok = ed25519SingleSignerAccount.ExtractMessageSigner() | ||
assert.True(t, ok) | ||
assert.Equal(t, ed25519PrivateKey, ed25519Out) | ||
|
||
secp256k1PrivateKey, err := crypto.GenerateSecp256k1Key() | ||
assert.NoError(t, err) | ||
secp256k1SingleSignerAccount, err := NewAccountFromSigner(crypto.NewSingleSigner(secp256k1PrivateKey)) | ||
assert.NoError(t, err) | ||
|
||
secp256k1Out, ok := secp256k1SingleSignerAccount.ExtractMessageSigner() | ||
assert.True(t, ok) | ||
assert.Equal(t, secp256k1PrivateKey, secp256k1Out) | ||
|
||
wrapperSigner := &WrapperSigner{signer: secp256k1SingleSignerAccount} | ||
customAccount, err := NewAccountFromSigner(wrapperSigner) | ||
assert.NoError(t, err) | ||
out, ok := customAccount.ExtractMessageSigner() | ||
assert.False(t, ok) | ||
assert.Nil(t, out) | ||
} | ||
|
||
func TestAccount_ExtractPrivateKeyString(t *testing.T) { | ||
ed25519PrivateKey, err := crypto.GenerateEd25519PrivateKey() | ||
assert.NoError(t, err) | ||
ed25519Account, err := NewAccountFromSigner(ed25519PrivateKey) | ||
assert.NoError(t, err) | ||
|
||
_, err = NewAccountFromSigner(key, authenticationKey, authenticationKey) | ||
ed25519KeyString, err := ed25519Account.ExtractPrivateKeyString() | ||
assert.NoError(t, err) | ||
expectedEd25519String, err := ed25519PrivateKey.ToAIP80() | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedEd25519String, ed25519KeyString) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe add another test case to verify AIP 80
|
||
|
||
ed25519SingleSignerAccount, err := NewAccountFromSigner(crypto.NewSingleSigner(ed25519PrivateKey)) | ||
assert.NoError(t, err) | ||
|
||
ed25519SingleSignerKeyString, err := ed25519SingleSignerAccount.ExtractPrivateKeyString() | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedEd25519String, ed25519SingleSignerKeyString) | ||
|
||
secp256k1PrivateKey, err := crypto.GenerateSecp256k1Key() | ||
assert.NoError(t, err) | ||
secp256k1SingleSignerAccount, err := NewAccountFromSigner(crypto.NewSingleSigner(secp256k1PrivateKey)) | ||
assert.NoError(t, err) | ||
|
||
expectedSecp256k1String, err := secp256k1PrivateKey.ToAIP80() | ||
assert.NoError(t, err) | ||
secp256k1SingleSignerKeyString, err := secp256k1SingleSignerAccount.ExtractPrivateKeyString() | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedSecp256k1String, secp256k1SingleSignerKeyString) | ||
|
||
wrapperSigner := &WrapperSigner{signer: secp256k1SingleSignerAccount} | ||
customAccount, err := NewAccountFromSigner(wrapperSigner) | ||
assert.NoError(t, err) | ||
out, err := customAccount.ExtractPrivateKeyString() | ||
assert.Error(t, err) | ||
assert.Empty(t, out) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is
for
needed?