-
Notifications
You must be signed in to change notification settings - Fork 3.7k
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
[TS SDK V2] Port over account and transaction authenticator, signed transaction type #10367
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
84 changes: 84 additions & 0 deletions
84
ecosystem/typescript/sdk_v2/src/transactions/authenticator/account.ts
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,84 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
|
||
import { Serializer, Deserializer, Serializable } from "../../bcs"; | ||
import { Ed25519PublicKey, Ed25519Signature } from "../../crypto/ed25519"; | ||
import { MultiEd25519PublicKey, MultiEd25519Signature } from "../../crypto/multi_ed25519"; | ||
import { RustEnumAccountAuthenticatorVariant } from "../../types"; | ||
|
||
export abstract class AccountAuthenticator extends Serializable { | ||
abstract serialize(serializer: Serializer): void; | ||
|
||
static deserialize(deserializer: Deserializer): AccountAuthenticator { | ||
const index = deserializer.deserializeUleb128AsU32(); | ||
switch (index) { | ||
case RustEnumAccountAuthenticatorVariant.AccountAuthenticatorEd25519: | ||
return AccountAuthenticatorEd25519.load(deserializer); | ||
case RustEnumAccountAuthenticatorVariant.AccountAuthenticatorMultiEd25519: | ||
return AccountAuthenticatorMultiEd25519.load(deserializer); | ||
default: | ||
throw new Error(`Unknown variant index for AccountAuthenticator: ${index}`); | ||
} | ||
} | ||
} | ||
|
||
export class AccountAuthenticatorEd25519 extends AccountAuthenticator { | ||
public readonly public_key: Ed25519PublicKey; | ||
|
||
public readonly signature: Ed25519Signature; | ||
|
||
constructor(public_key: Ed25519PublicKey, signature: Ed25519Signature) { | ||
super(); | ||
this.public_key = public_key; | ||
this.signature = signature; | ||
} | ||
|
||
/** | ||
* Transaction authenticator Ed25519 for a multi signers transaction | ||
* | ||
* @param public_key Account's Ed25519 public key. | ||
* @param signature Account's Ed25519 signature | ||
* | ||
*/ | ||
serialize(serializer: Serializer): void { | ||
serializer.serializeU32AsUleb128(RustEnumAccountAuthenticatorVariant.AccountAuthenticatorEd25519); | ||
this.public_key.serialize(serializer); | ||
this.signature.serialize(serializer); | ||
} | ||
|
||
static load(deserializer: Deserializer): AccountAuthenticatorEd25519 { | ||
const public_key = Ed25519PublicKey.deserialize(deserializer); | ||
const signature = Ed25519Signature.deserialize(deserializer); | ||
return new AccountAuthenticatorEd25519(public_key, signature); | ||
} | ||
} | ||
|
||
export class AccountAuthenticatorMultiEd25519 extends AccountAuthenticator { | ||
public readonly public_key: MultiEd25519PublicKey; | ||
|
||
public readonly signature: MultiEd25519Signature; | ||
|
||
/** | ||
* Transaction authenticator Multi Ed25519 for a multi signers transaction | ||
* | ||
* @param public_key Account's MultiEd25519 public key. | ||
* @param signature Account's MultiEd25519 signature | ||
* | ||
*/ | ||
constructor(public_key: MultiEd25519PublicKey, signature: MultiEd25519Signature) { | ||
super(); | ||
this.public_key = public_key; | ||
this.signature = signature; | ||
} | ||
|
||
serialize(serializer: Serializer): void { | ||
serializer.serializeU32AsUleb128(RustEnumAccountAuthenticatorVariant.AccountAuthenticatorMultiEd25519); | ||
this.public_key.serialize(serializer); | ||
this.signature.serialize(serializer); | ||
} | ||
|
||
static load(deserializer: Deserializer): AccountAuthenticatorMultiEd25519 { | ||
const public_key = MultiEd25519PublicKey.deserialize(deserializer); | ||
const signature = MultiEd25519Signature.deserialize(deserializer); | ||
return new AccountAuthenticatorMultiEd25519(public_key, signature); | ||
} | ||
} |
183 changes: 183 additions & 0 deletions
183
ecosystem/typescript/sdk_v2/src/transactions/authenticator/transaction.ts
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,183 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
|
||
import { Deserializer, Serializer } from "../../bcs"; | ||
import { AccountAddress } from "../../core"; | ||
import { Ed25519PublicKey, Ed25519Signature } from "../../crypto/ed25519"; | ||
import { MultiEd25519PublicKey, MultiEd25519Signature } from "../../crypto/multi_ed25519"; | ||
import { RustEnumTransactionAuthenticatorVariant } from "../../types"; | ||
import { AccountAuthenticator } from "./account"; | ||
|
||
export abstract class TransactionAuthenticator { | ||
abstract serialize(serializer: Serializer): void; | ||
|
||
static deserialize(deserializer: Deserializer): TransactionAuthenticator { | ||
const index = deserializer.deserializeUleb128AsU32(); | ||
switch (index) { | ||
case RustEnumTransactionAuthenticatorVariant.TransactionAuthenticatorEd25519: | ||
return TransactionAuthenticatorEd25519.load(deserializer); | ||
case RustEnumTransactionAuthenticatorVariant.TransactionAuthenticatorMultiEd25519: | ||
return TransactionAuthenticatorMultiEd25519.load(deserializer); | ||
case RustEnumTransactionAuthenticatorVariant.TransactionAuthenticatorMultiAgent: | ||
return TransactionAuthenticatorMultiAgent.load(deserializer); | ||
case RustEnumTransactionAuthenticatorVariant.TransactionAuthenticatorFeePayer: | ||
return TransactionAuthenticatorFeePayer.load(deserializer); | ||
default: | ||
throw new Error(`Unknown variant index for TransactionAuthenticator: ${index}`); | ||
} | ||
} | ||
} | ||
|
||
export class TransactionAuthenticatorEd25519 extends TransactionAuthenticator { | ||
public readonly public_key: Ed25519PublicKey; | ||
|
||
public readonly signature: Ed25519Signature; | ||
|
||
/** | ||
* Transaction authenticator Ed25519 for a single signer transaction | ||
* | ||
* @param public_key Client's public key. | ||
* @param signature Ed25519 signature of a raw transaction. | ||
* @see {@link https://aptos.dev/integration/creating-a-signed-transaction | Creating a Signed Transaction} | ||
* for details about generating a signature. | ||
*/ | ||
constructor(public_key: Ed25519PublicKey, signature: Ed25519Signature) { | ||
super(); | ||
this.public_key = public_key; | ||
this.signature = signature; | ||
} | ||
|
||
serialize(serializer: Serializer): void { | ||
serializer.serializeU32AsUleb128(RustEnumTransactionAuthenticatorVariant.TransactionAuthenticatorEd25519); | ||
this.public_key.serialize(serializer); | ||
this.signature.serialize(serializer); | ||
} | ||
|
||
static load(deserializer: Deserializer): TransactionAuthenticatorEd25519 { | ||
const public_key = Ed25519PublicKey.deserialize(deserializer); | ||
const signature = Ed25519Signature.deserialize(deserializer); | ||
return new TransactionAuthenticatorEd25519(public_key, signature); | ||
} | ||
} | ||
|
||
export class TransactionAuthenticatorMultiEd25519 extends TransactionAuthenticator { | ||
public readonly public_key: MultiEd25519PublicKey; | ||
|
||
public readonly signature: MultiEd25519Signature; | ||
|
||
/** | ||
* Transaction authenticator Ed25519 for a multi signers transaction | ||
* | ||
* @param public_key Client's public key. | ||
* @param signature Multi Ed25519 signature of a raw transaction. | ||
* | ||
*/ | ||
constructor(public_key: MultiEd25519PublicKey, signature: MultiEd25519Signature) { | ||
super(); | ||
this.public_key = public_key; | ||
this.signature = signature; | ||
} | ||
|
||
serialize(serializer: Serializer): void { | ||
serializer.serializeU32AsUleb128(RustEnumTransactionAuthenticatorVariant.TransactionAuthenticatorMultiEd25519); | ||
this.public_key.serialize(serializer); | ||
this.signature.serialize(serializer); | ||
} | ||
|
||
static load(deserializer: Deserializer): TransactionAuthenticatorMultiEd25519 { | ||
const public_key = MultiEd25519PublicKey.deserialize(deserializer); | ||
const signature = MultiEd25519Signature.deserialize(deserializer); | ||
return new TransactionAuthenticatorMultiEd25519(public_key, signature); | ||
} | ||
} | ||
|
||
export class TransactionAuthenticatorMultiAgent extends TransactionAuthenticator { | ||
public readonly sender: AccountAuthenticator; | ||
|
||
public readonly secondary_signer_addresses: Array<AccountAddress>; | ||
|
||
public readonly secondary_signers: Array<AccountAuthenticator>; | ||
|
||
/** | ||
* Transaction authenticator for a multi agent transaction | ||
* | ||
* @param sender Sender account authenticator | ||
* @param secondary_signer_addresses Secondary signers address | ||
* @param secondary_signers Secondary signers account authenticators | ||
* | ||
*/ | ||
constructor( | ||
sender: AccountAuthenticator, | ||
secondary_signer_addresses: Array<AccountAddress>, | ||
secondary_signers: Array<AccountAuthenticator>, | ||
) { | ||
super(); | ||
this.sender = sender; | ||
this.secondary_signer_addresses = secondary_signer_addresses; | ||
this.secondary_signers = secondary_signers; | ||
} | ||
|
||
serialize(serializer: Serializer): void { | ||
serializer.serializeU32AsUleb128(RustEnumTransactionAuthenticatorVariant.TransactionAuthenticatorMultiAgent); | ||
this.sender.serialize(serializer); | ||
serializer.serializeVector<AccountAddress>(this.secondary_signer_addresses); | ||
serializer.serializeVector<AccountAuthenticator>(this.secondary_signers); | ||
} | ||
|
||
static load(deserializer: Deserializer): TransactionAuthenticatorMultiAgent { | ||
const sender = AccountAuthenticator.deserialize(deserializer); | ||
const secondary_signer_addresses = deserializer.deserializeVector(AccountAddress); | ||
const secondary_signers = deserializer.deserializeVector(AccountAuthenticator); | ||
return new TransactionAuthenticatorMultiAgent(sender, secondary_signer_addresses, secondary_signers); | ||
} | ||
} | ||
|
||
export class TransactionAuthenticatorFeePayer extends TransactionAuthenticator { | ||
public readonly sender: AccountAuthenticator; | ||
|
||
public readonly secondary_signer_addresses: Array<AccountAddress>; | ||
|
||
public readonly secondary_signers: Array<AccountAuthenticator>; | ||
|
||
public readonly fee_payer: { address: AccountAddress; authenticator: AccountAuthenticator }; | ||
|
||
/** | ||
* Transaction authenticator for a fee payer transaction | ||
* | ||
* @param sender Sender account authenticator | ||
* @param secondary_signer_addresses Secondary signers address | ||
* @param secondary_signers Secondary signers account authenticators | ||
* @param fee_payer Object of the fee payer account address and the fee payer authentication | ||
* | ||
*/ | ||
constructor( | ||
sender: AccountAuthenticator, | ||
secondary_signer_addresses: Array<AccountAddress>, | ||
secondary_signers: Array<AccountAuthenticator>, | ||
fee_payer: { address: AccountAddress; authenticator: AccountAuthenticator }, | ||
) { | ||
super(); | ||
this.sender = sender; | ||
this.secondary_signer_addresses = secondary_signer_addresses; | ||
this.secondary_signers = secondary_signers; | ||
this.fee_payer = fee_payer; | ||
} | ||
|
||
serialize(serializer: Serializer): void { | ||
serializer.serializeU32AsUleb128(RustEnumTransactionAuthenticatorVariant.TransactionAuthenticatorFeePayer); | ||
this.sender.serialize(serializer); | ||
serializer.serializeVector<AccountAddress>(this.secondary_signer_addresses); | ||
serializer.serializeVector<AccountAuthenticator>(this.secondary_signers); | ||
this.fee_payer.address.serialize(serializer); | ||
this.fee_payer.authenticator.serialize(serializer); | ||
} | ||
|
||
static load(deserializer: Deserializer): TransactionAuthenticatorMultiAgent { | ||
const sender = AccountAuthenticator.deserialize(deserializer); | ||
const secondary_signer_addresses = deserializer.deserializeVector(AccountAddress); | ||
const secondary_signers = deserializer.deserializeVector(AccountAuthenticator); | ||
const address = AccountAddress.deserialize(deserializer); | ||
const authenticator = AccountAuthenticator.deserialize(deserializer); | ||
const fee_payer = { address, authenticator }; | ||
return new TransactionAuthenticatorFeePayer(sender, secondary_signer_addresses, secondary_signers, fee_payer); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
ecosystem/typescript/sdk_v2/src/transactions/types/signedTransaction.ts
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,38 @@ | ||
/* eslint-disable @typescript-eslint/naming-convention */ | ||
|
||
import { Serializer, Deserializer } from "../../bcs"; | ||
import { TransactionAuthenticator } from "../authenticator/transaction"; | ||
import { RawTransaction } from "./rawTransaction"; | ||
|
||
export class SignedTransaction { | ||
public readonly raw_txn: RawTransaction; | ||
|
||
public readonly authenticator: TransactionAuthenticator; | ||
|
||
/** | ||
* A SignedTransaction consists of a raw transaction and an authenticator. The authenticator | ||
* contains a client's public key and the signature of the raw transaction. | ||
* | ||
* @see {@link https://aptos.dev/integration/creating-a-signed-transaction | Creating a Signed Transaction} | ||
* | ||
* @param raw_txn | ||
* @param authenticator Contains a client's public key and the signature of the raw transaction. | ||
* Authenticator has 3 flavors: single signature, multi-signature and multi-agent. | ||
* @see {@link https://github.com/aptos-labs/aptos-core/blob/main/types/src/transaction/authenticator.rs} for details. | ||
*/ | ||
constructor(raw_txn: RawTransaction, authenticator: TransactionAuthenticator) { | ||
this.raw_txn = raw_txn; | ||
this.authenticator = authenticator; | ||
} | ||
|
||
serialize(serializer: Serializer): void { | ||
this.raw_txn.serialize(serializer); | ||
this.authenticator.serialize(serializer); | ||
} | ||
|
||
static deserialize(deserializer: Deserializer): SignedTransaction { | ||
const raw_txn = RawTransaction.deserialize(deserializer); | ||
const authenticator = TransactionAuthenticator.deserialize(deserializer); | ||
return new SignedTransaction(raw_txn, authenticator); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -42,6 +42,26 @@ export enum RustEnumTransactionVariants { | |
FeePayerTransaction = 1, | ||
} | ||
|
||
/** | ||
* Transaction Authenticator enum as they are represented in Rust | ||
* {@link https://github.com/aptos-labs/aptos-core/blob/main/types/src/transaction/authenticator.rs#L44} | ||
*/ | ||
export enum RustEnumTransactionAuthenticatorVariant { | ||
TransactionAuthenticatorEd25519 = 0, | ||
TransactionAuthenticatorMultiEd25519 = 1, | ||
TransactionAuthenticatorMultiAgent = 2, | ||
TransactionAuthenticatorFeePayer = 4, | ||
} | ||
|
||
/** | ||
* Transaction Authenticator enum as they are represented in Rust | ||
* {@link https://github.com/aptos-labs/aptos-core/blob/main/types/src/transaction/authenticator.rs#L414} | ||
*/ | ||
export enum RustEnumAccountAuthenticatorVariant { | ||
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. Similarly |
||
AccountAuthenticatorEd25519 = 0, | ||
AccountAuthenticatorMultiEd25519 = 1, | ||
} | ||
|
||
/** | ||
* BCS types | ||
*/ | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Those should be pretty easy to name simply. Doesn't have to be that descriptive that it refers to rust.
TransactionAuthenticatorType
Then just
Ed25519
MultiEd25519
MultiAgent
FeePayer
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.
I agree, though most of the time people look at it and ask "what are these numbers? where they come from?" it helped me a lot to figure out that these are fixed numbers that come from our rust code, but also the comment on each enum should help with that.
kk, let me change these names in a another PR
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.
yeah I feel that but I think we can just document that it comes from Rust, at some point we should be able to say "Read the documentation" rather than have to explain these things