Skip to content

Commit a5b11e1

Browse files
committed
Changing fromCiphertext, verify, and getBlockRange to use object params
1 parent c737dff commit a5b11e1

File tree

7 files changed

+110
-54
lines changed

7 files changed

+110
-54
lines changed

sdk/src/account.ts

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -68,17 +68,23 @@ export class Account {
6868

6969
/**
7070
* Attempts to create an account from a private key ciphertext
71-
* @param {PrivateKeyCiphertext | string} ciphertext The encrypted private key ciphertext or its string representation
72-
* @param {string} password The password used to decrypt the private key ciphertext
71+
* @param {Object} params
72+
* @param {PrivateKeyCiphertext | string} params.ciphertext The encrypted private key ciphertext or its string representation
73+
* @param {string} params.password The password used to decrypt the private key ciphertext
7374
* @returns {Account} A new Account instance created from the decrypted private key
7475
*
7576
* @example
7677
* import { Account } from "@provablehq/sdk/testnet.js";
7778
*
7879
* // Create an account object from a previously encrypted ciphertext and password.
79-
* const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
80+
* const account = Account.fromCiphertext({
81+
* ciphertext: process.env.ciphertext,
82+
* password: process.env.password,
83+
* });
8084
*/
81-
public static fromCiphertext(ciphertext: PrivateKeyCiphertext | string, password: string): Account {
85+
public static fromCiphertext(params: { ciphertext: PrivateKeyCiphertext | string, password: string }): Account {
86+
let { ciphertext, password } = params;
87+
8288
try {
8389
ciphertext = (typeof ciphertext === "string") ? PrivateKeyCiphertext.fromString(ciphertext) : ciphertext;
8490
const _privateKey = PrivateKey.fromPrivateKeyCiphertext(ciphertext, password);
@@ -211,7 +217,10 @@ export class Account {
211217
*
212218
* // Create a connection to the Aleo network and an account
213219
* const networkClient = new AleoNetworkClient({ host: "https://api.explorer.provable.com/v1" });
214-
* const account = Account.fromCiphertext(process.env.ciphertext!, process.env.password!);
220+
* const account = Account.fromCiphertext({
221+
* ciphertext: process.env.ciphertext!,
222+
* password: process.env.password!,
223+
* });
215224
*
216225
* // Get the record ciphertexts from a transaction.
217226
* const transaction = await networkClient.getTransactionObject("at1fjy6s9md2v4rgcn3j3q4qndtfaa2zvg58a4uha0rujvrn4cumu9qfazxdd");
@@ -241,7 +250,10 @@ export class Account {
241250
*
242251
* // Create a connection to the Aleo network and an account
243252
* const networkClient = new AleoNetworkClient({ host: "https://api.explorer.provable.com/v1" });
244-
* const account = Account.fromCiphertext(process.env.ciphertext!, process.env.password!);
253+
* const account = Account.fromCiphertext({
254+
* ciphertext: process.env.ciphertext!,
255+
* password: process.env.password!,
256+
* });
245257
*
246258
* // Get the record ciphertexts from a transaction.
247259
* const transaction = await networkClient.getTransactionObject("at1fjy6s9md2v4rgcn3j3q4qndtfaa2zvg58a4uha0rujvrn4cumu9qfazxdd");
@@ -259,14 +271,17 @@ export class Account {
259271
* This key can be used to decrypt the record without revealing the account's view key.
260272
* @param {RecordCiphertext | string} recordCiphertext The record ciphertext to generate the view key for
261273
* @returns {Field} The record view key
262-
*
274+
*
263275
* @example
264276
* // Import the Account class
265277
* import { Account } from "@provablehq/sdk/testnet.js";
266-
*
278+
*
267279
* // Create an account object from a previously encrypted ciphertext and password.
268-
* const account = Account.fromCiphertext(process.env.ciphertext!, process.env.password!);
269-
*
280+
* const account = Account.fromCiphertext({
281+
* ciphertext: process.env.ciphertext!,
282+
* password: process.env.password!,
283+
* });
284+
*
270285
* // Generate a record view key from the account's view key and a record ciphertext
271286
* const recordCiphertext = RecordCiphertext.fromString("your_record_ciphertext_here");
272287
* const recordViewKey = account.generateRecordViewKey(recordCiphertext);
@@ -283,18 +298,18 @@ export class Account {
283298

284299
/**
285300
* Generates a transition view key from the account owner's view key and the transition public key.
286-
* This key can be used to decrypt the private inputs and outputs of a the transition without
301+
* This key can be used to decrypt the private inputs and outputs of a the transition without
287302
* revealing the account's view key.
288303
* @param {string | Group} tpk The transition public key
289304
* @returns {Field} The transition view key
290-
*
305+
*
291306
* @example
292307
* // Import the Account class
293308
* import { Account } from "@provablehq/sdk/testnet.js";
294-
*
309+
*
295310
* // Generate a transition view key from the account's view key and a transition public key
296311
* const tpk = Group.fromString("your_transition_public_key_here");
297-
*
312+
*
298313
* const transitionViewKey = account.generateTransitionViewKey(tpk);
299314
*/
300315
generateTransitionViewKey(tpk: string | Group): Field {
@@ -315,7 +330,10 @@ export class Account {
315330
*
316331
* // Create a connection to the Aleo network and an account
317332
* const networkClient = new AleoNetworkClient({ host: "https://api.explorer.provable.com/v1" });
318-
* const account = Account.fromCiphertext(process.env.ciphertext!, process.env.password!);
333+
* const account = Account.fromCiphertext({
334+
* ciphertext: process.env.ciphertext!,
335+
* password: process.env.password!,
336+
* });
319337
*
320338
* // Get the record ciphertexts from a transaction and check ownership of them.
321339
* const transaction = await networkClient.getTransactionObject("at1fjy6s9md2v4rgcn3j3q4qndtfaa2zvg58a4uha0rujvrn4cumu9qfazxdd");
@@ -356,15 +374,18 @@ export class Account {
356374
* import { Account } from "@provablehq/sdk/testnet.js";
357375
*
358376
* // Create a connection to the Aleo network and an account
359-
* const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
377+
* const account = Account.fromCiphertext({
378+
* ciphertext: process.env.ciphertext,
379+
* password: process.env.password,
380+
* });
360381
*
361382
* // Create an account and a message to sign.
362383
* const account = new Account();
363384
* const message = Uint8Array.from([104, 101, 108, 108, 111 119, 111, 114, 108, 100])
364385
* const signature = account.sign(message);
365386
*
366387
* // Verify the signature.
367-
* assert(account.verify(message, signature));
388+
* assert(account.verify({ message, signature }));
368389
*/
369390
sign(message: Uint8Array): Signature {
370391
return this._privateKey.sign(message);
@@ -373,25 +394,30 @@ export class Account {
373394
/**
374395
* Verifies the Signature on a message.
375396
*
376-
* @param {Uint8Array} message Message in bytes to be signed.
377-
* @param {Signature} signature Signature to be verified.
397+
* @param {Object} params
398+
* @param {Uint8Array} params.message Message in bytes to be signed.
399+
* @param {Signature} params.signature Signature to be verified.
378400
* @returns {boolean} True if the signature is valid, false otherwise.
379401
*
380402
* @example
381403
* // Import the Account class
382404
* import { Account } from "@provablehq/sdk/testnet.js";
383405
*
384406
* // Create a connection to the Aleo network and an account
385-
* const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
407+
* const account = Account.fromCiphertext({
408+
* ciphertext: process.env.ciphertext,
409+
* password: process.env.password,
410+
* });
386411
*
387412
* // Sign a message.
388413
* const message = Uint8Array.from([104, 101, 108, 108, 111 119, 111, 114, 108, 100])
389414
* const signature = account.sign(message);
390415
*
391416
* // Verify the signature.
392-
* assert(account.verify(message, signature));
417+
* assert(account.verify({ message, signature }));
393418
*/
394-
verify(message: Uint8Array, signature: Signature): boolean {
419+
verify(params: { message: Uint8Array, signature: Signature }): boolean {
420+
const { message, signature } = params;
395421
return this._address.verify(message, signature);
396422
}
397423
}

sdk/src/network-client.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ interface DelegatedProvingParams {
4545
* const localNetworkClient = new AleoNetworkClient({ host: "http://0.0.0.0:3030" });
4646
*
4747
* // Connection to a public beacon node
48-
* const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
48+
* const account = Account.fromCiphertext({
49+
* ciphertext: process.env.ciphertext,
50+
* password: process.env.password,
51+
* });
4952
* const publicNetworkClient = new AleoNetworkClient({ host: "http://api.explorer.provable.com/v1" });
5053
*/
5154
class AleoNetworkClient {
@@ -207,7 +210,10 @@ class AleoNetworkClient {
207210
* import { Account, AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
208211
*
209212
* // Import an account from a ciphertext and password.
210-
* const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
213+
* const account = Account.fromCiphertext({
214+
* ciphertext: process.env.ciphertext,
215+
* password: process.env.password,
216+
* });
211217
*
212218
* // Create a network client.
213219
* const networkClient = new AleoNetworkClient({ host: "http://api.explorer.provable.com/v1" });
@@ -313,7 +319,7 @@ class AleoNetworkClient {
313319
}
314320
try {
315321
// Get 50 blocks (or the difference between the start and end if less than 50)
316-
const blocks = await this.getBlockRange(start, end);
322+
const blocks = await this.getBlockRange({ start, end });
317323
end = start;
318324
// Iterate through blocks to find unspent records
319325
for (let i = 0; i < blocks.length; i++) {
@@ -542,7 +548,10 @@ class AleoNetworkClient {
542548
* @example
543549
* import { Account, AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
544550
*
545-
* const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
551+
* const account = Account.fromCiphertext({
552+
* ciphertext: process.env.ciphertext,
553+
* password: process.env.password,
554+
* });
546555
*
547556
* // Create a network client and set an account to search for records with.
548557
* const networkClient = new AleoNetworkClient({ host: "http://api.explorer.provable.com/v1" });
@@ -632,24 +641,27 @@ class AleoNetworkClient {
632641
/**
633642
* Returns a range of blocks between the specified block heights. A maximum of 50 blocks can be fetched at a time.
634643
*
635-
* @param {number} start Starting block to fetch.
636-
* @param {number} end Ending block to fetch. This cannot be more than 50 blocks ahead of the start block.
644+
* @param {Object} params
645+
* @param {number} params.start Starting block to fetch.
646+
* @param {number} params.end Ending block to fetch. This cannot be more than 50 blocks ahead of the start block.
637647
* @returns {Promise<Array<BlockJSON>>} An array of block objects
638648
*
639649
* @example
640650
* import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
641651
*
642652
* // Fetch 50 blocks.
643653
* const (start, end) = (2050, 2100);
644-
* const blockRange = networkClient.getBlockRange(start, end);
654+
* const blockRange = networkClient.getBlockRange({ start, end });
645655
*
646656
* let cursor = start;
647657
* blockRange.forEach((block) => {
648658
* assert(block.height == cursor);
649659
* cursor += 1;
650660
* }
651661
*/
652-
async getBlockRange(start: number, end: number): Promise<Array<BlockJSON>> {
662+
async getBlockRange(params: { start: number, end: number }): Promise<Array<BlockJSON>> {
663+
const { start, end } = params;
664+
653665
try {
654666
this.ctx = { "X-ALEO-METHOD": "getBlockRange" };
655667
return await this.fetchData<Array<BlockJSON>>(
@@ -1301,7 +1313,10 @@ class AleoNetworkClient {
13011313
* const networkClient = new AleoNetworkClient({ host: "http://api.explorer.provable.com/v1" });
13021314
*
13031315
* // Get the balance of an account from either an address object or address string.
1304-
* const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
1316+
* const account = Account.fromCiphertext({
1317+
* ciphertext: process.env.ciphertext,
1318+
* password: process.env.password,
1319+
* });
13051320
* const publicBalance = await networkClient.getPublicBalance(account.address());
13061321
* const publicBalanceFromString = await networkClient.getPublicBalance(account.address().to_string());
13071322
* assert(publicBalance === publicBalanceFromString);
@@ -1693,7 +1708,10 @@ class AleoNetworkClient {
16931708
* const programManager = new ProgramManager(networkClient);
16941709
*
16951710
* // Set the account for the program manager.
1696-
* programManager.setAccount(Account.fromCiphertext(process.env.ciphertext, process.env.password));
1711+
* programManager.setAccount(Account.fromCiphertext({
1712+
* ciphertext: process.env.ciphertext,
1713+
* password: process.env.password,
1714+
* }));
16971715
*
16981716
* // Build a transfer transaction.
16991717
* const tx = await programManager.buildTransferPublicTransaction({

sdk/src/program-manager.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -973,12 +973,12 @@ class ProgramManager {
973973
try {
974974
feeRecord = privateFee
975975
? <RecordPlaintext>(
976-
await this.getCreditsRecord(
977-
priorityFee,
978-
[],
979-
feeRecord,
980-
recordSearchParams,
981-
)
976+
await this.getCreditsRecord({
977+
amount: priorityFee,
978+
nonces: [],
979+
record: feeRecord,
980+
params: recordSearchParams,
981+
})
982982
)
983983
: undefined;
984984
} catch (e: any) {

sdk/tests/account.test.ts

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,14 @@ describe('Account', () => {
7575
const privateKeyCiphertextString = privateKeyCiphertext.toString();
7676

7777
// Generate account from valid private key string
78-
const accountFromString = Account.fromCiphertext(privateKeyCiphertextString, "mypassword");
79-
const accountFromObject = Account.fromCiphertext(privateKeyCiphertext, "mypassword");
78+
const accountFromString = Account.fromCiphertext({
79+
ciphertext: privateKeyCiphertextString,
80+
password: "mypassword",
81+
});
82+
const accountFromObject = Account.fromCiphertext({
83+
ciphertext: privateKeyCiphertext,
84+
password: "mypassword",
85+
});
8086

8187
for (const account of [accountFromString, accountFromObject]) {
8288
// Test that expected output is generated
@@ -93,8 +99,14 @@ describe('Account', () => {
9399
const privateKeyCiphertextString = privateKeyCiphertext.toString();
94100

95101
try {
96-
Account.fromCiphertext(privateKeyCiphertextString, "badpassword");
97-
Account.fromCiphertext(privateKeyCiphertext, "badpassword");
102+
Account.fromCiphertext({
103+
ciphertext: privateKeyCiphertextString,
104+
password: "badpassword",
105+
});
106+
Account.fromCiphertext({
107+
ciphertext: privateKeyCiphertext,
108+
password: "badpassword",
109+
});
98110

99111
// Should not get here
100112
expect(true).equal(false);
@@ -173,11 +185,11 @@ describe('Account', () => {
173185
expect(sign_spy.callCount).equal(2);
174186

175187
const verify_spy = sinon.spy(account._address, 'verify');
176-
const isValid = account.verify(message, signature);
188+
const isValid = account.verify({ message, signature });
177189
expect(verify_spy.calledWith(message, signature)).equal(true);
178-
const isSigValidForWrongMessage = account.verify(other_message, signature);
190+
const isSigValidForWrongMessage = account.verify({ message: other_message, signature });
179191
expect(verify_spy.calledWith(other_message, signature)).equal(true);
180-
const isSigValidForMultipleMessages = account.verify(other_message, other_signature);
192+
const isSigValidForMultipleMessages = account.verify({ message: other_message, signature: other_signature });
181193
expect(verify_spy.calledWith(other_message, other_signature)).equal(true);
182194
expect(verify_spy.callCount).equal(3);
183195
// Ensure the signature was valid

sdk/tests/network-client.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ describe("NodeConnection", () => {
7272

7373
describe("getBlockRange", () => {
7474
it.skip("should return an array of Block objects", async () => {
75-
const blockRange = await connection.getBlockRange(1, 3);
75+
const blockRange = await connection.getBlockRange({ start: 1, end: 3 });
7676
expect(Array.isArray(blockRange)).equal(true);
7777
expect((blockRange as BlockJSON[]).length).equal(3);
7878
expect(

0 commit comments

Comments
 (0)