Skip to content

Commit 27845e9

Browse files
authored
Add expiration and fee to createTransaction (#54)
1 parent 08bda29 commit 27845e9

File tree

4 files changed

+24
-4
lines changed

4 files changed

+24
-4
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") { (transactionVersion: UInt8, spendComponents: SpendComponentsInput, outputs: OutputsInput, spendingKey: Data) throws -> Data in
160+
AsyncFunction("createTransaction") { (transactionVersion: UInt8, transactionFee: String, expirationSequence: UInt32, 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(transactionVersion: transactionVersion, spendComponents: spendComponentsConverted, outputs: outputs.outputs.map {Data(hexString: $0)!}, spendingKey: spendingKey)
166+
let transaction = try createTransaction(transactionVersion: transactionVersion, transactionFee: UInt64(transactionFee)!, expirationSequence: expirationSequence, 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
@@ -339,6 +339,8 @@ pub fn create_note(params: NoteParams) -> Result<Vec<u8>, EnumError> {
339339
#[uniffi::export]
340340
pub fn create_transaction(
341341
transaction_version: u8,
342+
transaction_fee: u64,
343+
expiration_sequence: u32,
342344
spend_components: Vec<SpendComponents>,
343345
outputs: Vec<Vec<u8>>,
344346
spending_key: Vec<u8>,
@@ -394,13 +396,16 @@ pub fn create_transaction(
394396
.map_err(|e| EnumError::Error { msg: e.to_string() })?;
395397
}
396398

399+
transaction.set_expiration(expiration_sequence);
400+
397401
let mut spending_key_data = Cursor::new(spending_key);
398402
let spending_key = ironfish::SaplingKey::read(&mut spending_key_data)
399403
.map_err(|e| EnumError::Error { msg: e.to_string() })?;
404+
400405
let posted = transaction
401-
.post(&spending_key, None, 1)
406+
.post(&spending_key, None, transaction_fee)
402407
.map_err(|e| EnumError::Error { msg: e.to_string() })?;
403-
let mut buffer = Vec::new();
408+
let mut buffer: Vec<u8> = Vec::new();
404409
posted
405410
.write(&mut buffer)
406411
.map_err(|e| EnumError::Error { msg: e.to_string() })?;

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ export function createNote({
125125

126126
export function createTransaction(
127127
transactionVersion: number,
128+
transactionFee: string,
129+
expirationSequence: number,
128130
spendComponents: {
129131
note: string;
130132
witnessRootHash: string;
@@ -136,6 +138,8 @@ export function createTransaction(
136138
): Promise<Uint8Array> {
137139
return IronfishNativeModule.createTransaction(
138140
transactionVersion,
141+
transactionFee,
142+
expirationSequence,
139143
{ components: spendComponents },
140144
{ outputs },
141145
spendingKey,

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ function assertStarted(state: WalletState): asserts state is StartedState {
3737
// TODO: Make confirmations configurable
3838
const CONFIRMATIONS = 2;
3939

40+
// Used when calculating a transaction's expiration sequence by adding it
41+
// to the latest block sequence returned from the API.
42+
const EXPIRATION_DELTA = 20;
43+
4044
export class Wallet {
4145
state: WalletState = { type: "STOPPED" };
4246
scanState: ScanState = { type: "IDLE" };
@@ -812,8 +816,15 @@ export class Wallet {
812816
if (spendingKey === null) {
813817
throw new Error("Spending key not found");
814818
}
819+
820+
const { sequence: latestSequence } =
821+
await Blockchain.getLatestBlock(network);
822+
815823
const result = await IronfishNativeModule.createTransaction(
816824
txnVersion,
825+
// TODO: Implement proper transaction fees
826+
"1",
827+
latestSequence + EXPIRATION_DELTA,
817828
spendComponents,
818829
notes.map((note) => Uint8ArrayUtils.toHex(note)),
819830
Uint8ArrayUtils.fromHex(spendingKey),

0 commit comments

Comments
 (0)