-
Notifications
You must be signed in to change notification settings - Fork 573
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds UnsignedTransaction primitive (#4619)
- Loading branch information
Showing
1 changed file
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |