File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -62,6 +62,42 @@ export class WalletStateSigner implements StateSigner {
6262 }
6363}
6464
65+ /**
66+ * Implementation of the StateSigner interface using a viem Account.
67+ * This class uses the account to sign states and raw messages.
68+ * It is suitable for use in scenarios where the account is available and can sign messages,
69+ * e.g. signing with a private key or other account providers.
70+ */
71+ export class AccountStateSigner implements StateSigner {
72+ private readonly account : Account ;
73+
74+ constructor ( account : Account ) {
75+ this . account = account ;
76+ }
77+
78+ getAddress ( ) : Address {
79+ return this . account . address ;
80+ }
81+
82+ async signState ( channelId : Hex , state : State ) : Promise < Hex > {
83+ if ( ! this . account . signMessage ) {
84+ throw new Error ( 'Account does not support message signing' ) ;
85+ }
86+
87+ const packedState = getPackedState ( channelId , state ) ;
88+
89+ return this . account . signMessage ( { message : { raw : packedState } } ) ;
90+ }
91+
92+ async signRawMessage ( message : Hex ) : Promise < Hex > {
93+ if ( ! this . account . signMessage ) {
94+ throw new Error ( 'Account does not support message signing' ) ;
95+ }
96+
97+ return this . account . signMessage ( { message : { raw : message } } ) ;
98+ }
99+ }
100+
65101/**
66102 * Implementation of the StateSigner interface using a session key.
67103 * This class uses a session key to sign states and raw messages.
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import { Address, zeroAddress } from 'viem';
22import * as Errors from '../errors' ;
33import { getChannelId , getPackedChallengeState } from '../utils' ;
44import { PreparerDependencies } from './prepare' ;
5- import { StateSigner , WalletStateSigner } from './signer' ;
5+ import { AccountStateSigner , SessionKeyStateSigner , StateSigner , WalletStateSigner } from './signer' ;
66import {
77 ChallengeChannelParams ,
88 ChannelId ,
@@ -199,8 +199,8 @@ async function _fetchParticipantAndGetSigner(deps: PreparerDependencies, channel
199199 */
200200function _checkParticipantAndGetSigner ( deps : PreparerDependencies , participant : Address ) : StateSigner {
201201 let signer = deps . stateSigner ;
202- // if (participant == deps.account.address) {
203- // signer = new WalletStateSigner (deps.account);
204- // }
202+ if ( participant == deps . account . address && signer instanceof SessionKeyStateSigner ) {
203+ signer = new AccountStateSigner ( deps . account ) ;
204+ }
205205 return signer ;
206206}
You can’t perform that action at this time.
0 commit comments