Created
August 9, 2022 16:49
-
-
Save menduz/bef935e0b4278cdb39fa4b6c2a17c333 to your computer and use it in GitHub Desktop.
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
export function asnEcdsaSignatureParser(signature: Uint8Array): { | |
r: BigNumber | |
s: BigNumber | |
} { | |
// 30 — type tag indicating SEQUENCE | |
// 46 — length in octets of value that follows | |
// 02 — type tag indicating INTEGER | |
// 21 — length in octets of value that follows | |
// 00 b15232269023d8c65689f816996f6af874b484347f1e7a537df1230cb1b12d76 R | |
// 02 — type tag indicating INTEGER | |
// 21 — length in octets of value that follows | |
// 00 eeb5a6819db5d5f20b2ddd21ed887221e242be1e2659163f730dc7c5be470efc S | |
// 30 | |
// 45 | |
// 02 | |
// 21 | |
// 00 8c74d8eaa50b187ffde87fba506167d999e4502dbe7a50efd2f54a73d6491ef8 | |
// 02 | |
// 20 | |
// 2174bd5d457919d437cfe201101aba3b47f7362f5a4a577d0af09a2584596e8b | |
let position = 0 | |
function readByte() { | |
const r = signature[position] | |
position++ | |
return r | |
} | |
function readBytes(amount: number) { | |
const r = signature.slice(position, position + amount) | |
position += amount | |
return r | |
} | |
if (readByte() != 0x30) throw new Error('Signature is not a sequence') | |
const _remainingSize = readByte() | |
if (readByte() != 0x02) | |
throw new Error('First element of the sequence is not an INTEGER') | |
let len = readByte() | |
const r = readBytes(len) | |
if (readByte() != 0x02) | |
throw new Error('Second element of the sequence is not an INTEGER') | |
len = readByte() | |
const s = readBytes(len) | |
return { | |
r: new BigNumber(bytesToHex(r), 16), | |
s: new BigNumber(bytesToHex(s), 16), | |
} | |
} | |
export function asnEcdsaPubParser(signature: Uint8Array): { | |
r: BigNumber | |
s: BigNumber | |
} { | |
// 30 — type tag indicating SEQUENCE | |
// 46 — length in octets of value that follows | |
// 02 — type tag indicating INTEGER | |
// 21 — length in octets of value that follows | |
// 00 b15232269023d8c65689f816996f6af874b484347f1e7a537df1230cb1b12d76 R | |
// 02 — type tag indicating INTEGER | |
// 21 — length in octets of value that follows | |
// 00 eeb5a6819db5d5f20b2ddd21ed887221e242be1e2659163f730dc7c5be470efc S | |
// 30 -- SEQ | |
// 59 -- LEN | |
// 30 -- SEQ | |
// 13 | |
// 06 -- STR | |
// 07 -- STR LEN | |
// 2a8648ce3d020106082a8648ce3d0301070342000460d301059de1d62a6ea61d623b993ec8bbd3b3de1437bd6ab973777f43ffb69aecd85a761446ac33b9bb5d9db19432b7c1c100b354042284238f09e1729b50e8 | |
let position = 0 | |
function readByte() { | |
const r = signature[position] | |
position++ | |
return r | |
} | |
function readBytes(amount: number) { | |
const r = signature.slice(position, position + amount) | |
position += amount | |
return r | |
} | |
if (readByte() != 0x30) throw new Error('Signature is not a sequence') | |
const _remainingSize = readByte() | |
if (readByte() != 0x02) | |
throw new Error('First element of the sequence is not an INTEGER') | |
let len = readByte() | |
const r = readBytes(len) | |
if (readByte() != 0x02) | |
throw new Error('Second element of the sequence is not an INTEGER') | |
len = readByte() | |
const s = readBytes(len) | |
return { | |
r: new BigNumber(bytesToHex(r), 16), | |
s: new BigNumber(bytesToHex(s), 16), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment