Skip to content
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

fix: package exports #18

Merged
merged 2 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/algorand.transaction.acfg.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {AlgorandEncoder} from "./algorand.encoder.js"
import {AssetParams} from "./algorand.asset.params.js";
import {ITransactionHeaderBuilder, TransactionHeader} from "./algorand.transaction.js";
import {ITransactionHeaderBuilder, TransactionHeader} from "./algorand.transaction.header.js";

/**
* This is used to create, configure and destroy an asset depending on which fields are set.
Expand Down
2 changes: 1 addition & 1 deletion lib/algorand.transaction.afrz.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {AlgorandEncoder} from "./algorand.encoder.js"
import {ITransactionHeaderBuilder, TransactionHeader} from "./algorand.transaction.js";
import {ITransactionHeaderBuilder, TransactionHeader} from "./algorand.transaction.header.js";

/**
* Includes all fields in {@link TransactionHeader} and "type" is "axfer".
Expand Down
2 changes: 1 addition & 1 deletion lib/algorand.transaction.axfer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {AlgorandEncoder} from "./algorand.encoder.js"
import {TransactionHeader, type ITransactionHeaderBuilder} from "./algorand.transaction.js";
import {ITransactionHeaderBuilder, TransactionHeader} from "./algorand.transaction.header.js";

/**
* Asset Transfer Transaction
Expand Down
28 changes: 14 additions & 14 deletions lib/algorand.transaction.crafter.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {AssetConfigTransaction} from "./algorand.transaction.acfg";
import {AssetParamsBuilder} from "./algorand.asset.params";
import {AssetTransferTransaction} from "./algorand.transaction.axfer";
import {AssetFreezeTransaction} from "./algorand.transaction.afrz";
import {ITransactionHeaderBuilder, TransactionHeader} from "./algorand.transaction";
import {ITransactionHeaderBuilder, TransactionHeader} from "./algorand.transaction.header";


// Setup Validator
Expand Down Expand Up @@ -111,14 +111,14 @@ describe("Algorand Transaction Crafter", () => {
const from: string = algoEncoder.encodeAddress(Buffer.from(transactionHeader.snd))
// to algorand address
const to: string = algoEncoder.encodeAddress(Buffer.from(randomBytes(32)))

// create pay transaction
const txn: PayTransaction = withTestTransactionHeader(
algorandCrafter
.pay(1000, from, to)
.addCloseTo(from)
).get()

expect(txn).toBeDefined()
expect(txn).toBeInstanceOf(PayTransaction)
expect(txn).toEqual({
Expand All @@ -131,7 +131,7 @@ describe("Algorand Transaction Crafter", () => {

const validate = ajv.compile(paySchema)
expect(validate(txn)).toBe(true)
})
})
})

describe("KeyReg Online Transactions", () => {
Expand All @@ -140,22 +140,22 @@ describe("Algorand Transaction Crafter", () => {
it("(OK) Craft Keyreg change-online transaction", async () => {
// from algorand address
const from: string = algoEncoder.encodeAddress(Buffer.from(transactionHeader.snd))

// vote key
const voteKey: string = Buffer.from(randomBytes(32)).toString("base64")

// selection key
const selectionKey: string = Buffer.from(randomBytes(32)).toString("base64")

// state proof key
const stateProofKey: string = Buffer.from(randomBytes(64)).toString("base64")

// create keyreg transaction
const txn: KeyregTransaction = withTestTransactionHeader(
algorandCrafter
.changeOnline(from, voteKey, selectionKey, stateProofKey, 1000, 2000, 32)
).get()

expect(txn).toBeDefined()
expect(txn).toBeInstanceOf(KeyregTransaction)

Expand All @@ -181,12 +181,12 @@ describe("Algorand Transaction Crafter", () => {
it("(OK) Craft Keyreg change-offline transaction", async () => {
// from algorand address
const from: string = algoEncoder.encodeAddress(Buffer.from(transactionHeader.snd))

// create keyreg transaction
const txn: KeyregTransaction = withTestTransactionHeader(algorandCrafter
.changeOffline(from)
).get()

expect(txn).toBeDefined()
expect(txn).toBeInstanceOf(KeyregTransaction)
expect(txn).toEqual({
Expand All @@ -208,16 +208,16 @@ describe("Algorand Transaction Crafter", () => {
it("(OK) Craft Keyreg non-participation transaction", async () => {
// from algorand address
const from: string = algoEncoder.encodeAddress(Buffer.from(transactionHeader.snd))

// note
const note: string = Buffer.from(randomBytes(32)).toString("base64")

// create keyreg transaction
const txn: KeyregTransaction = withTestTransactionHeader(
algorandCrafter
.markNonParticipation(from)
).get()

expect(txn).toBeDefined()
expect(txn).toBeInstanceOf(KeyregTransaction)
expect(txn).toEqual({
Expand Down
156 changes: 156 additions & 0 deletions lib/algorand.transaction.header.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**
* Specifies the type of transaction. This value is automatically generated using any of the developer tools.
* @category Common
*/
export type TransactionType = "pay" | "keyreg" | "acfg" | "axfer" | "afrz" | "appl" | "stpf"

/**
*
* Transaction Header
*
* Shared keys for all transactions
*
* @category Common
*/
export abstract class TransactionHeader {
/**
* Transaction Type
*
* Specifies the type of transaction. This value is automatically generated using any of the developer tools.
*/
type: TransactionType
/**
* Sender
*
* The address of the account that pays the fee and amount.
*/
snd: Uint8Array
/**
* Fee
*
* Paid by the sender to the FeeSink to prevent denial-of-service. The minimum fee on Algorand is currently 1000 microAlgos.
*/
fee: number
/**
* First Valid
*
* The first round for when the transaction is valid. If the transaction is sent prior to this round it will be rejected by the network.
*/
fv: number
/**
* Last Valid
*
* The ending round for which the transaction is valid. After this round, the transaction will be rejected by the network.
*/
lv: number
/**
* Genesis Hash
*
* The hash of the genesis block of the network for which the transaction is valid. See the genesis hash for MainNet, TestNet, and BetaNet.
*/
gh: Uint8Array
/**
* Genesis ID
*
* The human-readable string that identifies the network for the transaction.
* The genesis ID is found in the genesis block. See the genesis ID for MainNet, TestNet, and BetaNet.
*/
gen?: string
/**
* Note
*
* Any data up to 1000 bytes.
*/
note?: Uint8Array
/**
* Rekey To
*
* Specifies the authorized address. This address will be used to authorize all future transactions.
*/
rekey?: Uint8Array
/**
* Lease
*
* A lease enforces mutual exclusion of transactions. If this field is nonzero, then once the transaction is confirmed,
* it acquires the lease identified by the (Sender, Lease) pair of the transaction until the LastValid round passes.
* While this transaction possesses the lease, no other transaction specifying this lease can be confirmed.
* A lease is often used in the context of Algorand Smart Contracts to prevent replay attacks.
* Read more about Algorand Smart Contracts. Leases can also be used to safeguard against unintended duplicate spends.
* For example, if I send a transaction to the network and later realize my fee was too low,
* I could send another transaction with a higher fee, but the same lease value.
* This would ensure that only one of those transactions ends up getting confirmed during the validity period.
*/
lx?: Uint8Array
/**
* Group
*
* The group specifies that the transaction is part of a group and, if so, specifies the hash of the transaction group.
* Assign a group ID to a transaction through the workflow described in the Atomic Transfers Guide.
*/
grp?: Uint8Array
}

/**
* Interface for Transaction Header Builders
* @category Builders
* @internal
*/
export interface ITransactionHeaderBuilder<T> {
/**
* Add Sender
*
* @param sender The address of the account that pays the fee and amount.
*/
addSender(sender: string): T

/**
* Add Fee
*
* @param fee Paid by the sender to the FeeSink to prevent denial-of-service. The minimum fee on Algorand is currently 1000 microAlgos.
*/
addFee(fee: number): T

/**
* Add First Valid Round
*
* @param fv The first round for when the transaction is valid. If the transaction is sent prior to this round it will be rejected by the network.
*/
addFirstValidRound(fv: number): T

/**
* Add Last Valid Round
*
* @param lv The ending round for which the transaction is valid. After this round, the transaction will be rejected by the network.
*/
addLastValidRound(lv: number): T

/**
* Add Note
*
* @param note Any data up to 1000 bytes.
* @param encoding
*/
addNote(note: string, encoding?: BufferEncoding): T

/**
* Add Rekey Address
*
* @param rekey Specifies the authorized address. This address will be used to authorize all future transactions.
*/
addRekey(rekey: string): T

/**
* Add Lease
*
* @param lx A lease enforces mutual exclusion of transactions. If this field is nonzero, then once the transaction
* is confirmed, it acquires the lease identified by the (Sender, Lease) pair of the transaction until the LastValid round passes.
*/
addLease(lx: Uint8Array): T

/**
* Add Group
*
* @param grp The group specifies that the transaction is part of a group and, if so, specifies the hash of the transaction group.
*/
addGroup(grp: Uint8Array): T
}
3 changes: 2 additions & 1 deletion lib/algorand.transaction.keyreg.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {AlgorandEncoder} from "./algorand.encoder.js"
import {ITransactionHeaderBuilder, TransactionHeader} from "./algorand.transaction.js";
import {ITransactionHeaderBuilder, TransactionHeader} from "./algorand.transaction.header.js";

/**
* @category Transactions
* @see {@link AlgorandTransactionCrafter}
Expand Down
2 changes: 1 addition & 1 deletion lib/algorand.transaction.pay.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {AlgorandEncoder} from "./algorand.encoder.js"
import {ITransactionHeaderBuilder, TransactionHeader} from "./algorand.transaction.js";
import {ITransactionHeaderBuilder, TransactionHeader} from "./algorand.transaction.header.js";

/**
* @category Transactions
Expand Down
Loading