Skip to content

Commit 774b95c

Browse files
fazzattiVictor Hugotoruguera
authored
release 0.5.0 (#19)
Co-authored-by: Victor Hugo <[email protected]> Co-authored-by: Victor Hugo Martins <[email protected]>
1 parent 0a807ac commit 774b95c

File tree

9 files changed

+115
-59
lines changed

9 files changed

+115
-59
lines changed

codecov.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
coverage:
22
status:
33
patch:
4+
default:
5+
informational: true
6+
project:
47
default:
58
target: 70%

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: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +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";
1414
import type { ChannelInvoke, ChannelRead } from "./types.ts";
1515
import type { xdr } from "@stellar/stellar-sdk";
1616
import * as E from "./error.ts";
17+
import type { UTXOPublicKey } from "../core/utxo-keypair-base/types.ts";
18+
import { Buffer } from "buffer";
1719

1820
export class PrivacyChannel {
1921
private _client: Contract;
2022
private _authId: ContractId;
2123
private _assetId: ContractId;
2224
private _networkConfig: NetworkConfig;
23-
private _derivator: StellarDerivator;
2425

2526
public constructor(
2627
networkConfig: NetworkConfig,
@@ -38,11 +39,6 @@ export class PrivacyChannel {
3839
this._authId = authId;
3940

4041
this._assetId = assetId;
41-
42-
this._derivator = new StellarDerivator().withNetworkAndContract(
43-
networkConfig.networkPassphrase as StellarNetworkId,
44-
channelId as ContractId,
45-
);
4642
}
4743

4844
//==========================================
@@ -61,11 +57,10 @@ export class PrivacyChannel {
6157
private require(arg: "_client"): Contract;
6258
private require(arg: "_authId"): ContractId;
6359
private require(arg: "_networkConfig"): NetworkConfig;
64-
private require(arg: "_derivator"): StellarDerivator;
6560
private require(arg: "_assetId"): ContractId;
6661
private require(
67-
arg: "_client" | "_authId" | "_networkConfig" | "_derivator" | "_assetId",
68-
): Contract | ContractId | NetworkConfig | StellarDerivator {
62+
arg: "_client" | "_authId" | "_networkConfig" | "_assetId",
63+
): Contract | ContractId | NetworkConfig {
6964
if (this[arg]) return this[arg];
7065
throw new E.PROPERTY_NOT_SET(arg);
7166
}
@@ -125,10 +120,13 @@ export class PrivacyChannel {
125120
*
126121
* @params None
127122
* @returns {StellarDerivator} The StellarDerivator instance.
128-
* @throws {Error} If the StellarDerivator instance is not set.
123+
* @throws {Error} If any of the underlying properties are not set.
129124
*/
130125
public getDerivator(): StellarDerivator {
131-
return this.require("_derivator");
126+
return new StellarDerivator().withNetworkAndContract(
127+
this.getNetworkConfig().networkPassphrase as StellarNetworkId,
128+
this.getChannelId() as ContractId,
129+
);
132130
}
133131

134132
/**
@@ -142,8 +140,28 @@ export class PrivacyChannel {
142140
return this.getClient().getContractId();
143141
}
144142

143+
/**
144+
* Returns a function that fetches balances for given UTXO public keys.
145+
*
146+
* @returns {(publicKeys: Uint8Array[]) => Promise<bigint[]>}
147+
*/
148+
public getBalancesFetcher(): (
149+
publicKeys: UTXOPublicKey[],
150+
) => Promise<bigint[]> {
151+
const fetchBalances = async (
152+
publicKeys: UTXOPublicKey[],
153+
): Promise<bigint[]> => {
154+
return await this.read({
155+
method: ChannelReadMethods.utxo_balances,
156+
methodArgs: { utxos: publicKeys.map((pk) => Buffer.from(pk)) },
157+
});
158+
};
159+
160+
return fetchBalances;
161+
}
162+
145163
//==========================================
146-
// Read / Write Methods
164+
// Contract Read / Write Methods
147165
//==========================================
148166
//
149167
//

src/transaction-builder/index.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import type {
3939
import * as E from "./error.ts";
4040
import { assert } from "../utils/assert/assert.ts";
4141
import { assertExtOpsExist } from "./validators/operations.ts";
42+
import type { PrivacyChannel } from "../privacy-channel/index.ts";
4243

4344
export class MoonlightTransactionBuilder {
4445
private _create: CreateOperation[] = [];
@@ -75,6 +76,22 @@ export class MoonlightTransactionBuilder {
7576
this._network = network;
7677
}
7778

79+
/**
80+
* Creates a MoonlightTransactionBuilder instance from an existing PrivacyChannel.
81+
* @param channelClient - The PrivacyChannel instance to use.
82+
* @returns {MoonlightTransactionBuilder} A MoonlightTransactionBuilder instance pre-configured with the PrivacyChannel's parameters.
83+
*/
84+
static fromPrivacyChannel(
85+
channelClient: PrivacyChannel,
86+
): MoonlightTransactionBuilder {
87+
return new MoonlightTransactionBuilder({
88+
channelId: channelClient.getChannelId(),
89+
authId: channelClient.getAuthId(),
90+
network: channelClient.getNetworkConfig().networkPassphrase,
91+
assetId: channelClient.getAssetId(),
92+
});
93+
}
94+
7895
//==========================================
7996
// Meta Requirement Methods
8097
//==========================================

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+
};

src/utxo-based-account/utxo-based-stellar-account/index.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,43 @@ import type {
44
StellarDerivationIndex,
55
StellarDerivationRoot,
66
} from "../../derivation/stellar/types.ts";
7+
import type { PrivacyChannel } from "../../privacy-channel/index.ts";
8+
import type { UTXOBasedAccountConstructorArgs } from "../types.ts";
79

810
export class UtxoBasedStellarAccount extends UtxoBasedAccount<
911
StellarDerivationContext,
1012
StellarDerivationRoot,
1113
StellarDerivationIndex
12-
> {}
14+
> {
15+
/**
16+
* Create a UTXO-based Stellar account handler from a PrivacyChannel instance.
17+
*
18+
* @param args - The arguments for creating the UTXO-based Stellar account handler.
19+
* @param args.channelClient - The PrivacyChannel instance to use.
20+
* @param args.root - The root derivation key for the Stellar account.
21+
* @param args.options - Additional options for the UTXO-based account handler.
22+
* @returns {UtxoBasedStellarAccount} A UTXO-based Stellar account handler.
23+
*/
24+
static fromPrivacyChannel(args: {
25+
channelClient: PrivacyChannel;
26+
root: StellarDerivationRoot;
27+
options?: Omit<
28+
UTXOBasedAccountConstructorArgs<
29+
StellarDerivationContext,
30+
StellarDerivationRoot,
31+
StellarDerivationIndex
32+
>["options"],
33+
"fetchBalances"
34+
>;
35+
}): UtxoBasedStellarAccount {
36+
const { channelClient, root } = args;
37+
return new UtxoBasedStellarAccount({
38+
derivator: channelClient.getDerivator(),
39+
root,
40+
options: {
41+
...(args.options ?? {}),
42+
fetchBalances: channelClient.getBalancesFetcher(),
43+
},
44+
});
45+
}
46+
}

test/integration/privacy-channel.integration.test.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ import {
3131
generateNonce,
3232
generateP256KeyPair,
3333
MoonlightOperation as op,
34-
MoonlightTransactionBuilder,
3534
PrivacyChannel,
3635
} from "../../mod.ts";
3736

3837
import { Asset, Keypair } from "@stellar/stellar-sdk";
3938

4039
import { disableSanitizeConfig } from "../utils/disable-sanitize-config.ts";
4140
import { Server } from "@stellar/stellar-sdk/rpc";
41+
import { MoonlightTransactionBuilder } from "../../src/transaction-builder/index.ts";
4242

4343
describe(
4444
"[Testnet - Integration] PrivacyChannel",
@@ -232,14 +232,9 @@ describe(
232232
const utxoAKeypair = await generateP256KeyPair();
233233
const utxoBKeypair = await generateP256KeyPair();
234234

235-
const depositTx = new MoonlightTransactionBuilder({
236-
network: networkConfig.networkPassphrase,
237-
channelId: channelId,
238-
authId: authId,
239-
assetId: Asset.native().contractId(
240-
networkConfig.networkPassphrase,
241-
) as ContractId,
242-
});
235+
const depositTx = MoonlightTransactionBuilder.fromPrivacyChannel(
236+
channelClient,
237+
);
243238

244239
const createOpA = op.create(utxoAKeypair.publicKey, 250n);
245240
const createOpB = op.create(utxoBKeypair.publicKey, 250n);

test/integration/utxo-based-account.integration.test.ts

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -187,14 +187,7 @@ describe(
187187
root: testRoot,
188188
options: {
189189
batchSize: 10,
190-
fetchBalances: async (publicKeys: Uint8Array[]) => {
191-
return channelClient.read({
192-
method: ChannelReadMethods.utxo_balances,
193-
methodArgs: {
194-
utxos: publicKeys.map((pk) => Buffer.from(pk)),
195-
},
196-
});
197-
},
190+
fetchBalances: channelClient.getBalancesFetcher(),
198191
},
199192
});
200193

@@ -254,25 +247,11 @@ describe(
254247
const testRoot = "S-TEST_SECRET_ROOT_3";
255248
const depositAmount = 500000n; // 0.05 XLM
256249

257-
// Create a fresh derivator for this test
258-
const freshDerivator = new StellarDerivator().withNetworkAndContract(
259-
StellarNetworkId.Testnet,
260-
channelId,
261-
);
262-
263-
const utxoAccount = new UtxoBasedStellarAccount({
264-
derivator: freshDerivator,
250+
const utxoAccount = UtxoBasedStellarAccount.fromPrivacyChannel({
251+
channelClient,
265252
root: testRoot,
266253
options: {
267254
batchSize: 10,
268-
fetchBalances: async (publicKeys: Uint8Array[]) => {
269-
return channelClient.read({
270-
method: ChannelReadMethods.utxo_balances,
271-
methodArgs: {
272-
utxos: publicKeys.map((pk) => Buffer.from(pk)),
273-
},
274-
});
275-
},
276255
},
277256
});
278257

0 commit comments

Comments
 (0)