-
Notifications
You must be signed in to change notification settings - Fork 174
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into feat/safari-support
- Loading branch information
Showing
5 changed files
with
64 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/extension/src/providers/bitcoin/libs/sign-message-utils.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { sha256 } from "ethereumjs-util"; | ||
|
||
const MAGIC_BYTES = Buffer.from("Bitcoin Signed Message:\n"); | ||
|
||
const varintBufNum = (n: number) => { | ||
let buf; | ||
if (n < 253) { | ||
buf = Buffer.alloc(1); | ||
buf.writeUInt8(n, 0); | ||
} else if (n < 0x10000) { | ||
buf = Buffer.alloc(1 + 2); | ||
buf.writeUInt8(253, 0); | ||
buf.writeUInt16LE(n, 1); | ||
} else if (n < 0x100000000) { | ||
buf = Buffer.alloc(1 + 4); | ||
buf.writeUInt8(254, 0); | ||
buf.writeUInt32LE(n, 1); | ||
} else { | ||
buf = Buffer.alloc(1 + 8); | ||
buf.writeUInt8(255, 0); | ||
buf.writeInt32LE(n & -1, 1); | ||
buf.writeUInt32LE(Math.floor(n / 0x100000000), 5); | ||
} | ||
return buf; | ||
}; | ||
|
||
export const magicHash = (messageBuffer: Buffer) => { | ||
const prefix1 = varintBufNum(MAGIC_BYTES.length); | ||
const prefix2 = varintBufNum(messageBuffer.length); | ||
const buf = Buffer.concat([prefix1, MAGIC_BYTES, prefix2, messageBuffer]); | ||
return sha256(sha256(buf)); | ||
}; | ||
|
||
export const toCompact = ( | ||
i: number, | ||
signature: Uint8Array, | ||
compressed: boolean | ||
) => { | ||
if (!(i === 0 || i === 1 || i === 2 || i === 3)) { | ||
throw new Error("i must be equal to 0, 1, 2, or 3"); | ||
} | ||
let val = i + 27 + 4; | ||
if (!compressed) { | ||
val = val - 4; | ||
} | ||
return Buffer.concat([Uint8Array.of(val), Uint8Array.from(signature)]); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38ad2b3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Virus total analysis
chrome:
https://www.virustotal.com/gui/file/ccd3f84a022f26c83b56bee89a01288cb64c1c5acea21da20dfb2ff02b9dae2e
firefox:
https://www.virustotal.com/gui/file/c8937a0e25ca67e0cd8a9836815b669ee47c72235bd6ab2c7ba56c02c5c4f5dd