Skip to content

Commit 976c058

Browse files
authored
adds cli command to view details for raw, unsigned transactions (#4931)
allows users to view details for a transaction without needing to use the REPL useful for situations where you have a serialized transaction, but don't know what's in it
1 parent bf03a96 commit 976c058

File tree

1 file changed

+125
-0
lines changed
  • ironfish-cli/src/commands/wallet/transaction

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4+
import {
5+
ErrorUtils,
6+
RawTransaction,
7+
RawTransactionSerde,
8+
RpcClient,
9+
UnsignedTransaction,
10+
} from '@ironfish/sdk'
11+
import { Flags } from '@oclif/core'
12+
import inquirer from 'inquirer'
13+
import { IronfishCommand } from '../../../command'
14+
import { RemoteFlags } from '../../../flags'
15+
import { longPrompt } from '../../../utils/longPrompt'
16+
import {
17+
renderRawTransactionDetails,
18+
renderUnsignedTransactionDetails,
19+
} from '../../../utils/transaction'
20+
21+
export class TransactionViewCommand extends IronfishCommand {
22+
static description = `View transaction details`
23+
24+
static flags = {
25+
...RemoteFlags,
26+
account: Flags.string({
27+
char: 'f',
28+
description: 'The name of the account to use to for viewing transaction details',
29+
}),
30+
transaction: Flags.string({
31+
char: 't',
32+
description: 'The hex-encoded raw transaction or unsigned transaction to view',
33+
}),
34+
}
35+
36+
async start(): Promise<void> {
37+
const { flags } = await this.parse(TransactionViewCommand)
38+
39+
const client = await this.sdk.connectRpc()
40+
41+
const account = flags.account ?? (await this.selectAccount(client))
42+
43+
let transactionString = flags.transaction as string
44+
if (!transactionString) {
45+
transactionString = await longPrompt(
46+
'Enter the hex-encoded raw transaction or unsigned transaction to view',
47+
{
48+
required: true,
49+
},
50+
)
51+
}
52+
53+
const rawTransaction = this.tryDeserializeRawTransaction(transactionString)
54+
if (rawTransaction) {
55+
return await renderRawTransactionDetails(client, rawTransaction, account, this.logger)
56+
}
57+
58+
const unsignedTransaction = this.tryDeserializeUnsignedTransaction(transactionString)
59+
if (unsignedTransaction) {
60+
return await renderUnsignedTransactionDetails(
61+
client,
62+
unsignedTransaction,
63+
account,
64+
this.logger,
65+
)
66+
}
67+
68+
this.error(
69+
'Unable to deserialize transaction input as a raw transacton or an unsigned transaction',
70+
)
71+
}
72+
73+
async selectAccount(client: Pick<RpcClient, 'wallet'>): Promise<string> {
74+
const accountsResponse = await client.wallet.getAccounts()
75+
76+
const choices = []
77+
for (const account of accountsResponse.content.accounts) {
78+
choices.push({
79+
account,
80+
value: account,
81+
})
82+
}
83+
84+
choices.sort()
85+
86+
const selection = await inquirer.prompt<{
87+
account: string
88+
}>([
89+
{
90+
name: 'account',
91+
message: 'Select account',
92+
type: 'list',
93+
choices,
94+
},
95+
])
96+
97+
return selection.account
98+
}
99+
100+
tryDeserializeRawTransaction(transaction: string): RawTransaction | undefined {
101+
try {
102+
return RawTransactionSerde.deserialize(Buffer.from(transaction, 'hex'))
103+
} catch (e) {
104+
this.logger.debug(
105+
`Failed to deserialize transaction as RawTransaction: ${ErrorUtils.renderError(e)}`,
106+
)
107+
108+
return undefined
109+
}
110+
}
111+
112+
tryDeserializeUnsignedTransaction(transaction: string): UnsignedTransaction | undefined {
113+
try {
114+
return new UnsignedTransaction(Buffer.from(transaction, 'hex'))
115+
} catch (e) {
116+
this.logger.debug(
117+
`Failed to deserialize transaction as UnsignedTransaction: ${ErrorUtils.renderError(
118+
e,
119+
)}`,
120+
)
121+
122+
return undefined
123+
}
124+
}
125+
}

0 commit comments

Comments
 (0)