Skip to content

Commit 9493060

Browse files
committed
feat: add backward compatibility
1 parent c15a84e commit 9493060

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

sdk/src/client/signer.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff 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.

sdk/src/client/state.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Address, zeroAddress } from 'viem';
22
import * as Errors from '../errors';
33
import { getChannelId, getPackedChallengeState } from '../utils';
44
import { PreparerDependencies } from './prepare';
5-
import { StateSigner, WalletStateSigner } from './signer';
5+
import { AccountStateSigner, SessionKeyStateSigner, StateSigner, WalletStateSigner } from './signer';
66
import {
77
ChallengeChannelParams,
88
ChannelId,
@@ -199,8 +199,8 @@ async function _fetchParticipantAndGetSigner(deps: PreparerDependencies, channel
199199
*/
200200
function _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
}

0 commit comments

Comments
 (0)