Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Update] Update SDK methods to support latest credits.aleo method signatures #891

Merged
merged 6 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 14 additions & 10 deletions create-aleo-app/template-offline-public-transaction-ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ async function buildTransferPublicTxOffline(recipientAddress: Address, amount: n
// Create the proving keys from the key bytes on the offline machine
console.log("Creating proving keys from local key files");
const feePublicKeyBytes = await getLocalKey(<string>keyPaths[CREDITS_PROGRAM_KEYS.fee_public.locator]);
const transferPublicKeyBytes = await getLocalKey(<string>keyPaths[CREDITS_PROGRAM_KEYS.transfer_public.locator]);
const transferPublicAsSignerKeyBytes = await getLocalKey(<string>keyPaths[CREDITS_PROGRAM_KEYS.transfer_public_as_signer.locator]);
const feePublicProvingKey = ProvingKey.fromBytes(feePublicKeyBytes);
const transferPublicProvingKey = ProvingKey.fromBytes(transferPublicKeyBytes);
const transferPublicProvingKey = ProvingKey.fromBytes(transferPublicAsSignerKeyBytes);

// Create an offline key provider
console.log("Creating offline key provider");
Expand All @@ -37,7 +37,7 @@ async function buildTransferPublicTxOffline(recipientAddress: Address, amount: n

// Build tne transfer_public transaction offline
console.log("Building transfer transaction offline");
return programManager.buildTransferPublicTransaction(
return programManager.buildTransferPublicAsSignerTransaction(
amount,
recipientAddress.to_string(),
0.28,
Expand All @@ -47,7 +47,7 @@ async function buildTransferPublicTxOffline(recipientAddress: Address, amount: n
}

/// Build bonding and unbonding transactions without connection to the internet
async function buildBondingTxOffline(recipientAddress: Address, amount: number, latestStateRoot: string, keyPaths: {}): Promise<Error | Transaction[]> {
async function buildBondingTxOffline(stakerAddress: Address, validatorAddress: Address, withdrawalAddress: Address, amount: number, latestStateRoot: string, keyPaths: {}): Promise<Error | Transaction[]> {
// Create an offline program manager
const programManager = new ProgramManager();

Expand Down Expand Up @@ -93,7 +93,9 @@ async function buildBondingTxOffline(recipientAddress: Address, amount: number,
}

const bondTx = <Transaction>await programManager.buildBondPublicTransaction(
recipientAddress.to_string(),
stakerAddress.to_string(),
iamalwaysuncomfortable marked this conversation as resolved.
Show resolved Hide resolved
validatorAddress.to_string(),
withdrawalAddress.to_string(),
amount,
bondPublicOptions,
)
Expand All @@ -109,7 +111,7 @@ async function buildBondingTxOffline(recipientAddress: Address, amount: number,
}
}

const unBondTx = <Transaction>await programManager.buildUnbondPublicTransaction(amount, unbondPublicOptions);
const unBondTx = <Transaction>await programManager.buildUnbondPublicTransaction(stakerAddress.to_string(), amount, unbondPublicOptions);
console.log("\nunbond_public transaction built!\n");

console.log("Building a claim_unbond_public transaction offline")
Expand All @@ -123,7 +125,7 @@ async function buildBondingTxOffline(recipientAddress: Address, amount: number,
}
}

const claimUnbondTx = <Transaction>await programManager.buildClaimUnbondPublicTransaction(claimUnbondPublicOptions);
const claimUnbondTx = <Transaction>await programManager.buildClaimUnbondPublicTransaction(stakerAddress.to_string(), claimUnbondPublicOptions);
console.log("\nclaim_unbond_public transaction built!\n");
return [bondTx, unBondTx, claimUnbondTx];
}
Expand All @@ -142,14 +144,16 @@ const bondingKeyPaths = await preDownloadBondingKeys();
const latestStateRoot = "sr1p93gpsezrjzdhcd2wujznx5s07k8qa39t6vfcej35zew8vn2jyrs46te8q";

// Build a transfer_public transaction
const recipientAddress = new Account().address();
const transferTx = await buildTransferPublicTxOffline(recipientAddress, 100, latestStateRoot, transferKeyPaths);
const stakerAddress = new Account().address();
const validatorAddress = new Account().address();
const withdrawalAddress = new Account().address();
const transferTx = await buildTransferPublicTxOffline(stakerAddress, 10000, latestStateRoot, transferKeyPaths);
console.log("Transfer transaction built offline!");
console.log(`\n---------------transfer_public transaction---------------\n${transferTx}`);
console.log(`---------------------------------------------------------`);

// Build bonding & unbonding transactions
const bondTransactions = await buildBondingTxOffline(recipientAddress, 100, latestStateRoot, bondingKeyPaths);
const bondTransactions = await buildBondingTxOffline(stakerAddress, validatorAddress, withdrawalAddress, 100, latestStateRoot, bondingKeyPaths);
console.log("Bonding transactions built offline!");
console.log(`\n-----------------bond_public transaction-----------------\n${bondTransactions[0]}`);
console.log(`---------------------------------------------------------`);
Expand Down
4 changes: 2 additions & 2 deletions create-aleo-app/template-react-ts/src/workers/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ async function deployProgram(program) {
keyProvider.useCache(true);

// Create a record provider that will be used to find records and transaction data for Aleo programs
const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");

// Use existing account with funds
const account = new Account({
Expand All @@ -49,7 +49,7 @@ async function deployProgram(program) {

// Initialize a program manager to talk to the Aleo network with the configured key and record providers
const programManager = new ProgramManager(
"https://vm.aleo.org/api",
"https://api.explorer.aleo.org/v1",
keyProvider,
recordProvider,
);
Expand Down
6 changes: 3 additions & 3 deletions sdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1095,7 +1095,7 @@ read the value of a specific key within a mapping.
```typescript
import { AleoNetworkClient } from '@aleo/sdk';

const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
const creditsMappings = networkClient.getMappings("credits.aleo");
assert(creditsMappings === ["account"]);

Expand Down Expand Up @@ -1142,13 +1142,13 @@ import { Account, ProgramManager, AleoKeyProvider, NetworkRecordProvider, AleoNe

// Create a new NetworkClient, KeyProvider, and RecordProvider
const account = Account.from_string({privateKey: "user1PrivateKey"});
const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
const keyProvider = new AleoKeyProvider();
const recordProvider = new NetworkRecordProvider(account, networkClient);

// Initialize a program manager with the key provider to automatically fetch keys for executions
const RECIPIENT_ADDRESS = "user1Address";
const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
programManager.setAccount(account);

// Update or initialize a public balance
Expand Down
36 changes: 30 additions & 6 deletions sdk/src/function-key-provider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { ProvingKey, VerifyingKey, CREDITS_PROGRAM_KEYS, KEY_STORE, PRIVATE_TRANSFER, PRIVATE_TO_PUBLIC_TRANSFER, PUBLIC_TRANSFER, PUBLIC_TO_PRIVATE_TRANSFER} from "./index";
import {
ProvingKey,
VerifyingKey,
CREDITS_PROGRAM_KEYS,
KEY_STORE,
PRIVATE_TRANSFER,
PRIVATE_TO_PUBLIC_TRANSFER,
PUBLIC_TRANSFER,
PUBLIC_TO_PRIVATE_TRANSFER,
PUBLIC_TRANSFER_AS_SIGNER
} from "./index";
import { get } from "./utils";

type FunctionKeyPair = [ProvingKey, VerifyingKey];
Expand Down Expand Up @@ -52,6 +62,12 @@ interface FunctionKeyProvider {
*/
bondPublicKeys(): Promise<FunctionKeyPair | Error>;

/**
* Get bond_validator function keys from the credits.aleo program
*
* @returns {Promise<FunctionKeyPair | Error>} Proving and verifying keys for the bond_validator function
*/
bondValidatorKeys(): Promise<FunctionKeyPair | Error>;

/**
* Cache a set of keys. This will overwrite any existing keys with the same keyId. The user can check if a keyId
Expand Down Expand Up @@ -351,7 +367,7 @@ class AleoKeyProvider implements FunctionKeyProvider {
* const recordProvider = new NetworkRecordProvider(account, networkClient);
*
* // Initialize a program manager with the key provider to automatically fetch keys for value transfers
* const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
* const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
*
* // Keys can also be fetched manually
Expand Down Expand Up @@ -394,6 +410,10 @@ class AleoKeyProvider implements FunctionKeyProvider {
return this.fetchKeys(CREDITS_PROGRAM_KEYS.bond_public.prover, CREDITS_PROGRAM_KEYS.bond_public.verifier, CREDITS_PROGRAM_KEYS.bond_public.locator)
}

bondValidatorKeys(): Promise<FunctionKeyPair | Error> {
return this.fetchKeys(CREDITS_PROGRAM_KEYS.bond_validator.prover, CREDITS_PROGRAM_KEYS.bond_validator.verifier, CREDITS_PROGRAM_KEYS.bond_validator.locator)
}

claimUnbondPublicKeys(): Promise<FunctionKeyPair | Error> {
return this.fetchKeys(CREDITS_PROGRAM_KEYS.claim_unbond_public.prover, CREDITS_PROGRAM_KEYS.claim_unbond_public.verifier, CREDITS_PROGRAM_KEYS.claim_unbond_public.locator)
}
Expand All @@ -405,12 +425,12 @@ class AleoKeyProvider implements FunctionKeyProvider {
*
* @example
* // Create a new AleoKeyProvider
* const networkClient = new AleoNetworkClient("https://vm.aleo.org/api");
* const networkClient = new AleoNetworkClient("https://api.explorer.aleo.org/v1");
* const keyProvider = new AleoKeyProvider();
* const recordProvider = new NetworkRecordProvider(account, networkClient);
*
* // Initialize a program manager with the key provider to automatically fetch keys for value transfers
* const programManager = new ProgramManager("https://vm.aleo.org/api", keyProvider, recordProvider);
* const programManager = new ProgramManager("https://api.explorer.aleo.org/v1", keyProvider, recordProvider);
* programManager.transfer(1, "aleo166q6ww6688cug7qxwe7nhctjpymydwzy2h7rscfmatqmfwnjvggqcad0at", "public", 0.5);
*
* // Keys can also be fetched manually
Expand All @@ -423,6 +443,8 @@ class AleoKeyProvider implements FunctionKeyProvider {
return await this.fetchKeys(CREDITS_PROGRAM_KEYS.transfer_private_to_public.prover, CREDITS_PROGRAM_KEYS.transfer_private_to_public.verifier, CREDITS_PROGRAM_KEYS.transfer_private_to_public.locator);
} else if (PUBLIC_TRANSFER.has(visibility)) {
return await this.fetchKeys(CREDITS_PROGRAM_KEYS.transfer_public.prover, CREDITS_PROGRAM_KEYS.transfer_public.verifier, CREDITS_PROGRAM_KEYS.transfer_public.locator);
} else if (PUBLIC_TRANSFER_AS_SIGNER.has(visibility)) {
return await this.fetchKeys(CREDITS_PROGRAM_KEYS.transfer_public_as_signer.prover, CREDITS_PROGRAM_KEYS.transfer_public_as_signer.verifier, CREDITS_PROGRAM_KEYS.transfer_public_as_signer.locator);
} else if (PUBLIC_TO_PRIVATE_TRANSFER.has(visibility)) {
return await this.fetchKeys(CREDITS_PROGRAM_KEYS.transfer_public_to_private.prover, CREDITS_PROGRAM_KEYS.transfer_public_to_private.verifier, CREDITS_PROGRAM_KEYS.transfer_public_to_private.locator);
} else {
Expand Down Expand Up @@ -476,6 +498,8 @@ class AleoKeyProvider implements FunctionKeyProvider {
switch (verifierUri) {
case CREDITS_PROGRAM_KEYS.bond_public.verifier:
return CREDITS_PROGRAM_KEYS.bond_public.verifyingKey();
case CREDITS_PROGRAM_KEYS.bond_validator.verifier:
return CREDITS_PROGRAM_KEYS.bond_validator.verifyingKey();
case CREDITS_PROGRAM_KEYS.claim_unbond_public.verifier:
return CREDITS_PROGRAM_KEYS.claim_unbond_public.verifyingKey();
case CREDITS_PROGRAM_KEYS.fee_private.verifier:
Expand All @@ -496,10 +520,10 @@ class AleoKeyProvider implements FunctionKeyProvider {
return CREDITS_PROGRAM_KEYS.transfer_private_to_public.verifyingKey();
case CREDITS_PROGRAM_KEYS.transfer_public.verifier:
return CREDITS_PROGRAM_KEYS.transfer_public.verifyingKey();
case CREDITS_PROGRAM_KEYS.transfer_public_as_signer.verifier:
return CREDITS_PROGRAM_KEYS.transfer_public_as_signer.verifyingKey();
case CREDITS_PROGRAM_KEYS.transfer_public_to_private.verifier:
return CREDITS_PROGRAM_KEYS.transfer_public_to_private.verifyingKey();
case CREDITS_PROGRAM_KEYS.unbond_delegator_as_validator.verifier:
return CREDITS_PROGRAM_KEYS.unbond_delegator_as_validator.verifyingKey();
case CREDITS_PROGRAM_KEYS.unbond_public.verifier:
return CREDITS_PROGRAM_KEYS.unbond_public.verifyingKey();
default:
Expand Down
13 changes: 12 additions & 1 deletion sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ function convert(metadata: Metadata): Key {

const CREDITS_PROGRAM_KEYS = {
bond_public: convert(Metadata.bond_public()),
bond_validator: convert(Metadata.bond_validator()),
claim_unbond_public: convert(Metadata.claim_unbond_public()),
fee_private: convert(Metadata.fee_private()),
fee_public: convert(Metadata.fee_public()),
Expand All @@ -37,8 +38,8 @@ const CREDITS_PROGRAM_KEYS = {
transfer_private: convert(Metadata.transfer_private()),
transfer_private_to_public: convert(Metadata.transfer_private_to_public()),
transfer_public: convert(Metadata.transfer_public()),
transfer_public_as_signer: convert(Metadata.transfer_public_as_signer()),
transfer_public_to_private: convert(Metadata.transfer_public_to_private()),
unbond_delegator_as_validator: convert(Metadata.unbond_delegator_as_validator()),
unbond_public: convert(Metadata.unbond_public()),
};

Expand All @@ -58,10 +59,14 @@ const VALID_TRANSFER_TYPES = new Set([
"privateToPublic",
"transferPrivateToPublic",
"transfer_public",
"transfer_public_as_signer",
"public",
"public_as_signer",
"transferPublic",
"transferPublicAsSigner",
"transfer_public_to_private",
"publicToPrivate",
"publicAsSigner",
"transferPublicToPrivate",
]);
const PRIVATE_TRANSFER = new Set([
Expand All @@ -80,6 +85,11 @@ const PUBLIC_TRANSFER = new Set([
"transfer_public",
"transferPublic",
]);
const PUBLIC_TRANSFER_AS_SIGNER = new Set([
"public_as_signer",
"transfer_public_as_signer",
"transferPublicAsSigner",
]);
const PUBLIC_TO_PRIVATE_TRANSFER = new Set([
"public_to_private",
"publicToPrivate",
Expand Down Expand Up @@ -181,6 +191,7 @@ export {
PRIVATE_TO_PUBLIC_TRANSFER,
PRIVATE_TRANSFER_TYPES,
PUBLIC_TRANSFER,
PUBLIC_TRANSFER_AS_SIGNER,
PUBLIC_TO_PRIVATE_TRANSFER,
VALID_TRANSFER_TYPES,
logAndThrow,
Expand Down
Loading
Loading