Skip to content

Commit

Permalink
update account mgmt...
Browse files Browse the repository at this point in the history
  • Loading branch information
astinz committed May 29, 2024
1 parent 3c1bada commit 27fc64e
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 22 deletions.
33 changes: 33 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,39 @@ val aptos = Aptos(config)
val modules = aptos.getAccountModules("0x1".toAccountAddress())
```

### Account management (default to Ed25519)

#### Generate new keys

```kotlin
val account = Account.generate()
```

#### Derive from private key
```kotlin
// Create a private key instance for Ed25519 scheme
val privateKey = Ed25519PrivateKey("myEd25519privatekeystring")

// Derive an account from private key

// This is used as a local calculation and therefore is used to instantiate an `Account`
// that has not had its authentication key rotated
val account = Account.fromPrivateKey(privateKey)
```

#### Derive from private key and address

```kotlin
// Create a private key instance for Ed25519 scheme
val privateKey = Ed25519PrivateKey("myEd25519privatekeystring")

// Derive an account from private key and address

// create an AccountAddress instance from the account address string
val address = AccountAddress.fromString()

```

## Testing

To run the SDK tests, simply run from the root of this repository:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package xyz.mcxross.kaptos.core.crypto

import java.security.KeyPairGenerator
import java.security.NoSuchAlgorithmException
import java.security.SecureRandom
import java.util.*
Expand All @@ -12,22 +11,24 @@ import xyz.mcxross.kaptos.model.SigningScheme

@Throws(NoSuchAlgorithmException::class)
actual fun generateKeypair(scheme: SigningScheme): KeyPair {
val kpg = KeyPairGenerator.getInstance("Ed25519")
val kp = kpg.generateKeyPair()
val sk: ByteArray = kp.private.encoded
val pk: ByteArray = kp.public.encoded
return KeyPair(sk.copyOfRange(12, sk.size), pk.copyOfRange(12, pk.size))

when (scheme) {
SigningScheme.Ed25519 -> {
val kpg = Ed25519KeyPairGenerator()
kpg.init(Ed25519KeyGenerationParameters(SecureRandom()))
val kp = kpg.generateKeyPair()
val sk = kp.private as Ed25519PrivateKeyParameters
val pk = kp.public as Ed25519PublicKeyParameters
return KeyPair(sk.encoded, pk.encoded)
}
else -> throw NotImplementedError("Only Ed25519 is supported at the moment")
}
}

actual fun fromSeed(seed: ByteArray): KeyPair {
val secureRandom = SecureRandom(seed)
val keyGenParams = Ed25519KeyGenerationParameters(secureRandom)
val keyPairGenerator = Ed25519KeyPairGenerator()
keyPairGenerator.init(keyGenParams)
val keyPair = keyPairGenerator.generateKeyPair()
val privateKey = keyPair.private as Ed25519PrivateKeyParameters
val publicKey = keyPair.public as Ed25519PublicKeyParameters
return KeyPair(privateKey.encoded, publicKey.encoded)
val privateKeyParameters = Ed25519PrivateKeyParameters(seed, 0)
val publicKeyParameters = privateKeyParameters.generatePublicKey()
return KeyPair(privateKeyParameters.encoded, publicKeyParameters.encoded)
}

actual fun sha3Hash(input: ByteArray): ByteArray {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package xyz.mcxross.kaptos.core.account

import xyz.mcxross.kaptos.model.AccountAddress
import xyz.mcxross.kaptos.model.HexInput
import xyz.mcxross.kaptos.core.crypto.Ed25519PrivateKey
import xyz.mcxross.kaptos.core.crypto.PrivateKey
import xyz.mcxross.kaptos.core.crypto.PublicKey
import xyz.mcxross.kaptos.core.crypto.Signature
import xyz.mcxross.kaptos.model.SigningScheme
import xyz.mcxross.kaptos.model.SigningSchemeInput
import xyz.mcxross.kaptos.model.*

abstract class Account {

Expand All @@ -20,6 +19,7 @@ abstract class Account {

/**
* Sign the given message with the private key.
*
* @param message in HexInput format
* @returns AccountSignature
*/
Expand All @@ -36,5 +36,16 @@ abstract class Account {

throw NotImplementedError("Only Ed25519 is supported at the moment")
}

fun fromPrivateKey(
privateKey: PrivateKey,
address: AccountAddressInput? = null,
legacy: Boolean = true,
): Account {
if (privateKey is Ed25519PrivateKey && legacy) {
return Ed25519Account(privateKey, address)
}
throw NotImplementedError("Only Ed25519 is supported at the moment")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package xyz.mcxross.kaptos.core.crypto

import xyz.mcxross.kaptos.core.AuthenticationKey
import xyz.mcxross.kaptos.core.Hex
import xyz.mcxross.kaptos.model.HexInput
import xyz.mcxross.kaptos.model.AuthenticationKeyScheme
import xyz.mcxross.kaptos.model.HexInput
import xyz.mcxross.kaptos.model.SigningScheme

/**
Expand Down Expand Up @@ -74,6 +74,8 @@ class Ed25519PrivateKey(data: HexInput) : PrivateKey {
signingKeyPair = KeyPair.fromSecretSeed(hex.toByteArray())
}

constructor(data: String) : this(HexInput.fromString(data))

/**
* Sign the given message with the private key.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ package xyz.mcxross.kaptos.core.crypto
/**
* Represents a pair of signing keys: a public key and a private key.
*
* @property publicKey The public key in hex format.
* @property privateKey The private key in hex format.
* @property privateKey The public key in hex format.
* @property publicKey The private key in hex format.
*/
data class KeyPair(val publicKey: ByteArray, val privateKey: ByteArray) {
data class KeyPair(val privateKey: ByteArray, val publicKey: ByteArray) {

fun sign(message: ByteArray): Signature {
TODO()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,5 +278,9 @@ data class HexInput(override val value: String) : AccountAddressInput {
fun fromByteArray(byteArray: ByteArray): HexInput {
return HexInput(byteArray.joinToString("") { it.toUByte().toString(16).padStart(2, '0') })
}

fun fromString(string: String): HexInput {
return HexInput(string)
}
}
}

0 comments on commit 27fc64e

Please sign in to comment.