-
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
set up viem tests #119
Merged
Merged
set up viem tests #119
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
e3604aa
updated viem tests
andrewkmin 99334a5
add ethers examples
andrewkmin b42da1f
remove contract artifacts, aside for one required for tests
andrewkmin d9b9252
in-repo e2e setup
andrewkmin 12001fa
nits: add fetch polyfill; prettify
andrewkmin 392c701
cleanup
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
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 |
---|---|---|
|
@@ -26,8 +26,21 @@ jobs: | |
- name: Typecheck | ||
run: pnpm run typecheck-all | ||
|
||
# Run Anvil node in background for e2e tests | ||
# - name: Anvil | ||
# run: | | ||
# anvil & | ||
|
||
- name: Test | ||
run: pnpm run test-all | ||
run: anvil & pnpm run test-all | ||
env: # Or as an environment variable | ||
API_PUBLIC_KEY: ${{ secrets.API_PUBLIC_KEY }} | ||
API_PRIVATE_KEY: ${{ secrets.API_PRIVATE_KEY }} | ||
BASE_URL: ${{ secrets.BASE_URL }} | ||
ORGANIZATION_ID: ${{ secrets.ORGANIZATION_ID }} | ||
PRIVATE_KEY_ID: ${{ secrets.PRIVATE_KEY_ID }} | ||
EXPECTED_ETH_ADDRESS: ${{ secrets.EXPECTED_ETH_ADDRESS }} | ||
BANNED_TO_ADDRESS: ${{ secrets.BANNED_TO_ADDRESS }} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think some of these could be public? |
||
|
||
- name: Prettier | ||
run: pnpm run prettier-all:check | ||
|
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,115 @@ | ||
import * as path from "path"; | ||
import * as dotenv from "dotenv"; | ||
|
||
// Load environment variables from `.env.local` | ||
dotenv.config({ path: path.resolve(process.cwd(), ".env.local") }); | ||
|
||
import { TurnkeySigner } from "@turnkey/ethers"; | ||
import { ethers } from "ethers"; | ||
import { TurnkeyClient } from "@turnkey/http"; | ||
import { ApiKeyStamper } from "@turnkey/api-key-stamper"; | ||
import { createNewEthereumPrivateKey } from "./createNewEthereumPrivateKey"; | ||
|
||
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!, | ||
}) | ||
); | ||
|
||
// Initialize a Turnkey Signer | ||
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"; | ||
const provider = new ethers.providers.InfuraProvider(network); | ||
const connectedSigner = turnkeySigner.connect(provider); | ||
const address = await connectedSigner.getAddress(); | ||
|
||
print("Address:", address); | ||
|
||
const baseMessage = "Hello Turnkey"; | ||
|
||
// 1. Sign a raw hex message | ||
const hexMessage = ethers.utils.hexlify( | ||
ethers.utils.toUtf8Bytes(baseMessage) | ||
); | ||
let signature = await connectedSigner.signMessage(hexMessage); | ||
let recoveredAddress = ethers.utils.verifyMessage(hexMessage, signature); | ||
|
||
print("Turnkey-powered signature - raw hex message:", `${signature}`); | ||
assertEqual(recoveredAddress, address); | ||
|
||
// 2. Sign a raw bytes message | ||
const bytesMessage = ethers.utils.toUtf8Bytes(baseMessage); | ||
signature = await connectedSigner.signMessage(bytesMessage); | ||
recoveredAddress = ethers.utils.verifyMessage(bytesMessage, signature); | ||
|
||
print("Turnkey-powered signature - raw bytes message:", `${signature}`); | ||
assertEqual(recoveredAddress, address); | ||
|
||
// 3. Sign typed data (EIP-712) | ||
const typedData = { | ||
types: { | ||
// Note that we do not need to include `EIP712Domain` as a type here, as Ethers will automatically inject it for us | ||
Person: [ | ||
{ name: "name", type: "string" }, | ||
{ name: "wallet", type: "address" }, | ||
], | ||
}, | ||
domain: { | ||
name: "EIP712 Test", | ||
version: "1", | ||
}, | ||
primaryType: "Person", | ||
message: { | ||
name: "Alice", | ||
wallet: "0x742d35Cc6634C0532925a3b844Bc454e4438f44e", | ||
}, | ||
}; | ||
|
||
signature = await connectedSigner.signTypedData( | ||
typedData.domain, | ||
typedData.types, | ||
typedData.message | ||
); | ||
|
||
recoveredAddress = ethers.utils.verifyTypedData( | ||
typedData.domain, | ||
typedData.types, | ||
typedData.message, | ||
signature | ||
); | ||
|
||
print("Turnkey-powered signature - typed data (EIP-712):", `${signature}`); | ||
assertEqual(recoveredAddress, address); | ||
} | ||
|
||
main().catch((error) => { | ||
console.error(error); | ||
process.exit(1); | ||
}); | ||
|
||
function print(header: string, body: string): void { | ||
console.log(`${header}\n\t${body}\n`); | ||
} | ||
|
||
function assertEqual<T>(left: T, right: T) { | ||
if (left !== right) { | ||
throw new Error(`${JSON.stringify(left)} !== ${JSON.stringify(right)}`); | ||
} | ||
} |
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,9 @@ | ||
export function print(header: string, body: string): void { | ||
console.log(`${header}\n\t${body}\n`); | ||
} | ||
|
||
export function assertEqual<T>(left: T, right: T) { | ||
if (left !== right) { | ||
throw new Error(`${JSON.stringify(left)} !== ${JSON.stringify(right)}`); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -18,6 +18,6 @@ | |
"dotenv": "^16.0.3", | ||
"fetch": "^1.1.0", | ||
"typescript": "5.1", | ||
"viem": "^1.5.3" | ||
"viem": "^1.10.0" | ||
} | ||
} |
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,8 @@ | ||
# If you want to run unit tests, populate these values and save this file as a new `.env` file | ||
API_PUBLIC_KEY="<Turnkey API Public Key (that starts with 02 or 03)>" | ||
API_PRIVATE_KEY="<Turnkey API Private Key>" | ||
BASE_URL="https://api.turnkey.com" | ||
ORGANIZATION_ID="<Turnkey organization ID>" | ||
PRIVATE_KEY_ID="<Turnkey (crypto) private key ID>" | ||
EXPECTED_ETH_ADDRESS="<Corresponding to the private key above>" | ||
BANNED_TO_ADDRESS="<0x...>" |
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 @@ | ||
src/__tests__/contracts/artifacts |
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,14 @@ | ||
// require("@nomiclabs/hardhat-ethers"); | ||
// require("hardhat-jest-plugin"); | ||
|
||
/** @type import('hardhat/config').HardhatUserConfig */ | ||
const config = { | ||
solidity: "0.8.17", | ||
paths: { | ||
sources: "./src/__tests__/contracts/source", | ||
artifacts: "./src/__tests__/contracts/artifacts", | ||
cache: "./.cache", | ||
}, | ||
}; | ||
|
||
module.exports = config; |
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
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.
Remove if not used?