-
Notifications
You must be signed in to change notification settings - Fork 14
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
bump viem #139
Merged
Merged
bump viem #139
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
51c4f2b
bump viem version
andrewkmin 2816599
add getAddresses() test
andrewkmin 552b253
add contract and simulate examples
andrewkmin 7b6acea
add private key checks
andrewkmin da7c960
add changeset
andrewkmin a5a2843
prettier
andrewkmin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@turnkey/viem": patch | ||
--- | ||
|
||
Bump Viem dependency to fix `getAddresses()` for LocalAccount |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import * as path from "path"; | ||
import * as dotenv from "dotenv"; | ||
|
||
import { createAccount } from "@turnkey/viem"; | ||
import { TurnkeyClient } from "@turnkey/http"; | ||
import { ApiKeyStamper } from "@turnkey/api-key-stamper"; | ||
import { | ||
createWalletClient, | ||
createPublicClient, | ||
http, | ||
type Account, | ||
} from "viem"; | ||
import { goerli } from "viem/chains"; | ||
import { print } from "./util"; | ||
import { createNewEthereumPrivateKey } from "./createNewEthereumPrivateKey"; | ||
import WETH_TOKEN_ABI from "./weth-contract-abi.json"; | ||
const WETH_TOKEN_ADDRESS_GOERLI = "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6"; | ||
|
||
// Load environment variables from `.env.local` | ||
dotenv.config({ path: path.resolve(process.cwd(), ".env.local") }); | ||
|
||
async function main() { | ||
if (!process.env.PRIVATE_KEY_ID) { | ||
// If you don't specify a `PRIVATE_KEY_ID`, we'll create one for you via calling the Turnkey API. | ||
await createNewEthereumPrivateKey(); | ||
return; | ||
} | ||
|
||
const turnkeyClient = new TurnkeyClient( | ||
{ | ||
baseUrl: process.env.BASE_URL!, | ||
}, | ||
new ApiKeyStamper({ | ||
apiPublicKey: process.env.API_PUBLIC_KEY!, | ||
apiPrivateKey: process.env.API_PRIVATE_KEY!, | ||
}) | ||
); | ||
|
||
const turnkeyAccount = await createAccount({ | ||
client: turnkeyClient, | ||
organizationId: process.env.ORGANIZATION_ID!, | ||
privateKeyId: process.env.PRIVATE_KEY_ID!, | ||
}); | ||
|
||
const client = createWalletClient({ | ||
account: turnkeyAccount as Account, | ||
chain: goerli, | ||
transport: http( | ||
`https://goerli.infura.io/v3/${process.env.INFURA_API_KEY!}` | ||
), | ||
}); | ||
|
||
const address = client.account.address; | ||
print("Address:", address); | ||
|
||
const publicClient = createPublicClient({ | ||
transport: http("https://rpc.ankr.com/eth_goerli"), | ||
chain: goerli, | ||
}); | ||
|
||
const { request } = await publicClient.simulateContract({ | ||
abi: WETH_TOKEN_ABI, | ||
address: WETH_TOKEN_ADDRESS_GOERLI, | ||
functionName: "deposit", | ||
chain: goerli, | ||
value: 1n, | ||
account: client.account, | ||
}); | ||
|
||
const hash = await client.writeContract(request); | ||
|
||
print( | ||
"Successfully wrapped ETH 🥳. Transaction:", | ||
`https://goerli.etherscan.io/tx/${hash}` | ||
); | ||
} | ||
|
||
main().catch((error) => { | ||
console.error(error); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { | ||
TurnkeyClient, | ||
createActivityPoller, | ||
TurnkeyActivityError, | ||
} from "@turnkey/http"; | ||
import { ApiKeyStamper } from "@turnkey/api-key-stamper"; | ||
import * as crypto from "crypto"; | ||
|
||
export async function createNewEthereumPrivateKey() { | ||
console.log("creating a new Ethereum private key on Turnkey...\n"); | ||
|
||
const privateKeyName = `ETH Key ${crypto.randomBytes(2).toString("hex")}`; | ||
|
||
try { | ||
const turnkeyClient = new TurnkeyClient( | ||
{ baseUrl: process.env.BASE_URL! }, | ||
new ApiKeyStamper({ | ||
apiPublicKey: process.env.API_PUBLIC_KEY!, | ||
apiPrivateKey: process.env.API_PRIVATE_KEY!, | ||
}) | ||
); | ||
|
||
const activityPoller = createActivityPoller({ | ||
client: turnkeyClient, | ||
requestFn: turnkeyClient.createPrivateKeys, | ||
}); | ||
|
||
const completedActivity = await activityPoller({ | ||
type: "ACTIVITY_TYPE_CREATE_PRIVATE_KEYS_V2", | ||
timestampMs: String(Date.now()), | ||
organizationId: process.env.ORGANIZATION_ID!, | ||
parameters: { | ||
privateKeys: [ | ||
{ | ||
privateKeyName, | ||
curve: "CURVE_SECP256K1", | ||
addressFormats: ["ADDRESS_FORMAT_ETHEREUM"], | ||
privateKeyTags: [], | ||
}, | ||
], | ||
}, | ||
}); | ||
|
||
const privateKey = refineNonNull( | ||
completedActivity.result.createPrivateKeysResultV2?.privateKeys?.[0] | ||
); | ||
const privateKeyId = refineNonNull(privateKey.privateKeyId); | ||
const address = refineNonNull(privateKey.addresses?.[0]?.address); | ||
|
||
// Success! | ||
console.log( | ||
[ | ||
`New Ethereum private key created!`, | ||
`- Name: ${privateKeyName}`, | ||
`- Private key ID: ${privateKeyId}`, | ||
`- Address: ${address}`, | ||
``, | ||
"Now you can take the private key ID, put it in `.env.local`, then re-run the script.", | ||
].join("\n") | ||
); | ||
} catch (error) { | ||
// If needed, you can read from `TurnkeyActivityError` to find out why the activity didn't succeed | ||
if (error instanceof TurnkeyActivityError) { | ||
throw error; | ||
} | ||
|
||
throw new TurnkeyActivityError({ | ||
message: "Failed to create a new Ethereum private key", | ||
cause: error as Error, | ||
}); | ||
} | ||
} | ||
|
||
export function refineNonNull<T>( | ||
input: T | null | undefined, | ||
errorMessage?: string | ||
): T { | ||
if (input == null) { | ||
throw new Error(errorMessage ?? `Unexpected ${JSON.stringify(input)}`); | ||
} | ||
|
||
return input; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
[ | ||
{ | ||
"constant": true, | ||
"inputs": [], | ||
"name": "name", | ||
"outputs": [{ "name": "", "type": "string" }], | ||
"payable": false, | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"constant": false, | ||
"inputs": [ | ||
{ "name": "guy", "type": "address" }, | ||
{ "name": "wad", "type": "uint256" } | ||
], | ||
"name": "approve", | ||
"outputs": [{ "name": "", "type": "bool" }], | ||
"payable": false, | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
}, | ||
{ | ||
"constant": true, | ||
"inputs": [], | ||
"name": "totalSupply", | ||
"outputs": [{ "name": "", "type": "uint256" }], | ||
"payable": false, | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"constant": false, | ||
"inputs": [ | ||
{ "name": "src", "type": "address" }, | ||
{ "name": "dst", "type": "address" }, | ||
{ "name": "wad", "type": "uint256" } | ||
], | ||
"name": "transferFrom", | ||
"outputs": [{ "name": "", "type": "bool" }], | ||
"payable": false, | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
}, | ||
{ | ||
"constant": false, | ||
"inputs": [{ "name": "wad", "type": "uint256" }], | ||
"name": "withdraw", | ||
"outputs": [], | ||
"payable": false, | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
}, | ||
{ | ||
"constant": true, | ||
"inputs": [], | ||
"name": "decimals", | ||
"outputs": [{ "name": "", "type": "uint8" }], | ||
"payable": false, | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"constant": true, | ||
"inputs": [{ "name": "", "type": "address" }], | ||
"name": "balanceOf", | ||
"outputs": [{ "name": "", "type": "uint256" }], | ||
"payable": false, | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"constant": true, | ||
"inputs": [], | ||
"name": "symbol", | ||
"outputs": [{ "name": "", "type": "string" }], | ||
"payable": false, | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"constant": false, | ||
"inputs": [ | ||
{ "name": "dst", "type": "address" }, | ||
{ "name": "wad", "type": "uint256" } | ||
], | ||
"name": "transfer", | ||
"outputs": [{ "name": "", "type": "bool" }], | ||
"payable": false, | ||
"stateMutability": "nonpayable", | ||
"type": "function" | ||
}, | ||
{ | ||
"constant": false, | ||
"inputs": [], | ||
"name": "deposit", | ||
"outputs": [], | ||
"payable": true, | ||
"stateMutability": "payable", | ||
"type": "function" | ||
}, | ||
{ | ||
"constant": true, | ||
"inputs": [ | ||
{ "name": "", "type": "address" }, | ||
{ "name": "", "type": "address" } | ||
], | ||
"name": "allowance", | ||
"outputs": [{ "name": "", "type": "uint256" }], | ||
"payable": false, | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ "payable": true, "stateMutability": "payable", "type": "fallback" }, | ||
{ | ||
"anonymous": false, | ||
"inputs": [ | ||
{ "indexed": true, "name": "src", "type": "address" }, | ||
{ "indexed": true, "name": "guy", "type": "address" }, | ||
{ "indexed": false, "name": "wad", "type": "uint256" } | ||
], | ||
"name": "Approval", | ||
"type": "event" | ||
}, | ||
{ | ||
"anonymous": false, | ||
"inputs": [ | ||
{ "indexed": true, "name": "src", "type": "address" }, | ||
{ "indexed": true, "name": "dst", "type": "address" }, | ||
{ "indexed": false, "name": "wad", "type": "uint256" } | ||
], | ||
"name": "Transfer", | ||
"type": "event" | ||
}, | ||
{ | ||
"anonymous": false, | ||
"inputs": [ | ||
{ "indexed": true, "name": "dst", "type": "address" }, | ||
{ "indexed": false, "name": "wad", "type": "uint256" } | ||
], | ||
"name": "Deposit", | ||
"type": "event" | ||
}, | ||
{ | ||
"anonymous": false, | ||
"inputs": [ | ||
{ "indexed": true, "name": "src", "type": "address" }, | ||
{ "indexed": false, "name": "wad", "type": "uint256" } | ||
], | ||
"name": "Withdrawal", | ||
"type": "event" | ||
} | ||
] |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
note to self: do this in our separate repo https://github.com/tkhq/demo-viem-passkeys once this is version bump is released