Skip to content

Commit

Permalink
sdk: add PaladinVerifier type
Browse files Browse the repository at this point in the history
Signed-off-by: Andrew Richardson <[email protected]>
  • Loading branch information
awrichar committed Dec 13, 2024
1 parent f4a838b commit 6c9f5c7
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 83 deletions.
54 changes: 35 additions & 19 deletions sdk/typescript/src/domains/noto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { IGroupInfo, IStateEncoded, TransactionType } from "../interfaces";
import PaladinClient from "../paladin";
import * as notoPrivateJSON from "./abis/INotoPrivate.json";
import { penteGroupABI } from "./pente";
import { PaladinVerifier } from "../verifier";

const DEFAULT_POLL_TIMEOUT = 10000;

Expand Down Expand Up @@ -38,7 +39,7 @@ export const notoConstructorABI = (
});

export interface NotoConstructorParams {
notary: string;
notary: PaladinVerifier;
hooks?: {
privateGroup?: IGroupInfo;
publicAddress?: string;
Expand All @@ -48,13 +49,13 @@ export interface NotoConstructorParams {
}

export interface NotoMintParams {
to: string;
to: PaladinVerifier;
amount: string | number;
data: string;
}

export interface NotoTransferParams {
to: string;
to: PaladinVerifier;
amount: string | number;
data: string;
}
Expand Down Expand Up @@ -89,14 +90,17 @@ export class NotoFactory {
return new NotoFactory(paladin, this.domain, this.options);
}

async newNoto(from: string, data: NotoConstructorParams) {
async newNoto(from: PaladinVerifier, data: NotoConstructorParams) {
const txID = await this.paladin.sendTransaction({
type: TransactionType.PRIVATE,
domain: this.domain,
abi: [notoConstructorABI(!!data.hooks)],
function: "",
from,
data,
from: from.lookup,
data: {
...data,
notary: data.notary.lookup,
},
});
const receipt = await this.paladin.pollForReceipt(
txID,
Expand Down Expand Up @@ -126,64 +130,76 @@ export class NotoInstance {
return new NotoInstance(paladin, this.address, this.options);
}

async mint(from: string, data: NotoMintParams) {
async mint(from: PaladinVerifier, data: NotoMintParams) {
const txID = await this.paladin.sendTransaction({
type: TransactionType.PRIVATE,
abi: notoPrivateJSON.abi,
function: "mint",
to: this.address,
from,
data,
from: from.lookup,
data: {
...data,
to: data.to.lookup,
},
});
return this.paladin.pollForReceipt(txID, this.options.pollTimeout);
}

async transfer(from: string, data: NotoTransferParams) {
async transfer(from: PaladinVerifier, data: NotoTransferParams) {
const txID = await this.paladin.sendTransaction({
type: TransactionType.PRIVATE,
abi: notoPrivateJSON.abi,
function: "transfer",
to: this.address,
from,
data,
from: from.lookup,
data: {
...data,
to: data.to.lookup,
},
});
return this.paladin.pollForReceipt(txID, this.options.pollTimeout);
}

async prepareTransfer(from: string, data: NotoTransferParams) {
async prepareTransfer(from: PaladinVerifier, data: NotoTransferParams) {
const txID = await this.paladin.prepareTransaction({
type: TransactionType.PRIVATE,
abi: notoPrivateJSON.abi,
function: "transfer",
to: this.address,
from,
data,
from: from.lookup,
data: {
...data,
to: data.to.lookup,
},
});
return this.paladin.pollForPreparedTransaction(
txID,
this.options.pollTimeout
);
}

async approveTransfer(from: string, data: NotoApproveTransferParams) {
async approveTransfer(
from: PaladinVerifier,
data: NotoApproveTransferParams
) {
const txID = await this.paladin.sendTransaction({
type: TransactionType.PRIVATE,
abi: notoPrivateJSON.abi,
function: "approveTransfer",
to: this.address,
from,
from: from.lookup,
data,
});
return this.paladin.pollForReceipt(txID, this.options.pollTimeout);
}

async burn(from: string, data: NotoBurnParams) {
async burn(from: PaladinVerifier, data: NotoBurnParams) {
const txID = await this.paladin.sendTransaction({
type: TransactionType.PRIVATE,
abi: notoPrivateJSON.abi,
function: "burn",
to: this.address,
from,
from: from.lookup,
data,
});
return this.paladin.pollForReceipt(txID, this.options.pollTimeout);
Expand Down
65 changes: 49 additions & 16 deletions sdk/typescript/src/domains/pente.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { randomBytes } from "crypto";
import { ethers } from "ethers";
import { IGroupInfo, TransactionType } from "../interfaces";
import {
IGroupInfo,
IGroupInfoUnresolved,
TransactionType,
} from "../interfaces";
import PaladinClient from "../paladin";
import * as penteJSON from "./abis/PentePrivacyGroup.json";
import { PaladinVerifier } from "../verifier";

const DEFAULT_POLL_TIMEOUT = 10000;

Expand Down Expand Up @@ -70,7 +75,7 @@ const privateCallABI = (
});

export interface PentePrivacyGroupParams {
group: IGroupInfo;
group: IGroupInfo | IGroupInfoUnresolved;
evmVersion: string;
endorsementType: string;
externalCallsEnabled: boolean;
Expand All @@ -86,6 +91,20 @@ export interface PenteApproveTransitionParams {
export const newGroupSalt = () =>
"0x" + Buffer.from(randomBytes(32)).toString("hex");

export const resolveGroup = (
group: IGroupInfo | IGroupInfoUnresolved
): IGroupInfo => {
const members: string[] = [];
for (const member of group.members) {
if (typeof member === "string") {
members.push(member);
} else {
members.push(member.lookup);
}
}
return { members, salt: group.salt };
};

export class PenteFactory {
private options: Required<PenteOptions>;

Expand All @@ -104,14 +123,17 @@ export class PenteFactory {
return new PenteFactory(paladin, this.domain, this.options);
}

async newPrivacyGroup(from: string, data: PentePrivacyGroupParams) {
async newPrivacyGroup(from: PaladinVerifier, data: PentePrivacyGroupParams) {
const txID = await this.paladin.sendTransaction({
type: TransactionType.PRIVATE,
domain: this.domain,
abi: [penteConstructorABI],
function: "",
from,
data,
from: from.lookup,
data: {
...data,
group: resolveGroup(data.group),
},
});
const receipt = await this.paladin.pollForReceipt(
txID,
Expand All @@ -121,7 +143,7 @@ export class PenteFactory {
? undefined
: new PentePrivacyGroup(
this.paladin,
data.group,
resolveGroup(data.group),
receipt.contractAddress,
this.options
);
Expand Down Expand Up @@ -155,7 +177,7 @@ export class PentePrivacyGroup {
async deploy<ConstructorParams>(
abi: ReadonlyArray<ethers.JsonFragment>,
bytecode: string,
from: string,
from: PaladinVerifier,
inputs: ConstructorParams
) {
const constructor = abi.find((entry) => entry.type === "constructor");
Expand All @@ -167,7 +189,7 @@ export class PentePrivacyGroup {
abi: [privateDeployABI(constructor.inputs ?? [])],
function: "deploy",
to: this.address,
from,
from: from.lookup,
data: { group: this.group, bytecode, inputs },
});
const receipt = await this.paladin.pollForReceipt(
Expand All @@ -179,7 +201,7 @@ export class PentePrivacyGroup {
}

async invoke<Params>(
from: string,
from: PaladinVerifier,
to: string,
methodAbi: ethers.JsonFragment,
inputs: Params
Expand All @@ -189,14 +211,14 @@ export class PentePrivacyGroup {
abi: [privateInvokeABI(methodAbi.name ?? "", methodAbi.inputs ?? [])],
function: "",
to: this.address,
from,
from: from.lookup,
data: { group: this.group, to, inputs },
});
return this.paladin.pollForReceipt(txID, this.options.pollTimeout);
}

async call<Params>(
from: string,
from: PaladinVerifier,
to: string,
methodAbi: ethers.JsonFragment,
inputs: Params
Expand All @@ -212,18 +234,21 @@ export class PentePrivacyGroup {
],
function: "",
to: this.address,
from,
from: from.lookup,
data: { group: this.group, to, inputs },
});
}

async approveTransition(from: string, data: PenteApproveTransitionParams) {
async approveTransition(
from: PaladinVerifier,
data: PenteApproveTransitionParams
) {
const txID = await this.paladin.sendTransaction({
type: TransactionType.PUBLIC,
abi: penteJSON.abi,
function: "approveTransition",
to: this.address,
from,
from: from.lookup,
data,
});
return this.paladin.pollForReceipt(txID, this.options.pollTimeout);
Expand All @@ -241,15 +266,23 @@ export abstract class PentePrivateContract<ConstructorParams> {
paladin: PaladinClient
): PentePrivateContract<ConstructorParams>;

async invoke<Params>(from: string, methodName: string, params: Params) {
async invoke<Params>(
from: PaladinVerifier,
methodName: string,
params: Params
) {
const method = this.abi.find((entry) => entry.name === methodName);
if (method === undefined) {
throw new Error(`Method '${methodName}' not found`);
}
return this.evm.invoke(from, this.address, method, params);
}

async call<Params>(from: string, methodName: string, params: Params) {
async call<Params>(
from: PaladinVerifier,
methodName: string,
params: Params
) {
const method = this.abi.find((entry) => entry.name === methodName);
if (method === undefined) {
throw new Error(`Method '${methodName}' not found`);
Expand Down
23 changes: 14 additions & 9 deletions sdk/typescript/src/domains/zeto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TransactionType } from "../interfaces";
import PaladinClient from "../paladin";
import { PaladinVerifier } from "../verifier";

const DEFAULT_POLL_TIMEOUT = 10000;

Expand Down Expand Up @@ -74,7 +75,7 @@ export interface ZetoTransferParams {
}

export interface ZetoTransfer {
to: string;
to: PaladinVerifier;
amount: string | number;
}

Expand All @@ -96,13 +97,13 @@ export class ZetoFactory {
return new ZetoFactory(paladin, this.domain, this.options);
}

async newZeto(from: string, data: ZetoConstructorParams) {
async newZeto(from: PaladinVerifier, data: ZetoConstructorParams) {
const txID = await this.paladin.sendTransaction({
type: TransactionType.PRIVATE,
domain: this.domain,
abi: [zetoConstructorABI],
function: "",
from,
from: from.lookup,
data,
});
const receipt = await this.paladin.pollForReceipt(
Expand Down Expand Up @@ -133,26 +134,30 @@ export class ZetoInstance {
return new ZetoInstance(paladin, this.address, this.options);
}

async mint(from: string, data: ZetoMintParams) {
async mint(from: PaladinVerifier, data: ZetoMintParams) {
const txID = await this.paladin.sendTransaction({
type: TransactionType.PRIVATE,
abi: zetoPrivateAbi,
function: "mint",
to: this.address,
from,
data,
from: from.lookup,
data: {
mints: data.mints.map((t) => ({ ...t, to: t.to.lookup })),
},
});
return this.paladin.pollForReceipt(txID, this.options.pollTimeout);
}

async transfer(from: string, data: ZetoTransferParams) {
async transfer(from: PaladinVerifier, data: ZetoTransferParams) {
const txID = await this.paladin.sendTransaction({
type: TransactionType.PRIVATE,
abi: zetoPrivateAbi,
function: "transfer",
to: this.address,
from,
data,
from: from.lookup,
data: {
transfers: data.transfers.map((t) => ({ ...t, to: t.to.lookup })),
},
});
return this.paladin.pollForReceipt(txID, this.options.pollTimeout);
}
Expand Down
1 change: 1 addition & 0 deletions sdk/typescript/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export default PaladinClient;

export * from "./interfaces/index";
export * from "./utils";
export * from "./verifier";
export * from "./domains/noto";
export * from "./domains/pente";
export * from "./domains/zeto";
Loading

0 comments on commit 6c9f5c7

Please sign in to comment.