Skip to content

Commit 654e62a

Browse files
committed
Changing fromCiphertext, verify, and getBlockRange to use object params
1 parent 57a306d commit 654e62a

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 {
@@ -225,7 +228,10 @@ class AleoNetworkClient {
225228
* import { Account, AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
226229
*
227230
* // Import an account from a ciphertext and password.
228-
* const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
231+
* const account = Account.fromCiphertext({
232+
* ciphertext: process.env.ciphertext,
233+
* password: process.env.password,
234+
* });
229235
*
230236
* // Create a network client.
231237
* const networkClient = new AleoNetworkClient({ host: "http://api.explorer.provable.com/v1" });
@@ -331,7 +337,7 @@ class AleoNetworkClient {
331337
}
332338
try {
333339
// Get 50 blocks (or the difference between the start and end if less than 50)
334-
const blocks = await this.getBlockRange(start, end);
340+
const blocks = await this.getBlockRange({ start, end });
335341
end = start;
336342
// Iterate through blocks to find unspent records
337343
for (let i = 0; i < blocks.length; i++) {
@@ -560,7 +566,10 @@ class AleoNetworkClient {
560566
* @example
561567
* import { Account, AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
562568
*
563-
* const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
569+
* const account = Account.fromCiphertext({
570+
* ciphertext: process.env.ciphertext,
571+
* password: process.env.password,
572+
* });
564573
*
565574
* // Create a network client and set an account to search for records with.
566575
* const networkClient = new AleoNetworkClient({ host: "http://api.explorer.provable.com/v1" });
@@ -650,24 +659,27 @@ class AleoNetworkClient {
650659
/**
651660
* Returns a range of blocks between the specified block heights. A maximum of 50 blocks can be fetched at a time.
652661
*
653-
* @param {number} start Starting block to fetch.
654-
* @param {number} end Ending block to fetch. This cannot be more than 50 blocks ahead of the start block.
662+
* @param {Object} params
663+
* @param {number} params.start Starting block to fetch.
664+
* @param {number} params.end Ending block to fetch. This cannot be more than 50 blocks ahead of the start block.
655665
* @returns {Promise<Array<BlockJSON>>} An array of block objects
656666
*
657667
* @example
658668
* import { AleoNetworkClient } from "@provablehq/sdk/mainnet.js";
659669
*
660670
* // Fetch 50 blocks.
661671
* const (start, end) = (2050, 2100);
662-
* const blockRange = networkClient.getBlockRange(start, end);
672+
* const blockRange = networkClient.getBlockRange({ start, end });
663673
*
664674
* let cursor = start;
665675
* blockRange.forEach((block) => {
666676
* assert(block.height == cursor);
667677
* cursor += 1;
668678
* }
669679
*/
670-
async getBlockRange(start: number, end: number): Promise<Array<BlockJSON>> {
680+
async getBlockRange(params: { start: number, end: number }): Promise<Array<BlockJSON>> {
681+
const { start, end } = params;
682+
671683
try {
672684
this.ctx = { "X-ALEO-METHOD": "getBlockRange" };
673685
return await this.fetchData<Array<BlockJSON>>(
@@ -1319,7 +1331,10 @@ class AleoNetworkClient {
13191331
* const networkClient = new AleoNetworkClient({ host: "http://api.explorer.provable.com/v1" });
13201332
*
13211333
* // Get the balance of an account from either an address object or address string.
1322-
* const account = Account.fromCiphertext(process.env.ciphertext, process.env.password);
1334+
* const account = Account.fromCiphertext({
1335+
* ciphertext: process.env.ciphertext,
1336+
* password: process.env.password,
1337+
* });
13231338
* const publicBalance = await networkClient.getPublicBalance(account.address());
13241339
* const publicBalanceFromString = await networkClient.getPublicBalance(account.address().to_string());
13251340
* assert(publicBalance === publicBalanceFromString);
@@ -1712,7 +1727,10 @@ class AleoNetworkClient {
17121727
* const programManager = new ProgramManager(networkClient);
17131728
*
17141729
* // Set the account for the program manager.
1715-
* programManager.setAccount(Account.fromCiphertext(process.env.ciphertext, process.env.password));
1730+
* programManager.setAccount(Account.fromCiphertext({
1731+
* ciphertext: process.env.ciphertext,
1732+
* password: process.env.password,
1733+
* }));
17161734
*
17171735
* // Build a transfer transaction.
17181736
* const tx = await programManager.buildTransferPublicTransaction({

sdk/src/program-manager.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -994,12 +994,12 @@ class ProgramManager {
994994
try {
995995
feeRecord = privateFee
996996
? <RecordPlaintext>(
997-
await this.getCreditsRecord(
998-
priorityFee,
999-
[],
1000-
feeRecord,
1001-
recordSearchParams,
1002-
)
997+
await this.getCreditsRecord({
998+
amount: priorityFee,
999+
nonces: [],
1000+
record: feeRecord,
1001+
params: recordSearchParams,
1002+
})
10031003
)
10041004
: undefined;
10051005
} 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)