-
Notifications
You must be signed in to change notification settings - Fork 28
feat: replace stateWalletClient with StateSigner #297
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
480e2ed
feat: replace stateWalletClient with StateSigner
MaxMoskalenko d4bf782
fix: add stateSigner support for integration tests
MaxMoskalenko 6ba7ebb
fix: resolve comments
MaxMoskalenko 0012a35
fix: move keccak256 to signRawECDSAMessage
MaxMoskalenko 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,92 @@ | ||
| import { Account, Address, Chain, Hex, ParseAccount, toHex, Transport, WalletClient } from 'viem'; | ||
| import { State } from './types'; | ||
| import { getPackedState, getStateHash } from '../utils'; | ||
| import { signRawECDSAMessage } from '../utils/sign'; | ||
| import { privateKeyToAccount } from 'viem/accounts'; | ||
|
|
||
| // TODO: perhaps extend this interface with rpc signing methods and use it as universal signer interface | ||
|
|
||
| /** | ||
| * Interface for signing protocol states. | ||
| * This interface is used to abstract the signing logic for state updates in the Nitrolite SDK. | ||
| * It allows for different implementations, such as using a wallet client or a session key. | ||
| * Also implementation could include data packing/encoding, which is crucial for some signatures (EIP-712, EIP-191) | ||
| */ | ||
| export interface StateSigner { | ||
| /** | ||
| * Get the address of the signer. | ||
| * @returns The address of the signer. | ||
| */ | ||
| getAddress(): Address; | ||
| /** | ||
| * Sign a state for a given channel ID. | ||
| * @param channelId The ID of the channel. | ||
| * @param state The state to sign. | ||
| * @returns A Promise that resolves to the signature as a Hex string. | ||
| */ | ||
| signState(channelId: Hex, state: State): Promise<Hex>; | ||
| /** | ||
| * Sign a raw message. | ||
| * @param message The message to sign as a Hex string. | ||
| * @returns A Promise that resolves to the signature as a Hex string. | ||
| * @dev use viem's `toHex` to convert the message to Hex if needed. | ||
| */ | ||
| signRawMessage(message: Hex): Promise<Hex>; | ||
| } | ||
|
|
||
| /** | ||
| * Implementation of the StateSigner interface using a viem WalletClient. | ||
| * This class uses the wallet client to sign states and raw messages. | ||
| * It is suitable for use in scenarios where the wallet client is available and can sign messages, | ||
| * e.g. signing with MetaMask or other wallet providers. | ||
| */ | ||
| export class WalletStateSigner implements StateSigner { | ||
| private readonly walletClient: WalletClient<Transport, Chain, ParseAccount<Account>>; | ||
|
|
||
| constructor(walletClient: WalletClient<Transport, Chain, ParseAccount<Account>>) { | ||
| this.walletClient = walletClient; | ||
| } | ||
|
|
||
| getAddress(): Address { | ||
| return this.walletClient.account.address; | ||
| } | ||
|
|
||
| async signState(channelId: Hex, state: State): Promise<Hex> { | ||
| const packedState = getPackedState(channelId, state) | ||
|
|
||
| return this.walletClient.signMessage({ message: { raw: packedState } }); | ||
| } | ||
|
|
||
| async signRawMessage(message: Hex): Promise<Hex> { | ||
| return this.walletClient.signMessage({ message: { raw: message } }); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Implementation of the StateSigner interface using a session key. | ||
| * This class uses a session key to sign states and raw messages. | ||
| * It is suitable for scenarios where a session key is used for signing and private key could be exposed to application. | ||
| */ | ||
| export class SessionKeyStateSigner implements StateSigner { | ||
| private readonly sessionKey: Hex; | ||
nksazonov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| private readonly account: Account; | ||
|
|
||
| constructor(sessionKey: Hex) { | ||
| this.sessionKey = sessionKey; | ||
| this.account = privateKeyToAccount(sessionKey); | ||
| } | ||
|
|
||
| getAddress(): Address { | ||
| return this.account.address; | ||
| } | ||
|
|
||
| async signState(channelId: Hex, state: State): Promise<Hex> { | ||
| const packedState = getPackedState(channelId, state); | ||
|
|
||
| return signRawECDSAMessage(packedState, this.sessionKey); | ||
| } | ||
|
|
||
| async signRawMessage(message: Hex): Promise<Hex> { | ||
| return signRawECDSAMessage(message, this.sessionKey); | ||
| } | ||
| } | ||
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,9 @@ | ||
| import { Hex, keccak256 } from 'viem'; | ||
| import { privateKeyToAccount } from 'viem/accounts'; | ||
|
|
||
| export const signRawECDSAMessage = async (message: Hex, privateKey: Hex): Promise<Hex> => { | ||
| const hash = keccak256(message); | ||
| const flatSignature = await privateKeyToAccount(privateKey).sign({ hash }); | ||
|
|
||
| return flatSignature; | ||
| }; |
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.