-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[crypto] Refactor Keys, added examples, and Keyless simulation signat…
…ure (#3) * Add examples * Refactor key naming * Add keyless simulation signatures * Fix formatting * Add SingleKey tests
- Loading branch information
1 parent
de8fa01
commit f481389
Showing
30 changed files
with
743 additions
and
582 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace Aptos.Examples; | ||
|
||
public class SimpleTransferMultiKeyExample | ||
{ | ||
public static async Task Run() | ||
{ | ||
Console.WriteLine("=== Addresses ===\n"); | ||
var aptos = new AptosClient(new AptosConfig(Networks.Devnet)); | ||
var aliceSigner = Account.Generate(); | ||
var bobSigner = SingleKeyAccount.Generate(); | ||
var account = new MultiKeyAccount( | ||
new([aliceSigner.PublicKey, bobSigner.PublicKey], 1), | ||
[aliceSigner] | ||
); | ||
Console.WriteLine($"Alice-Bob MultiKey (1/2): {account.Address}"); | ||
|
||
Console.WriteLine("\n=== Funding accounts ===\n"); | ||
var aliceFundTxn = await aptos.Faucet.FundAccount(account.Address, 100_000_000); | ||
Console.WriteLine($"Alice-Bob MultiKey (1/2)'s fund transaction: {aliceFundTxn.Hash}"); | ||
|
||
Console.WriteLine("\n=== Building transaction ===\n"); | ||
|
||
var txn = await aptos.Transaction.Build( | ||
sender: account.Address, | ||
data: new GenerateEntryFunctionPayloadData( | ||
function: "0x1::coin::transfer", | ||
typeArguments: ["0x1::aptos_coin::AptosCoin"], | ||
functionArguments: [account.Address, "100000"] | ||
) | ||
); | ||
Console.WriteLine($"{JsonConvert.SerializeObject(txn.RawTransaction)}"); | ||
|
||
Console.WriteLine("\n=== Signing and submitting transaction ===\n"); | ||
|
||
var pendingTxn = await aptos.Transaction.SignAndSubmitTransaction(account, txn); | ||
Console.WriteLine($"Submitted transaction with hash: {pendingTxn.Hash}"); | ||
|
||
Console.WriteLine("Waiting for transaction..."); | ||
var committedTxn = await aptos.Transaction.WaitForTransaction(pendingTxn.Hash.ToString()); | ||
Console.WriteLine( | ||
$"Transaction {committedTxn.Hash} is {(committedTxn.Success ? "success" : "failure")}" | ||
); | ||
|
||
Console.WriteLine("\n=== Account Balance ===\n"); | ||
var balance = await aptos.Account.GetCoinBalance(account.Address, "0xa"); | ||
Console.WriteLine($"Account {account.Address} has {balance?.Amount ?? 0} coin balances"); | ||
} | ||
} |
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,45 @@ | ||
using Newtonsoft.Json; | ||
|
||
namespace Aptos.Examples; | ||
|
||
public class SimpleTransferSingleKeyExample | ||
{ | ||
public static async Task Run() | ||
{ | ||
Console.WriteLine("=== Addresses ===\n"); | ||
var aptos = new AptosClient(new AptosConfig(Networks.Devnet)); | ||
var account = SingleKeyAccount.Generate(); | ||
Console.WriteLine($"Alice: {account.Address}"); | ||
|
||
Console.WriteLine("\n=== Funding accounts ===\n"); | ||
var aliceFundTxn = await aptos.Faucet.FundAccount(account.Address, 100_000_000); | ||
Console.WriteLine($"Alice's fund transaction: {aliceFundTxn.Hash}"); | ||
|
||
Console.WriteLine("\n=== Building transaction ===\n"); | ||
|
||
var txn = await aptos.Transaction.Build( | ||
sender: account.Address, | ||
data: new GenerateEntryFunctionPayloadData( | ||
function: "0x1::coin::transfer", | ||
typeArguments: ["0x1::aptos_coin::AptosCoin"], | ||
functionArguments: [account.Address, "100000"] | ||
) | ||
); | ||
Console.WriteLine($"{JsonConvert.SerializeObject(txn.RawTransaction)}"); | ||
|
||
Console.WriteLine("\n=== Signing and submitting transaction ===\n"); | ||
|
||
var pendingTxn = await aptos.Transaction.SignAndSubmitTransaction(account, txn); | ||
Console.WriteLine($"Submitted transaction with hash: {pendingTxn.Hash}"); | ||
|
||
Console.WriteLine("Waiting for transaction..."); | ||
var committedTxn = await aptos.Transaction.WaitForTransaction(pendingTxn.Hash.ToString()); | ||
Console.WriteLine( | ||
$"Transaction {committedTxn.Hash} is {(committedTxn.Success ? "success" : "failure")}" | ||
); | ||
|
||
Console.WriteLine("\n=== Account Balance ===\n"); | ||
var balance = await aptos.Account.GetCoinBalance(account.Address, "0xa"); | ||
Console.WriteLine($"Account {account.Address} has {balance?.Amount ?? 0} coin balances"); | ||
} | ||
} |
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,81 @@ | ||
namespace Aptos.Tests.Core; | ||
|
||
using Xunit.Gherkin.Quick; | ||
|
||
[FeatureFile("../../../../features/single_key.feature")] | ||
public sealed class SingleKeyFeatureTests : Feature | ||
{ | ||
private string? _publicKeyType; | ||
|
||
private dynamic? _inputValue; | ||
|
||
private dynamic? _output; | ||
|
||
[Given(@"(ed25519|secp256k1|keyless) (.*)")] | ||
public void GivenValue(string type, string value) | ||
{ | ||
PublicKey publicKey = type switch | ||
{ | ||
"ed25519" => Ed25519PublicKey.Deserialize(new(value)), | ||
"secp256k1" => Secp256k1PublicKey.Deserialize(new(value)), | ||
"keyless" => KeylessPublicKey.Deserialize(new(value)), | ||
_ => throw new ArgumentException("Invalid public key type"), | ||
}; | ||
_publicKeyType = type; | ||
_inputValue = new SingleKey(publicKey); | ||
} | ||
|
||
[When(@"I serialize")] | ||
public void WhenISerialize() | ||
{ | ||
if (_inputValue == null) | ||
throw new ArgumentException("No input value"); | ||
if (_inputValue is SingleKey singleKey) | ||
_output = singleKey.BcsToHex(); | ||
} | ||
|
||
[When(@"I derive authentication key")] | ||
public void WhenIDeriveAuthenticationKey() | ||
{ | ||
if (_inputValue == null) | ||
throw new ArgumentException("No input value"); | ||
if (_inputValue is SingleKey singleKey) | ||
_output = singleKey.AuthKey().BcsToHex(); | ||
} | ||
|
||
[When(@"I verify signature (.*) with message (.*)")] | ||
public void WhenIVerifySignatureWithMessage(string signature, string message) | ||
{ | ||
if (_inputValue == null) | ||
throw new ArgumentException("No input value"); | ||
|
||
if (_inputValue is SingleKey singleKey) | ||
{ | ||
PublicKeySignature publicKeySignature = _publicKeyType switch | ||
{ | ||
"ed25519" => Ed25519Signature.Deserialize(new(signature)), | ||
"secp256k1" => Secp256k1Signature.Deserialize(new(signature)), | ||
"keyless" => KeylessSignature.Deserialize(new(signature)), | ||
_ => throw new ArgumentException("Invalid signature public key type"), | ||
}; | ||
_output = singleKey.VerifySignature(message, publicKeySignature); | ||
} | ||
} | ||
|
||
[Then(@"the result should be (.*) (.*)")] | ||
public void ThenTheResultShouldBeTypeValue(string type, string value) | ||
{ | ||
if (_output == null) | ||
throw new ArgumentException("No output value"); | ||
|
||
switch (type) | ||
{ | ||
case "bool": | ||
Assert.Equal(bool.Parse(value), _output); | ||
break; | ||
case "bcs": | ||
Assert.Equal(Hex.FromHexString(value), _output); | ||
break; | ||
} | ||
} | ||
} |
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
Oops, something went wrong.