Skip to content

Commit

Permalink
adds UnsignedTransaction primitive (#4619)
Browse files Browse the repository at this point in the history
  • Loading branch information
jowparks authored Jan 26, 2024
1 parent a59fcbd commit 59664c2
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions ironfish/src/primitives/unsignedTransaction.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */

import { UnsignedTransaction as NativeUnsignedTransaction } from '@ironfish/rust-nodejs'

export class UnsignedTransaction {
private readonly unsignedTransactionSerialized: Buffer
private referenceCount = 0
private nativeUnsignedTransaction: NativeUnsignedTransaction | null = null

constructor(unsignedTransactionSerialized: Buffer) {
this.unsignedTransactionSerialized = unsignedTransactionSerialized
}

serialize(): Buffer {
return this.unsignedTransactionSerialized
}

/**
* Preallocate any resources necessary for using the transaction.
*/
takeReference(): NativeUnsignedTransaction {
this.referenceCount++
if (this.nativeUnsignedTransaction === null) {
this.nativeUnsignedTransaction = new NativeUnsignedTransaction(
this.unsignedTransactionSerialized,
)
}
return this.nativeUnsignedTransaction
}

/**
* Return any resources necessary for using the transaction.
*/
returnReference(): void {
this.referenceCount--
if (this.referenceCount <= 0) {
this.referenceCount = 0
this.nativeUnsignedTransaction = null
}
}

/**
* Wraps the given callback in takeReference and returnReference.
*/
withReference<R>(callback: (transaction: NativeUnsignedTransaction) => R): R {
const transaction = this.takeReference()

const result = callback(transaction)

void Promise.resolve(result).finally(() => {
this.returnReference()
})

return result
}
}

0 comments on commit 59664c2

Please sign in to comment.