-
Notifications
You must be signed in to change notification settings - Fork 36
clients/js: add viem compat wrapper #164
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,28 @@ | ||
| import { Cipher } from '../cipher.js'; | ||
|
|
||
| const SAPPHIRE_PROP = 'sapphire'; | ||
|
|
||
| export type SapphireAnnex = { | ||
| [SAPPHIRE_PROP]: { | ||
| cipher: Cipher; | ||
| }; | ||
| }; | ||
|
|
||
| export type Hooks<T> = { | ||
| [K in keyof T]?: T[K]; | ||
| }; | ||
|
|
||
| export function makeProxy<U extends object>( | ||
| upstream: U, | ||
| cipher: Cipher, | ||
| hooks: Hooks<U>, | ||
| ): U & SapphireAnnex { | ||
| return new Proxy(upstream, { | ||
| get(upstream, prop) { | ||
| if (prop === SAPPHIRE_PROP) return { cipher }; | ||
| if (prop in hooks) return Reflect.get(hooks, prop); | ||
| const value = Reflect.get(upstream, prop); | ||
| return typeof value === 'function' ? value.bind(upstream) : value; | ||
| }, | ||
| }) as U & SapphireAnnex; | ||
| } |
This file contains hidden or 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,110 @@ | ||
| import { | ||
| Chain, | ||
| EIP1193Provider, | ||
| Hex, | ||
| PublicClient, | ||
| WalletClient, | ||
| toBytes, | ||
| } from 'viem'; | ||
|
|
||
| import { | ||
| Cipher, | ||
| X25519DeoxysII, | ||
| fetchRuntimePublicKeyByChainId, | ||
| lazy as lazyCipher, | ||
| } from '../cipher.js'; | ||
| import { Hooks, makeProxy } from './utils.js'; | ||
|
|
||
| export function wrapPublicClient<U extends PublicClient>( | ||
| upstream: U, | ||
| overrides?: Partial<{ | ||
| cipher: Cipher; | ||
| transport: { request: EIP1193Provider['request'] }; | ||
| }>, | ||
| ): U { | ||
| const transport = overrides?.transport ?? upstream.transport; | ||
| if (!transport) | ||
| throw new Error( | ||
| 'unknown transport. please configure one on the wallet client or pass it as an override', | ||
| ); | ||
| upstream.transport.request; | ||
| const cipher = | ||
| overrides?.cipher ?? | ||
| lazyCipher(async () => { | ||
| const rtPubKey = await fetchRuntimePublicKey(transport, upstream.chain); | ||
| return X25519DeoxysII.ephemeral(rtPubKey); | ||
| }); | ||
| return makeProxy(upstream, cipher, { | ||
| async call(req) { | ||
|
Contributor
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. Noting that we |
||
| return upstream.call({ | ||
| ...req, | ||
| data: await cipher.encryptEncode(req.data), | ||
| }); | ||
| }, | ||
| } as Hooks<U>); | ||
| } | ||
|
|
||
| export function wrapWalletClient<U extends WalletClient>( | ||
| upstream: U, | ||
| overrides?: Partial<{ | ||
| cipher: Cipher; | ||
| transport: { request: EIP1193Provider['request'] }; | ||
| }>, | ||
| ): U { | ||
| const transport = overrides?.transport ?? upstream.transport; | ||
| if (!transport) | ||
| throw new Error( | ||
| 'unknown transport. please configure one on the wallet client or pass it as an override', | ||
| ); | ||
| upstream.transport.request; | ||
| const cipher = | ||
| overrides?.cipher ?? | ||
| lazyCipher(async () => { | ||
| const rtPubKey = await fetchRuntimePublicKey(transport, upstream.chain); | ||
| return X25519DeoxysII.ephemeral(rtPubKey); | ||
| }); | ||
|
|
||
| return makeProxy<U>(upstream, cipher, { | ||
| async sendTransaction(req) { | ||
| req.data = await cipher.encryptEncode(req.data); | ||
| return upstream.sendTransaction(req); | ||
| }, | ||
| async signTransaction(req) { | ||
| req.data = await cipher.encryptEncode(req.data); | ||
| return upstream.signTransaction(req); | ||
| }, | ||
| } as Hooks<U>); | ||
| } | ||
|
|
||
| export async function getDefaultCipher(pc: PublicClient): Promise<Cipher> { | ||
| return X25519DeoxysII.ephemeral( | ||
| await fetchRuntimePublicKey(pc.transport, pc.chain), | ||
| ); | ||
| } | ||
|
|
||
| async function fetchRuntimePublicKey( | ||
| { | ||
| request, | ||
| }: { | ||
| request: EIP1193Provider['request']; | ||
| }, | ||
| chain?: Chain, | ||
| ): Promise<Uint8Array> { | ||
| try { | ||
| const resp: any = await request({ | ||
| method: 'oasis_callDataPublicKey' as any, | ||
| args: [], | ||
| }); | ||
| if (resp && 'key' in resp) { | ||
| return toBytes(resp.key); | ||
| } | ||
| } catch (e: any) { | ||
| console.error( | ||
| 'failed to fetch runtime public key using upstream transport:', | ||
| e, | ||
| ); | ||
| } | ||
| if (!chain) | ||
| throw new Error('unable to fetch runtime public key. chain not provided'); | ||
| return fetchRuntimePublicKeyByChainId(chain.id); | ||
| } | ||
This file contains hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.
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.
Noting that this is consolidated/merged.