@@ -8,12 +8,20 @@ import { StellarDerivator } from "../derivation/stellar/index.ts";
88import type { StellarNetworkId } from "../derivation/stellar/stellar-network-id.ts" ;
99import {
1010 type ChannelInvokeMethods ,
11- type ChannelReadMethods ,
11+ ChannelReadMethods ,
1212 ChannelSpec ,
1313} from "./constants.ts" ;
14- import type { ChannelInvoke , ChannelRead } from "./types.ts" ;
14+ import type {
15+ ChannelInvoke ,
16+ ChannelRead ,
17+ GetUTXOAccountHandlerArgs ,
18+ } from "./types.ts" ;
1519import type { xdr } from "@stellar/stellar-sdk" ;
1620import * as E from "./error.ts" ;
21+ import type { UTXOPublicKey } from "../core/utxo-keypair-base/types.ts" ;
22+ import { Buffer } from "buffer" ;
23+ import { MoonlightTransactionBuilder } from "../transaction-builder/index.ts" ;
24+ import { UtxoBasedStellarAccount } from "../utxo-based-account/utxo-based-stellar-account/index.ts" ;
1725
1826export class PrivacyChannel {
1927 private _client : Contract ;
@@ -142,8 +150,73 @@ export class PrivacyChannel {
142150 return this . getClient ( ) . getContractId ( ) ;
143151 }
144152
153+ /**
154+ * Returns a function that fetches balances for given UTXO public keys.
155+ *
156+ * @returns {(publicKeys: Uint8Array[]) => Promise<bigint[]> }
157+ */
158+ public getBalancesFetcher ( ) : (
159+ publicKeys : UTXOPublicKey [ ] ,
160+ ) => Promise < bigint [ ] > {
161+ const fetchBalances = ( publicKeys : UTXOPublicKey [ ] ) => {
162+ return this . read ( {
163+ method : ChannelReadMethods . utxo_balances ,
164+ methodArgs : { utxos : publicKeys . map ( ( pk ) => Buffer . from ( pk ) ) } ,
165+ } ) ;
166+ } ;
167+
168+ return fetchBalances ;
169+ }
170+
171+ /**
172+ * Creates and returns a MoonlightTransactionBuilder instance
173+ * pre-configured for this privacy channel.
174+ *
175+ * @returns {MoonlightTransactionBuilder } A pre-configured MoonlightTransactionBuilder instance.
176+ */
177+ public getTransactionBuilder ( ) : MoonlightTransactionBuilder {
178+ const txBuilder = new MoonlightTransactionBuilder ( {
179+ channelId : this . getChannelId ( ) ,
180+ authId : this . getAuthId ( ) ,
181+ network : this . getNetworkConfig ( ) . networkPassphrase ,
182+ assetId : this . getAssetId ( ) ,
183+ } ) ;
184+
185+ return txBuilder ;
186+ }
187+
188+ /**
189+ * Creates and returns a UtxoBasedStellarAccount handler
190+ * pre-configured for this privacy channel.
191+ *
192+ * @param {GetUTXOAccountHandlerArgs } args - The arguments for creating the UTXO account handler.
193+ * @param {Ed25519SecretKey } args.root - The root secret key for the Stellar account.
194+ * @param {Object } [args.options] - Additional options for the UTXO account handler.
195+ * @returns {UtxoBasedStellarAccount } A handler for UTXO-based Stellar accounts, pre-configured for this privacy channel. Use this to manage UTXO-based operations for the associated Stellar account.
196+ */
197+ public getUTXOAccountHandler (
198+ args : GetUTXOAccountHandlerArgs ,
199+ ) : UtxoBasedStellarAccount {
200+ const { root, options } = args ;
201+
202+ const derivator = new StellarDerivator ( ) . withNetworkAndContract (
203+ this . getNetworkConfig ( ) . networkPassphrase as StellarNetworkId ,
204+ this . getChannelId ( ) as ContractId ,
205+ ) ;
206+ const accountHandler = new UtxoBasedStellarAccount ( {
207+ derivator,
208+ root,
209+ options : {
210+ ...options ,
211+ fetchBalances : this . getBalancesFetcher ( ) ,
212+ } ,
213+ } ) ;
214+
215+ return accountHandler ;
216+ }
217+
145218 //==========================================
146- // Read / Write Methods
219+ // Contract Read / Write Methods
147220 //==========================================
148221 //
149222 //
0 commit comments