Skip to content

Commit

Permalink
more ergonomic client input
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewkmin committed Sep 11, 2023
1 parent dcbf133 commit 228f173
Show file tree
Hide file tree
Showing 13 changed files with 109 additions and 82 deletions.
10 changes: 5 additions & 5 deletions examples/deployer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ async function main() {
);

// Initialize a Turnkey Signer
const turnkeySigner = new TurnkeySigner(
turnkeyClient,
process.env.ORGANIZATION_ID!,
process.env.PRIVATE_KEY_ID!
);
const turnkeySigner = new TurnkeySigner({
client: turnkeyClient,
organizationId: process.env.ORGANIZATION_ID!,
privateKeyId: process.env.PRIVATE_KEY_ID!,
});

// Connect it with a Provider (https://docs.ethers.org/v5/api/providers/)
const network = "goerli";
Expand Down
10 changes: 5 additions & 5 deletions examples/rebalancer/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ export function getTurnkeySigner(
);

// Initialize a Turnkey Signer
const turnkeySigner = new TurnkeySigner(
turnkeyClient,
process.env.ORGANIZATION_ID!,
privateKeyId
);
const turnkeySigner = new TurnkeySigner({
client: turnkeyClient,
organizationId: process.env.ORGANIZATION_ID!,
privateKeyId,
});

return turnkeySigner.connect(provider);
}
10 changes: 5 additions & 5 deletions examples/sweeper/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ export function getTurnkeySigner(
);

// Initialize a Turnkey Signer
const turnkeySigner = new TurnkeySigner(
turnkeyClient,
process.env.ORGANIZATION_ID!,
process.env.PRIVATE_KEY_ID!
);
const turnkeySigner = new TurnkeySigner({
client: turnkeyClient,
organizationId: process.env.ORGANIZATION_ID!,
privateKeyId: process.env.PRIVATE_KEY_ID!,
});

return turnkeySigner.connect(provider);
}
10 changes: 5 additions & 5 deletions examples/trading-runner/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ export function getTurnkeySigner(
);

// Initialize a Turnkey Signer
const turnkeySigner = new TurnkeySigner(
turnkeyClient,
process.env.ORGANIZATION_ID!,
privateKeyId
);
const turnkeySigner = new TurnkeySigner({
client: turnkeyClient,
organizationId: process.env.ORGANIZATION_ID!,
privateKeyId,
});

return turnkeySigner.connect(provider);
}
10 changes: 5 additions & 5 deletions examples/with-ethers-and-passkeys/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ export default function Home() {
throw new Error("sub-org id or private key not found");
}

const ethersSigner = new TurnkeySigner(
passkeyHttpClient,
subOrgId,
privateKey.id
);
const ethersSigner = new TurnkeySigner({
client: passkeyHttpClient,
organizationId: subOrgId,
privateKeyId: privateKey.id,
});

const signedMessage = await ethersSigner.signMessage(data.messageToSign);

Expand Down
10 changes: 5 additions & 5 deletions examples/with-ethers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ async function main() {
);

// Initialize a Turnkey Signer
const turnkeySigner = new TurnkeySigner(
turnkeyClient,
process.env.ORGANIZATION_ID!,
process.env.PRIVATE_KEY_ID!
);
const turnkeySigner = new TurnkeySigner({
client: turnkeyClient,
organizationId: process.env.ORGANIZATION_ID!,
privateKeyId: process.env.PRIVATE_KEY_ID!,
});

// Bring your own provider (such as Alchemy or Infura: https://docs.ethers.org/v5/api/providers/)
const network = "goerli";
Expand Down
10 changes: 5 additions & 5 deletions examples/with-ethers/src/legacySepolia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ async function main() {
);

// Initialize a Turnkey Signer
const turnkeySigner = new TurnkeySigner(
turnkeyClient,
process.env.ORGANIZATION_ID!,
process.env.PRIVATE_KEY_ID!
);
const turnkeySigner = new TurnkeySigner({
client: turnkeyClient,
organizationId: process.env.ORGANIZATION_ID!,
privateKeyId: process.env.PRIVATE_KEY_ID!,
});

// Bring your own provider (such as Alchemy or Infura: https://docs.ethers.org/v5/api/providers/)
const network = "sepolia";
Expand Down
30 changes: 15 additions & 15 deletions examples/with-gnosis/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,23 @@ async function main() {
);

// Initialize a Turnkey Signer
const turnkeySigner1 = new TurnkeySigner(
turnkeyClient,
process.env.ORGANIZATION_ID!,
process.env.PRIVATE_KEY_ID_1!
);
const turnkeySigner1 = new TurnkeySigner({
client: turnkeyClient,
organizationId: process.env.ORGANIZATION_ID!,
privateKeyId: process.env.PRIVATE_KEY_ID_1!,
});

const turnkeySigner2 = new TurnkeySigner(
turnkeyClient,
process.env.ORGANIZATION_ID!,
process.env.PRIVATE_KEY_ID_2!
);
const turnkeySigner2 = new TurnkeySigner({
client: turnkeyClient,
organizationId: process.env.ORGANIZATION_ID!,
privateKeyId: process.env.PRIVATE_KEY_ID_2!,
});

const turnkeySigner3 = new TurnkeySigner(
turnkeyClient,
process.env.ORGANIZATION_ID!,
process.env.PRIVATE_KEY_ID_3!
);
const turnkeySigner3 = new TurnkeySigner({
client: turnkeyClient,
organizationId: process.env.ORGANIZATION_ID!,
privateKeyId: process.env.PRIVATE_KEY_ID_3!,
});

// Bring your own provider (for the sake of this demo, we recommend using Sepolia + Infura)
const network = "sepolia";
Expand Down
10 changes: 5 additions & 5 deletions examples/with-nonce-manager/src/managedOptimistic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,11 @@ async function main() {
);

// Initialize a Turnkey Signer
const turnkeySigner = new TurnkeySigner(
turnkeyClient,
process.env.ORGANIZATION_ID!,
process.env.PRIVATE_KEY_ID!
);
const turnkeySigner = new TurnkeySigner({
client: turnkeyClient,
organizationId: process.env.ORGANIZATION_ID!,
privateKeyId: process.env.PRIVATE_KEY_ID!,
});

// Bring your own provider (such as Alchemy or Infura: https://docs.ethers.org/v5/api/providers/)
const network = "goerli";
Expand Down
10 changes: 5 additions & 5 deletions examples/with-nonce-manager/src/simpleSequential.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ async function main() {
);

// Initialize a Turnkey Signer
const turnkeySigner = new TurnkeySigner(
turnkeyClient,
process.env.ORGANIZATION_ID!,
process.env.PRIVATE_KEY_ID!
);
const turnkeySigner = new TurnkeySigner({
client: turnkeyClient,
organizationId: process.env.ORGANIZATION_ID!,
privateKeyId: process.env.PRIVATE_KEY_ID!,
});

// Bring your own provider (such as Alchemy or Infura: https://docs.ethers.org/v5/api/providers/)
const network = "goerli";
Expand Down
10 changes: 5 additions & 5 deletions examples/with-uniswap/src/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,11 @@ export function getTurnkeySigner(
);

// Initialize a Turnkey Signer
const turnkeySigner = new TurnkeySigner(
turnkeyClient,
process.env.ORGANIZATION_ID!,
process.env.PRIVATE_KEY_ID!
);
const turnkeySigner = new TurnkeySigner({
client: turnkeyClient,
organizationId: process.env.ORGANIZATION_ID!,
privateKeyId: process.env.PRIVATE_KEY_ID!,
});

return turnkeySigner.connect(provider);
}
25 changes: 20 additions & 5 deletions packages/ethers/src/__tests__/index-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ const testCase: typeof test = (...argList) => {

describe("TurnkeySigner", () => {
let connectedSigner: TurnkeySigner;
let signerWithProvider: TurnkeySigner;
let chainId: number;
let expectedEthAddress: string;
let bannedToAddress: string;
Expand Down Expand Up @@ -76,11 +77,20 @@ describe("TurnkeySigner", () => {
})
);

connectedSigner = new TurnkeySigner(
turnkeyClient,
connectedSigner = new TurnkeySigner({
client: turnkeyClient,
organizationId,
privateKeyId
).connect(provider);
privateKeyId,
}).connect(provider);

signerWithProvider = new TurnkeySigner(
{
client: turnkeyClient,
organizationId,
privateKeyId,
},
provider
);

chainId = (await connectedSigner.provider!.getNetwork()).chainId;

Expand All @@ -89,11 +99,16 @@ describe("TurnkeySigner", () => {
setBalance(expectedEthAddress, ethers.utils.parseEther("999999"));
});

testCase("basics", async () => {
testCase("basics for connected signer", async () => {
expect(ethers.Signer.isSigner(connectedSigner)).toBe(true);
expect(await connectedSigner.getAddress()).toBe(expectedEthAddress);
});

testCase("basics for connected signer via constructor", async () => {
expect(ethers.Signer.isSigner(signerWithProvider)).toBe(true);
expect(await signerWithProvider.getAddress()).toBe(expectedEthAddress);
});

testCase("it signs transactions", async () => {
const tx = await connectedSigner.signTransaction({
to: "0x2Ad9eA1E677949a536A270CEC812D6e868C88108",
Expand Down
36 changes: 24 additions & 12 deletions packages/ethers/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,44 @@ import type {
TypedDataField,
} from "ethers";

type TConfig = {
/**
* Turnkey client
*/
client: TurnkeyClient;
/**
* Turnkey organization ID
*/
organizationId: string;
/**
* Turnkey private key ID
*/
privateKeyId: string;
};

export class TurnkeySigner extends ethers.Signer implements TypedDataSigner {
private readonly client: TurnkeyClient;

public readonly organizationId: string;
public readonly privateKeyId: string;

constructor(
client: TurnkeyClient,
organizationId: string,
privateKeyId: string,
provider?: ethers.providers.Provider
) {
constructor(config: TConfig, provider?: ethers.providers.Provider) {
super();

ethers.utils.defineReadOnly(this, "provider", provider);
this.client = client;
this.client = config.client;

this.organizationId = organizationId;
this.privateKeyId = privateKeyId;
this.organizationId = config.organizationId;
this.privateKeyId = config.privateKeyId;
}

connect(provider: ethers.providers.Provider): TurnkeySigner {
return new TurnkeySigner(
this.client,
this.organizationId,
this.privateKeyId,
{
client: this.client,
organizationId: this.organizationId,
privateKeyId: this.privateKeyId,
},
provider
);
}
Expand Down

0 comments on commit 228f173

Please sign in to comment.