Skip to content

Commit 37315e0

Browse files
authored
Pass in transaction version from SDK (#52)
1 parent ed5aa96 commit 37315e0

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed

packages/ironfish-native-module/ios/IronfishNativeModule.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,13 @@ public class IronfishNativeModule: Module {
157157
}
158158
}
159159

160-
AsyncFunction("createTransaction") { (spendComponents: SpendComponentsInput, outputs: OutputsInput, spendingKey: Data) throws -> Data in
160+
AsyncFunction("createTransaction") { (transactionVersion: UInt8, spendComponents: SpendComponentsInput, outputs: OutputsInput, spendingKey: Data) throws -> Data in
161161
let spendComponentsConverted = spendComponents.components.map { spendComponent in
162162
let witnessAuthPath: [WitnessNode] = spendComponent.witnessAuthPath.map { WitnessNode(side: $0.side, hashOfSibling: Data(hexString: $0.hashOfSibling)!) }
163163
return SpendComponents(note: Data(hexString: spendComponent.note)!, witnessRootHash: Data(hexString: spendComponent.witnessRootHash)!, witnessTreeSize: UInt64(spendComponent.witnessTreeSize)!, witnessAuthPath: witnessAuthPath)
164164
}
165165
do {
166-
let transaction = try createTransaction(spendComponents: spendComponentsConverted, outputs: outputs.outputs.map {Data(hexString: $0)!}, spendingKey: spendingKey)
166+
let transaction = try createTransaction(transactionVersion: transactionVersion, spendComponents: spendComponentsConverted, outputs: outputs.outputs.map {Data(hexString: $0)!}, spendingKey: spendingKey)
167167
return transaction
168168
} catch let error as NSError {
169169
print("Unexpected error: \(error.debugDescription)")

packages/ironfish-native-module/rust_lib/src/lib.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,12 +338,17 @@ pub fn create_note(params: NoteParams) -> Result<Vec<u8>, EnumError> {
338338

339339
#[uniffi::export]
340340
pub fn create_transaction(
341+
transaction_version: u8,
341342
spend_components: Vec<SpendComponents>,
342343
outputs: Vec<Vec<u8>>,
343344
spending_key: Vec<u8>,
344345
) -> Result<Vec<u8>, EnumError> {
345-
let mut transaction =
346-
ironfish::ProposedTransaction::new(ironfish::transaction::TransactionVersion::V2);
346+
let version = ironfish::transaction::TransactionVersion::from_u8(transaction_version)
347+
.ok_or_else(|| EnumError::Error {
348+
msg: "Invalid transaction version".to_string(),
349+
})?;
350+
351+
let mut transaction = ironfish::ProposedTransaction::new(version);
347352
for spend_component in spend_components {
348353
let note_data = Cursor::new(spend_component.note);
349354
let note =

packages/ironfish-native-module/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export function createNote({
124124
}
125125

126126
export function createTransaction(
127+
transactionVersion: number,
127128
spendComponents: {
128129
note: string;
129130
witnessRootHash: string;
@@ -134,6 +135,7 @@ export function createTransaction(
134135
spendingKey: Uint8Array,
135136
): Promise<Uint8Array> {
136137
return IronfishNativeModule.createTransaction(
138+
transactionVersion,
137139
{ components: spendComponents },
138140
{ outputs },
139141
spendingKey,

packages/mobile-app/app/send/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export default function Send() {
3535
{getAccountResult.data?.balances.custom.map((b) => (
3636
<Button
3737
key={b.assetId}
38-
title={`${b.assetId} (${getAccountResult.data?.balances.iron.confirmed ?? 0}) ${selectedAssetId === b.assetId ? "(selected)" : ""}`}
38+
title={`${b.assetId} (${b.confirmed ?? 0}) ${selectedAssetId === b.assetId ? "(selected)" : ""}`}
3939
onPress={() => setSelectedAssetId(b.assetId)}
4040
/>
4141
))}

packages/mobile-app/data/wallet/wallet.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { AssetLoader } from "./assetLoader";
1616
import { Blockchain } from "../blockchain";
1717
import { Output } from "../facades/wallet/types";
1818
import { WalletServerApi } from "../walletServerApi/walletServer";
19+
import { Consensus, MAINNET, TESTNET } from "@ironfish/sdk";
1920

2021
type StartedState = { type: "STARTED"; db: WalletDb; assetLoader: AssetLoader };
2122
type WalletState = { type: "STOPPED" } | { type: "LOADING" } | StartedState;
@@ -683,6 +684,21 @@ export class Wallet {
683684
return this.state.assetLoader.getAsset(network, assetId);
684685
}
685686

687+
private async getActiveTransactionVersion(network: Network) {
688+
// TODO IFL-2898 Consider fetching the active transaction version from the API
689+
// so we don't have to deploy a new version when setting the activation sequence.
690+
const latestBlock = await Blockchain.getLatestBlock(network);
691+
let consensus;
692+
if (network === Network.MAINNET) {
693+
consensus = new Consensus(MAINNET.consensus);
694+
} else if (network === Network.TESTNET) {
695+
consensus = new Consensus(TESTNET.consensus);
696+
} else {
697+
throw new Error("Unsupported network");
698+
}
699+
return consensus.getActiveTransactionVersion(latestBlock.sequence);
700+
}
701+
686702
async sendTransaction(
687703
network: Network,
688704
accountName: string,
@@ -775,6 +791,8 @@ export class Wallet {
775791
throw new Error("witnesses don't match");
776792
}
777793

794+
const txnVersion = await this.getActiveTransactionVersion(network);
795+
778796
console.log(
779797
`Witnesses and notes prepared in ${performance.now() - lastTime}ms`,
780798
);
@@ -795,6 +813,7 @@ export class Wallet {
795813
throw new Error("Spending key not found");
796814
}
797815
const result = await IronfishNativeModule.createTransaction(
816+
txnVersion,
798817
spendComponents,
799818
notes.map((note) => Uint8ArrayUtils.toHex(note)),
800819
Uint8ArrayUtils.fromHex(spendingKey),

0 commit comments

Comments
 (0)