45 lines
935 B
TypeScript
45 lines
935 B
TypeScript
/**
|
|
* MessageHash Value Object
|
|
*
|
|
* Hash of message to be signed.
|
|
*/
|
|
|
|
export class MessageHash {
|
|
private readonly _hash: Buffer;
|
|
|
|
private constructor(hash: Buffer) {
|
|
this._hash = hash;
|
|
}
|
|
|
|
get bytes(): Buffer {
|
|
return Buffer.from(this._hash);
|
|
}
|
|
|
|
static create(hash: Buffer): MessageHash {
|
|
if (!hash || hash.length !== 32) {
|
|
throw new Error('Message hash must be 32 bytes');
|
|
}
|
|
return new MessageHash(hash);
|
|
}
|
|
|
|
static fromHex(hex: string): MessageHash {
|
|
const cleanHex = hex.replace('0x', '');
|
|
if (cleanHex.length !== 64) {
|
|
throw new Error('Message hash must be 32 bytes (64 hex characters)');
|
|
}
|
|
return MessageHash.create(Buffer.from(cleanHex, 'hex'));
|
|
}
|
|
|
|
toHex(): string {
|
|
return '0x' + this._hash.toString('hex');
|
|
}
|
|
|
|
equals(other: MessageHash): boolean {
|
|
return this._hash.equals(other._hash);
|
|
}
|
|
|
|
toString(): string {
|
|
return this.toHex();
|
|
}
|
|
}
|