Skip to content

Commit 0f36842

Browse files
authored
Merge pull request #18 from Moonlight-Protocol/feat/add-client-helping-functions-to-channel-client
2 parents acf8f1f + dcdaac3 commit 0f36842

File tree

5 files changed

+120
-16
lines changed

5 files changed

+120
-16
lines changed

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@moonlight/moonlight-sdk",
3-
"version": "0.4.0",
3+
"version": "0.5.0",
44
"description": "A privacy-focused toolkit for the Moonlight protocol on Stellar Soroban smart contracts.",
55
"license": "MIT",
66
"tasks": {

src/privacy-channel/index.ts

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,20 @@ import { StellarDerivator } from "../derivation/stellar/index.ts";
88
import type { StellarNetworkId } from "../derivation/stellar/stellar-network-id.ts";
99
import {
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";
1519
import type { xdr } from "@stellar/stellar-sdk";
1620
import * 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

1826
export 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
//

src/privacy-channel/types.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
1-
import type { ContractId, Ed25519PublicKey } from "@colibri/core";
1+
import type {
2+
ContractId,
3+
Ed25519PublicKey,
4+
Ed25519SecretKey,
5+
} from "@colibri/core";
26
import type { Buffer } from "node:buffer";
37
import type { ChannelInvokeMethods, ChannelReadMethods } from "./constants.ts";
8+
import type { UTXOBasedAccountConstructorArgs } from "../utxo-based-account/types.ts";
49

510
export type ChannelConstructorArgs = {
611
admin: Ed25519PublicKey | ContractId;
@@ -73,3 +78,19 @@ export type ChannelInvoke = {
7378
output: None;
7479
};
7580
};
81+
82+
export type GetUTXOAccountHandlerArgs =
83+
& Pick<
84+
UTXOBasedAccountConstructorArgs<string, Ed25519SecretKey, `${number}`>,
85+
"root"
86+
>
87+
& {
88+
options?: Omit<
89+
UTXOBasedAccountConstructorArgs<
90+
string,
91+
Ed25519SecretKey,
92+
`${number}`
93+
>["options"],
94+
"fetchBalances"
95+
>;
96+
};

src/utxo-based-account/index.ts

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { UTXOKeypair } from "../core/utxo-keypair/index.ts";
22
import { UTXOStatus } from "../core/utxo-keypair/types.ts";
33
import type { BaseDerivator } from "../derivation/base/index.ts";
44
import { UTXOSelectionStrategy } from "./selection-strategy.ts";
5-
import type { UTXOSelectionResult } from "./types.ts";
5+
import type {
6+
UTXOBasedAccountConstructorArgs,
7+
UTXOSelectionResult,
8+
} from "./types.ts";
69
import * as E from "./error.ts";
710
import { assert } from "../utils/assert/assert.ts";
811
/**
@@ -46,16 +49,7 @@ export class UtxoBasedAccount<
4649
/**
4750
* Creates a new UtxoBasedAccount instance
4851
*/
49-
constructor(args: {
50-
derivator: BaseDerivator<Context, Root, Index>;
51-
root: Root;
52-
options?: {
53-
batchSize?: number;
54-
fetchBalances?: (publicKeys: Uint8Array[]) => Promise<bigint[]>;
55-
startIndex?: number;
56-
maxReservationAgeMs?: number;
57-
};
58-
}) {
52+
constructor(args: UTXOBasedAccountConstructorArgs<Context, Root, Index>) {
5953
this.root = args.root;
6054
this.derivator = args.derivator;
6155
this.derivator.withRoot(this.root);

src/utxo-based-account/types.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { UTXOKeypair } from "../core/utxo-keypair/index.ts";
2+
import type { BaseDerivator } from "../derivation/base/index.ts";
23

34
/**
45
* Result of UTXO selection for transfers
@@ -8,3 +9,18 @@ export interface UTXOSelectionResult<Context extends string> {
89
totalAmount: bigint;
910
changeAmount: bigint;
1011
}
12+
13+
export type UTXOBasedAccountConstructorArgs<
14+
Context extends string,
15+
Root extends string,
16+
Index extends `${number}`,
17+
> = {
18+
derivator: BaseDerivator<Context, Root, Index>;
19+
root: Root;
20+
options?: {
21+
batchSize?: number;
22+
fetchBalances?: (publicKeys: Uint8Array[]) => Promise<bigint[]>;
23+
startIndex?: number;
24+
maxReservationAgeMs?: number;
25+
};
26+
};

0 commit comments

Comments
 (0)