Skip to content

Commit 1d69435

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

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
@@ -30,7 +30,10 @@ interface AleoNetworkClientOptions {
3030
* const localNetworkClient = new AleoNetworkClient({ host: "http://0.0.0.0:3030" });
3131
*
3232
* // Connection to a public beacon node
33-
* const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
33+
* const account = Account.fromCiphertext({
34+
* ciphertext: process.env.ciphertext,
35+
* password: process.env.password,
36+
* });
3437
* const publicNetworkClient = new AleoNetworkClient({ host: "http://api.explorer.provable.com/v1" });
3538
*/
3639
class AleoNetworkClient {
@@ -192,7 +195,10 @@ class AleoNetworkClient {
192195
* import { Account, AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
193196
*
194197
* // Import an account from a ciphertext and password.
195-
* const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
198+
* const account = Account.fromCiphertext({
199+
* ciphertext: process.env.ciphertext,
200+
* password: process.env.password,
201+
* });
196202
*
197203
* // Create a network client.
198204
* const networkClient = new AleoNetworkClient({ host: "http://api.explorer.provable.com/v1" });
@@ -298,7 +304,7 @@ class AleoNetworkClient {
298304
}
299305
try {
300306
// Get 50 blocks (or the difference between the start and end if less than 50)
301-
const blocks = await this.getBlockRange(start, end);
307+
const blocks = await this.getBlockRange({ start, end });
302308
end = start;
303309
// Iterate through blocks to find unspent records
304310
for (let i = 0; i < blocks.length; i++) {
@@ -527,7 +533,10 @@ class AleoNetworkClient {
527533
* @example
528534
* import { Account, AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
529535
*
530-
* const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
536+
* const account = Account.fromCiphertext({
537+
* ciphertext: process.env.ciphertext,
538+
* password: process.env.password,
539+
* });
531540
*
532541
* // Create a network client and set an account to search for records with.
533542
* const networkClient = new AleoNetworkClient({ host: "http://api.explorer.provable.com/v1" });
@@ -617,24 +626,27 @@ class AleoNetworkClient {
617626
/**
618627
* Returns a range of blocks between the specified block heights. A maximum of 50 blocks can be fetched at a time.
619628
*
620-
* @param {number} start Starting block to fetch.
621-
* @param {number} end Ending block to fetch. This cannot be more than 50 blocks ahead of the start block.
629+
* @param {Object} params
630+
* @param {number} params.start Starting block to fetch.
631+
* @param {number} params.end Ending block to fetch. This cannot be more than 50 blocks ahead of the start block.
622632
* @returns {Promise<Array<BlockJSON>>} An array of block objects
623633
*
624634
* @example
625635
* import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
626636
*
627637
* // Fetch 50 blocks.
628638
* const (start, end) = (2050, 2100);
629-
* const blockRange = networkClient.getBlockRange(start, end);
639+
* const blockRange = networkClient.getBlockRange({ start, end });
630640
*
631641
* let cursor = start;
632642
* blockRange.forEach((block) => {
633643
* assert(block.height == cursor);
634644
* cursor += 1;
635645
* }
636646
*/
637-
async getBlockRange(start: number, end: number): Promise<Array<BlockJSON>> {
647+
async getBlockRange(params: { start: number, end: number }): Promise<Array<BlockJSON>> {
648+
const { start, end } = params;
649+
638650
try {
639651
this.ctx = { "X-ALEO-METHOD": "getBlockRange" };
640652
return await this.fetchData<Array<BlockJSON>>(
@@ -1236,7 +1248,10 @@ class AleoNetworkClient {
12361248
* const networkClient = new AleoNetworkClient({ host: "http://api.explorer.provable.com/v1" });
12371249
*
12381250
* // Get the balance of an account from either an address object or address string.
1239-
* const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
1251+
* const account = Account.fromCiphertext({
1252+
* ciphertext: process.env.ciphertext,
1253+
* password: process.env.password,
1254+
* });
12401255
* const publicBalance = await networkClient.getPublicBalance(account.address());
12411256
* const publicBalanceFromString = await networkClient.getPublicBalance(account.address().to_string());
12421257
* assert(publicBalance === publicBalanceFromString);
@@ -1589,7 +1604,10 @@ class AleoNetworkClient {
15891604
* const programManager = new ProgramManager(networkClient);
15901605
*
15911606
* // Set the account for the program manager.
1592-
* programManager.setAccount(Account.fromCiphertext(process.env.ciphertext, process.env.password));
1607+
* programManager.setAccount(Account.fromCiphertext({
1608+
* ciphertext: process.env.ciphertext,
1609+
* password: process.env.password,
1610+
* }));
15931611
*
15941612
* // Build a transfer transaction.
15951613
* const tx = await programManager.buildTransferPublicTransaction({

sdk/src/program-manager.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -927,12 +927,12 @@ class ProgramManager {
927927
try {
928928
feeRecord = privateFee
929929
? <RecordPlaintext>(
930-
await this.getCreditsRecord(
931-
priorityFee,
932-
[],
933-
feeRecord,
934-
recordSearchParams,
935-
)
930+
await this.getCreditsRecord({
931+
amount: priorityFee,
932+
nonces: [],
933+
record: feeRecord,
934+
params: recordSearchParams,
935+
})
936936
)
937937
: undefined;
938938
} 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
@@ -71,7 +71,7 @@ describe("NodeConnection", () => {
7171

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

0 commit comments

Comments
 (0)