Skip to content
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 package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@wharfkit/session",
"description": "Create account-based sessions, perform transactions, and allow users to login using Antelope-based blockchains.",
"version": "2.0.0-rc4",
"version": "2.0.0-rc5",
"homepage": "https://github.com/wharfkit/session",
"license": "BSD-3-Clause",
"main": "lib/session.js",
Expand Down
13 changes: 13 additions & 0 deletions src/kit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {BrowserLocalStorage, SessionStorage} from './storage'
import {
AbstractTransactPlugin,
BaseTransactPlugin,
BroadcastOptions,
TransactABIDef,
TransactPlugin,
TransactPluginsOptions,
Expand Down Expand Up @@ -86,6 +87,8 @@ export interface SessionKitOptions {
storage?: SessionStorage
transactPlugins?: TransactPlugin[]
transactPluginsOptions?: TransactPluginsOptions
awaitIrreversible?: boolean
broadcastOptions?: BroadcastOptions
accountCreationPlugins?: AccountCreationPlugin[]
sessionKey?: SessionKeyConfig
}
Expand All @@ -97,6 +100,8 @@ export class SessionKit {
readonly abis: TransactABIDef[] = []
readonly allowModify: boolean = true
readonly appName: string
readonly awaitIrreversible: boolean = false
readonly broadcastOptions?: BroadcastOptions
readonly expireSeconds: number = 120
readonly fetch: Fetch
readonly loginPlugins: AbstractLoginPlugin[]
Expand Down Expand Up @@ -153,6 +158,12 @@ export class SessionKit {
if (typeof options.allowModify !== 'undefined') {
this.allowModify = options.allowModify
}
if (options.awaitIrreversible !== undefined) {
this.awaitIrreversible = options.awaitIrreversible
}
if (options.broadcastOptions !== undefined) {
this.broadcastOptions = options.broadcastOptions
}
// Override default expireSeconds for all sessions if specified
if (options.expireSeconds) {
this.expireSeconds = options.expireSeconds
Expand Down Expand Up @@ -790,6 +801,8 @@ export class SessionKit {
storage: this.storage,
transactPlugins: options?.transactPlugins || this.transactPlugins,
transactPluginsOptions: options?.transactPluginsOptions || this.transactPluginsOptions,
awaitIrreversible: this.awaitIrreversible,
broadcastOptions: this.broadcastOptions,
ui: this.ui,
sessionKeyManager: this.sessionKeyManager,
onPersist: (session: Session) => this.persistSession(session),
Expand Down
37 changes: 35 additions & 2 deletions src/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import {ABICache, ABICacheInterface} from '@wharfkit/abicache'
import {
AbstractTransactPlugin,
BaseTransactPlugin,
BroadcastOptions,
TransactABIDef,
TransactArgs,
TransactContext,
Expand All @@ -44,6 +45,7 @@ import {
import {SessionStorage} from './storage'
import {
actionMatchesPermission,
buildSendTransaction2Options,
extractActions,
getFetch,
getPluginTranslations,
Expand Down Expand Up @@ -87,6 +89,8 @@ export interface SessionOptions {
transactPlugins?: AbstractTransactPlugin[]
transactPluginsOptions?: TransactPluginsOptions
ui?: UserInterface
awaitIrreversible?: boolean
broadcastOptions?: BroadcastOptions
sessionKeyManager?: SessionKeyManager
onPersist?: (session: Session) => Promise<void>
}
Expand All @@ -108,7 +112,9 @@ export class Session {
readonly abis: TransactABIDef[] = []
readonly abiCache: ABICacheInterface
readonly allowModify: boolean = true
readonly awaitIrreversible: boolean = false
readonly broadcast: boolean = true
readonly broadcastOptions?: BroadcastOptions
readonly chain: ChainDefinition
readonly expireSeconds: number = 120
readonly fetch: Fetch
Expand Down Expand Up @@ -201,6 +207,12 @@ export class Session {
if (options.broadcast !== undefined) {
this.broadcast = options.broadcast
}
if (options.awaitIrreversible !== undefined) {
this.awaitIrreversible = options.awaitIrreversible
}
if (options.broadcastOptions !== undefined) {
this.broadcastOptions = options.broadcastOptions
}
if (options.expireSeconds) {
this.expireSeconds = options.expireSeconds
}
Expand Down Expand Up @@ -448,6 +460,16 @@ export class Session {
? options.broadcast
: this.broadcast

const awaitIrreversible =
options && options.awaitIrreversible !== undefined
? options.awaitIrreversible
: this.awaitIrreversible

const broadcastOptions =
options && options.broadcastOptions !== undefined
? options.broadcastOptions
: this.broadcastOptions

// The abi provider to use for this transaction, falling back to the session instance
const abiCache = this.getMergedAbiCache(args, options)

Expand Down Expand Up @@ -596,8 +618,19 @@ export class Session {
signatures: result.signatures,
})

// Broadcast the SignedTransaction and save the API response to the TransactResult
result.response = await context.client.v1.chain.send_transaction(signed)
const tx2Options = buildSendTransaction2Options(awaitIrreversible, broadcastOptions)
try {
result.response = await context.client.v1.chain.send_transaction2(
signed,
tx2Options
)
} catch (error: any) {
if (error?.response?.status === 404) {
result.response = await context.client.v1.chain.send_transaction(signed)
} else {
throw error
}
}

// Find and process any return values from the transaction
if (result.response.processed && result.response.processed.action_traces) {
Expand Down
31 changes: 16 additions & 15 deletions src/sessionkey/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,24 +51,25 @@ export class SessionKeyLoginPlugin extends AbstractLoginPlugin {
})),
})

if (choice === 'update') {
await manager.updateLinks(session)
if (choice !== 'update') {
return
}
} else {
return
}
return
}

if (!manager.config.skipConsent && context.ui?.onSessionKeyConsent) {
const consent = await context.ui.onSessionKeyConsent({
appName: String(session.appName || 'this app'),
whitelist: manager.whitelist.map((e) => ({
contract: String(e.contract),
actions: e.actions?.map((a) => String(a)),
})),
})
} else {
if (!manager.config.skipConsent && context.ui?.onSessionKeyConsent) {
const consent = await context.ui.onSessionKeyConsent({
appName: String(session.appName || 'this app'),
whitelist: manager.whitelist.map((e) => ({
contract: String(e.contract),
actions: e.actions?.map((a) => String(a)),
})),
})

if (!consent) {
return
if (!consent) {
return
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions src/transact.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,22 @@ export interface TransactOptions {
* Optional parameter to control whether signatures returned from plugins are validated.
*/
validatePluginSignatures?: boolean
/**
* Wait for the transaction to become irreversible before returning.
* Uses send_transaction2 with retry enabled and no block limit (waits for LIB).
*/
awaitIrreversible?: boolean
/**
* Advanced options for send_transaction2. Provides fine-grained control over
* retry behavior and failure tracing.
*/
broadcastOptions?: BroadcastOptions
}

export interface BroadcastOptions {
returnFailureTrace?: boolean
retryTrx?: boolean
retryTrxNumBlocks?: number
}

export interface TransactABIDef {
Expand Down
27 changes: 26 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
} from '@wharfkit/antelope'
import type {Fetch, LocaleDefinitions} from '@wharfkit/common'
import {PlaceholderAuth, SigningRequest} from '@wharfkit/signing-request'
import {TransactArgs, TransactPlugin} from './transact'
import {BroadcastOptions, TransactArgs, TransactPlugin} from './transact'
import {WalletPlugin} from './wallet'

/**
Expand Down Expand Up @@ -215,3 +215,28 @@ export function rewriteAuthorizations(

return args
}

export interface SendTransaction2Options {
return_failure_trace?: boolean
retry_trx?: boolean
retry_trx_num_blocks?: number
}

export function buildSendTransaction2Options(
awaitIrreversible: boolean,
broadcastOptions?: BroadcastOptions
): SendTransaction2Options {
const options: SendTransaction2Options = {
return_failure_trace: broadcastOptions?.returnFailureTrace ?? true,
}
if (awaitIrreversible) {
options.retry_trx = true
options.retry_trx_num_blocks = undefined
} else if (broadcastOptions) {
options.retry_trx = broadcastOptions.retryTrx ?? true
if (broadcastOptions.retryTrxNumBlocks !== undefined) {
options.retry_trx_num_blocks = broadcastOptions.retryTrxNumBlocks
}
}
return options
}
19 changes: 0 additions & 19 deletions test/data/0672555e54e5f8adde3a189f52eed3ae86b4d3e5.json

This file was deleted.

Loading