Skip to content

Commit

Permalink
fix: add missing customizable prefixes
Browse files Browse the repository at this point in the history
  • Loading branch information
janniks committed Sep 29, 2022
1 parent e5f342d commit a1d385c
Showing 1 changed file with 5 additions and 6 deletions.
11 changes: 5 additions & 6 deletions packages/encryption/src/messageSignature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,18 @@ import { decode, encode, encodingLength } from 'varuint-bitcoin';
// 'Stacks Message Signing:\n'.length.toString(16) // = 18
const chainPrefix: string = '\x18Stacks Message Signing:\n';

export function hashMessage(message: string): Buffer {
return Buffer.from(sha256(encodeMessage(message)));
export function hashMessage(message: string, prefix: string = chainPrefix): Buffer {
return Buffer.from(sha256(encodeMessage(message, prefix)));
}

export function encodeMessage(message: string | Buffer, prefix: string = chainPrefix): Buffer {
const encoded = encode(Buffer.from(message).length);
return Buffer.concat([Buffer.from(prefix), encoded, Buffer.from(message)]);
}

export function decodeMessage(encodedMessage: Buffer): Buffer {
// Remove the chain prefix: 1 for the varint and 24 for the length of the string
// 'Stacks Message Signing:\n'
const messageWithoutChainPrefix = encodedMessage.subarray(1 + 24);
export function decodeMessage(encodedMessage: Buffer, prefix: string = chainPrefix): Buffer {
const prefixByteLength = Buffer.from(prefix).byteLength;
const messageWithoutChainPrefix = encodedMessage.subarray(prefixByteLength);
const decoded = decode(messageWithoutChainPrefix);
const varIntLength = encodingLength(decoded);
// Remove the varint prefix
Expand Down

1 comment on commit a1d385c

@vercel
Copy link

@vercel vercel bot commented on a1d385c Sep 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.