Skip to content
Open
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
162 changes: 100 additions & 62 deletions sdk/src/program-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ interface ExecuteOptions {
program?: string | Program;
imports?: ProgramImports;
edition?: number,
skipProof?: boolean,
}

/**
Expand Down Expand Up @@ -316,6 +317,7 @@ class ProgramManager {
* @param {RecordSearchParams | undefined} recordSearchParams Optional parameters for searching for a record to use pay the deployment fee
* @param {string | RecordPlaintext | undefined} feeRecord Optional Fee record to use for the transaction
* @param {PrivateKey | undefined} privateKey Optional private key to use for the transaction
* @param {boolean | undefined} skipCertificate Whether to skip proof generation and key sysnthesis in the deployment transaction
* @returns {string} The transaction id of the deployed program or a failure message from the network
*
* @example
Expand Down Expand Up @@ -352,6 +354,7 @@ class ProgramManager {
recordSearchParams?: RecordSearchParams,
feeRecord?: string | RecordPlaintext,
privateKey?: PrivateKey,
skipCertificate?: boolean,
): Promise<Transaction> {
// Ensure the program is valid.
let programObject;
Expand Down Expand Up @@ -412,6 +415,28 @@ class ProgramManager {
);
}

// Resolve the program imports if they exist
let imports;
try {
imports = await this.networkClient.getProgramImports(program);
} catch (e: any) {
logAndThrow(
`Error finding program imports. Network response: '${e.message}'. Please ensure you're connected to a valid Aleo network and the program is deployed to the network.`,
);
}
if (skipCertificate === undefined) {
skipCertificate = false;
}
if (skipCertificate === true) {
return await WasmProgramManager.buildDevnodeDeploymentTransaction(
deploymentPrivateKey,
program,
priorityFee,
feeRecord,
this.host,
imports,
);
}
// Get the proving and verifying keys from the key provider
let feeKeys;
try {
Expand All @@ -425,16 +450,6 @@ class ProgramManager {
}
const [feeProvingKey, feeVerifyingKey] = feeKeys;

// Resolve the program imports if they exist
let imports;
try {
imports = await this.networkClient.getProgramImports(program);
} catch (e: any) {
logAndThrow(
`Error finding program imports. Network response: '${e.message}'. Please ensure you're connected to a valid Aleo network and the program is deployed to the network.`,
);
}

// Build a deployment transaction
return await WasmProgramManager.buildDeploymentTransaction(
deploymentPrivateKey,
Expand Down Expand Up @@ -580,6 +595,12 @@ class ProgramManager {
let programName = options.programName;
let imports = options.imports;
let edition = options.edition;
let skipProof = options.skipProof;

// Skip proof defaults to false.
if (typeof skipProof === "undefined") {
skipProof = false;
}

// Ensure the function exists on the network
if (program === undefined) {
Expand Down Expand Up @@ -640,32 +661,6 @@ class ProgramManager {
);
}

// Get the fee proving and verifying keys from the key provider
let feeKeys;
try {
feeKeys = privateFee
? <FunctionKeyPair>await this.keyProvider.feePrivateKeys()
: <FunctionKeyPair>await this.keyProvider.feePublicKeys();
} catch (e: any) {
logAndThrow(
`Error finding fee keys. Key finder response: '${e.message}'. Please ensure your key provider is configured correctly.`,
);
}
const [feeProvingKey, feeVerifyingKey] = feeKeys;

// If the function proving and verifying keys are not provided, attempt to find them using the key provider
if (!provingKey || !verifyingKey) {
try {
[provingKey, verifyingKey] = <FunctionKeyPair>(
await this.keyProvider.functionKeys(keySearchParams)
);
} catch (e) {
console.log(
`Function keys not found. Key finder response: '${e}'. The function keys will be synthesized`,
);
}
}

// Resolve the program imports if they exist
const numberOfImports = Program.fromString(program).getImports().length;
if (numberOfImports > 0 && !imports) {
Expand All @@ -679,35 +674,78 @@ class ProgramManager {
);
}
}

if (offlineQuery && !this.inclusionKeysLoaded) {
if (skipProof === true) {
console.log("program is: {}", program);
// Build a transaction without a proof
return await WasmProgramManager.buildDevnodeExecutionTransaction(
executionPrivateKey,
program,
functionName,
inputs,
priorityFee,
feeRecord,
this.host,
imports,
offlineQuery,
edition
);
}

// Get the fee proving and verifying keys from the key provider
let feeKeys;
try {
const inclusionKeys = await this.keyProvider.inclusionKeys();
WasmProgramManager.loadInclusionProver(inclusionKeys[0])
this.inclusionKeysLoaded = true;
console.log("Successfully loaded inclusion key");
} catch {
logAndThrow(`Inclusion key bytes not loaded, please ensure the program manager is initialized with a KeyProvider that includes the inclusion key.`)
feeKeys = privateFee
? <FunctionKeyPair>await this.keyProvider.feePrivateKeys()
: <FunctionKeyPair>await this.keyProvider.feePublicKeys();
} catch (e: any) {
logAndThrow(
`Error finding fee keys. Key finder response: '${e.message}'. Please ensure your key provider is configured correctly.`,
);
}
const [feeProvingKey, feeVerifyingKey] = feeKeys;

// If the function proving and verifying keys are not provided, attempt to find them using the key provider
if (!provingKey || !verifyingKey) {
try {
[provingKey, verifyingKey] = <FunctionKeyPair>(
await this.keyProvider.functionKeys(keySearchParams)
);
} catch (e) {
console.log(
`Function keys not found. Key finder response: '${e}'. The function keys will be synthesized`,
);
}
}
}

// Build an execution transaction
return await WasmProgramManager.buildExecutionTransaction(
executionPrivateKey,
program,
functionName,
inputs,
priorityFee,
feeRecord,
this.host,
imports,
provingKey,
verifyingKey,
feeProvingKey,
feeVerifyingKey,
offlineQuery,
edition
);
if (offlineQuery && !this.inclusionKeysLoaded) {
try {
const inclusionKeys = await this.keyProvider.inclusionKeys();
WasmProgramManager.loadInclusionProver(inclusionKeys[0])
this.inclusionKeysLoaded = true;
console.log("Successfully loaded inclusion key");
} catch {
logAndThrow(`Inclusion key bytes not loaded, please ensure the program manager is initialized with a KeyProvider that includes the inclusion key.`)
}
}

// Build an execution transaction
return await WasmProgramManager.buildExecutionTransaction(
executionPrivateKey,
program,
functionName,
inputs,
priorityFee,
feeRecord,
this.host,
imports,
provingKey,
verifyingKey,
feeProvingKey,
feeVerifyingKey,
offlineQuery,
edition
);

}

/**
Expand Down
Loading