|
7 | 7 | Address as SequenceAddress, |
8 | 8 | Signature as SequenceSignature, |
9 | 9 | } from '@0xsequence/wallet-primitives' |
10 | | -import { AbiFunction, Address, Bytes, Hex, Provider } from 'ox' |
| 10 | +import { AbiFunction, Address, Bytes, Hex, Provider, TypedData } from 'ox' |
11 | 11 | import * as Envelope from './envelope.js' |
12 | 12 | import * as State from './state/index.js' |
13 | 13 |
|
@@ -343,6 +343,41 @@ export class Wallet { |
343 | 343 | } |
344 | 344 | } |
345 | 345 |
|
| 346 | + async prepareMessageSignature( |
| 347 | + message: string | Hex.Hex | Payload.TypedDataToSign, |
| 348 | + chainId: bigint, |
| 349 | + ): Promise<Envelope.Envelope<Payload.Message>> { |
| 350 | + let encodedMessage: Hex.Hex |
| 351 | + if (typeof message !== 'string') { |
| 352 | + encodedMessage = TypedData.encode(message) |
| 353 | + } else { |
| 354 | + let hexMessage = Hex.validate(message) ? message : Hex.fromString(message) |
| 355 | + const messageSize = Hex.size(hexMessage) |
| 356 | + encodedMessage = Hex.concat(Hex.fromString(`${`\x19Ethereum Signed Message:\n${messageSize}`}`), hexMessage) |
| 357 | + } |
| 358 | + return { |
| 359 | + ...(await this.prepareBlankEnvelope(chainId)), |
| 360 | + payload: Payload.fromMessage(encodedMessage), |
| 361 | + } |
| 362 | + } |
| 363 | + |
| 364 | + async buildMessageSignature( |
| 365 | + envelope: Envelope.Signed<Payload.Message>, |
| 366 | + provider?: Provider.Provider, |
| 367 | + ): Promise<Bytes.Bytes> { |
| 368 | + const status = await this.getStatus(provider) |
| 369 | + const signature = Envelope.encodeSignature(envelope) |
| 370 | + if (!status.isDeployed) { |
| 371 | + const deployTransaction = await this.buildDeployTransaction() |
| 372 | + signature.erc6492 = { to: deployTransaction.to, data: Bytes.fromHex(deployTransaction.data) } |
| 373 | + } |
| 374 | + const encoded = SequenceSignature.encodeSignature({ |
| 375 | + ...signature, |
| 376 | + suffix: status.pendingUpdates.map(({ signature }) => signature), |
| 377 | + }) |
| 378 | + return encoded |
| 379 | + } |
| 380 | + |
346 | 381 | private async prepareBlankEnvelope(chainId: bigint) { |
347 | 382 | const status = await this.getStatus() |
348 | 383 |
|
|
0 commit comments