import { Address } from '@solana/addresses'; import { Instruction } from '@solana/instructions'; import { Brand } from '@solana/nominal-types'; import { AdvanceNonceAccountInstruction } from './durable-nonce-instruction'; import { ExcludeTransactionMessageLifetime } from './lifetime'; import { TransactionMessage } from './transaction-message'; import { ExcludeTransactionMessageWithinSizeLimit } from './transaction-message-size'; type DurableNonceConfig = Readonly<{ readonly nonce: Nonce; readonly nonceAccountAddress: Address; readonly nonceAuthorityAddress: Address; }>; /** Represents a string that is particularly known to be the base58-encoded value of a nonce. */ export type Nonce = Brand; /** * A constraint which, when applied to a transaction message, makes that transaction message * eligible to land on the network. * * The transaction message will continue to be eligible to land until the network considers the * `nonce` to have advanced. This can happen when the nonce account in which this nonce is found is * destroyed, or the nonce value within changes. */ export type NonceLifetimeConstraint = Readonly<{ /** * A value contained in the related nonce account at the time the transaction was prepared. * * The transaction will be considered eligible to land until the nonce account ceases to exist * or contain this value. */ nonce: Nonce; }>; /** * Represents a transaction message whose lifetime is defined by the value of a nonce it includes. * * Such a transaction can only be landed on the network if the nonce is known to the network and has * not already been used to land a different transaction. */ export interface TransactionMessageWithDurableNonceLifetime { readonly instructions: readonly [ AdvanceNonceAccountInstruction, ...Instruction[] ]; readonly lifetimeConstraint: NonceLifetimeConstraint; } /** * A helper type to exclude the durable nonce lifetime constraint from a transaction message. */ export type ExcludeTransactionMessageDurableNonceLifetime = TTransactionMessage extends TransactionMessageWithDurableNonceLifetime ? ExcludeTransactionMessageLifetime : TTransactionMessage; /** * A type guard that returns `true` if the transaction message conforms to the * {@link TransactionMessageWithDurableNonceLifetime} type, and refines its type for use in your * program. * * @example * ```ts * import { isTransactionMessageWithDurableNonceLifetime } from '@solana/transaction-messages'; * import { fetchNonce } from "@solana-program/system"; * * if (isTransactionMessageWithDurableNonceLifetime(message)) { * // At this point, `message` has been refined to a * // `TransactionMessageWithDurableNonceLifetime`. * const { nonce, nonceAccountAddress } = message.lifetimeConstraint; * const { data: { blockhash: actualNonce } } = await fetchNonce(nonceAccountAddress); * setNonceIsValid(nonce === actualNonce); * } else { * setError( * `${getSignatureFromTransaction(transaction)} does not have a nonce-based lifetime`, * ); * } * ``` */ export declare function isTransactionMessageWithDurableNonceLifetime(transactionMessage: TransactionMessage | (TransactionMessage & TransactionMessageWithDurableNonceLifetime)): transactionMessage is TransactionMessage & TransactionMessageWithDurableNonceLifetime; /** * From time to time you might acquire a transaction message, that you expect to have a * nonce-based lifetime, from an untrusted network API or user input. Use this function to assert * that such a transaction message actually has a nonce-based lifetime. * * @example * ```ts * import { assertIsTransactionMessageWithDurableNonceLifetime } from '@solana/transaction-messages'; * * try { * // If this type assertion function doesn't throw, then * // Typescript will upcast `message` to `TransactionMessageWithDurableNonceLifetime`. * assertIsTransactionMessageWithDurableNonceLifetime(message); * // At this point, `message` is a `TransactionMessageWithDurableNonceLifetime` that can be used * // with the RPC. * const { nonce, nonceAccountAddress } = message.lifetimeConstraint; * const { data: { blockhash: actualNonce } } = await fetchNonce(nonceAccountAddress); * } catch (e) { * // `message` turned out not to have a nonce-based lifetime * } * ``` */ export declare function assertIsTransactionMessageWithDurableNonceLifetime(transactionMessage: TransactionMessage | (TransactionMessage & TransactionMessageWithDurableNonceLifetime)): asserts transactionMessage is TransactionMessage & TransactionMessageWithDurableNonceLifetime; /** * Given a nonce, the account where the value of the nonce is stored, and the address of the account * authorized to consume that nonce, this method will return a new transaction having the same type * as the one supplied plus the {@link TransactionMessageWithDurableNonceLifetime} type. * * In particular, this method _prepends_ an instruction to the transaction message designed to * consume (or 'advance') the nonce in the same transaction whose lifetime is defined by it. * * @param config * * @example * ```ts * import { Nonce, setTransactionMessageLifetimeUsingDurableNonce } from '@solana/transaction-messages'; * import { fetchNonce } from '@solana-program/system'; * * const nonceAccountAddress = address('EGtMh4yvXswwHhwVhyPxGrVV2TkLTgUqGodbATEPvojZ'); * const nonceAuthorityAddress = address('4KD1Rdrd89NG7XbzW3xsX9Aqnx2EExJvExiNme6g9iAT'); * * const { * data: { blockhash }, * } = await fetchNonce(rpc, nonceAccountAddress); * const nonce = blockhash as string as Nonce; * * const durableNonceTransactionMessage = setTransactionMessageLifetimeUsingDurableNonce( * { nonce, nonceAccountAddress, nonceAuthorityAddress }, * tx, * ); * ``` */ export declare function setTransactionMessageLifetimeUsingDurableNonce({ nonce, nonceAccountAddress, nonceAuthorityAddress, }: DurableNonceConfig, transactionMessage: TTransactionMessage): SetTransactionMessageWithDurableNonceLifetime; /** * Helper type that transforms a given transaction message type into a new one that has the * `AdvanceNonceAccount` instruction as the first instruction and a lifetime constraint * representing the nonce value. */ type SetTransactionMessageWithDurableNonceLifetime = TTransactionMessage extends unknown ? Omit, 'instructions' | 'lifetimeConstraint'> & { readonly instructions: TTransactionMessage['instructions'] extends readonly [ AdvanceNonceAccountInstruction, ...infer TTail extends readonly Instruction[] ] ? readonly [AdvanceNonceAccountInstruction, ...TTail] : readonly [ AdvanceNonceAccountInstruction, ...TTransactionMessage['instructions'] ]; readonly lifetimeConstraint: NonceLifetimeConstraint; } : never; export {}; //# sourceMappingURL=durable-nonce.d.ts.map