-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[crypto] Refactor crypto to be generic / pull ed25519 out of transact…
…ion logic (#9) * [crypto] Refactor crypto to be generic / pull ed25519 out of transaction logic * [crypto] Fix go client with bytes function
- Loading branch information
1 parent
f5af0f8
commit 42dee9c
Showing
7 changed files
with
154 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package aptos | ||
|
||
// Signer a generic interface for any kind of signing | ||
type Signer interface { | ||
Sign(msg []byte) (authenticator Authenticator, err error) | ||
} | ||
|
||
// PrivateKey a generic interface for a signing private key | ||
type PrivateKey interface { | ||
Signer | ||
|
||
/// PubKey Retrieve the public key for signature verification | ||
PubKey() PublicKey | ||
|
||
Bytes() []byte | ||
} | ||
|
||
// PublicKey a generic interface for a public key associated with the private key | ||
type PublicKey interface { | ||
// BCSStruct The public key must be serializable or it will not be used in transactions | ||
BCSStruct | ||
|
||
// Bytes the raw bytes for an authenticator | ||
Bytes() []byte | ||
// Scheme The scheme used for address derivation | ||
Scheme() uint8 | ||
|
||
// TODO: add verify | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package aptos | ||
|
||
import ( | ||
"crypto/ed25519" | ||
) | ||
|
||
type Ed25519PrivateKey struct { | ||
inner ed25519.PrivateKey | ||
} | ||
|
||
func GenerateEd5519Keys() (privkey Ed25519PrivateKey, pubkey Ed25519PublicKey, err error) { | ||
pub, priv, err := ed25519.GenerateKey(nil) | ||
if err != nil { | ||
return | ||
} | ||
privkey = Ed25519PrivateKey{priv} | ||
pubkey = Ed25519PublicKey{pub} | ||
return | ||
} | ||
|
||
func (key *Ed25519PrivateKey) PubKey() PublicKey { | ||
pubkey := key.inner.Public() | ||
return Ed25519PublicKey{ | ||
pubkey.(ed25519.PublicKey), | ||
} | ||
} | ||
|
||
func (key *Ed25519PrivateKey) Bytes() []byte { | ||
return key.inner[:] | ||
} | ||
|
||
func (key *Ed25519PrivateKey) Sign(msg []byte) (authenticator Authenticator, err error) { | ||
publicKeyBytes := key.PubKey().Bytes() | ||
signature := ed25519.Sign(key.inner, msg) | ||
|
||
auth := &Ed25519Authenticator{} | ||
copy(auth.PublicKey[:], publicKeyBytes[:]) | ||
copy(auth.Signature[:], signature[:]) // TODO: Signature type? | ||
authenticator = Authenticator{ | ||
AuthenticatorEd25519, | ||
auth, | ||
} | ||
return | ||
} | ||
|
||
type Ed25519PublicKey struct { | ||
inner ed25519.PublicKey | ||
} | ||
|
||
func (key Ed25519PublicKey) Bytes() []byte { | ||
return key.inner[:] | ||
} | ||
func (key Ed25519PublicKey) Scheme() uint8 { | ||
return Ed25519Scheme | ||
} | ||
|
||
func (key Ed25519PublicKey) MarshalBCS(bcs *Serializer) { | ||
bcs.FixedBytes(key.inner[:]) | ||
} | ||
|
||
func (key Ed25519PublicKey) UnmarshalBCS(bcs *Deserializer) { | ||
key = Ed25519PublicKey{} | ||
bytes := bcs.ReadFixedBytes(32) | ||
if bcs.Error() != nil { | ||
return | ||
} | ||
copy(key.inner[:], bytes) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters