-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
128 additions
and
98 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Buffer } from 'buffer'; | ||
|
||
export const fromBase64 = (text: string) => { | ||
return Buffer.from(text, 'base64').toString('ascii'); | ||
}; | ||
|
||
export const toBase64 = (text: string) => { | ||
return Buffer.from(text).toString('base64'); | ||
}; | ||
|
||
/** Removes 0x from hex */ | ||
export const parseHex = (hex: string): string => { | ||
if (hex.startsWith('0x')) { | ||
return hex.slice(2); | ||
} | ||
return hex; | ||
}; | ||
|
||
/** Converts hex to buffer */ | ||
export const hexToBuffer = (hex: string): Buffer => { | ||
return Buffer.from(parseHex(hex), 'hex'); | ||
}; | ||
|
||
/** | ||
* Converts DER signature to R and S | ||
* R and S are hex strings | ||
*/ | ||
export const derToRs = (derSignature: string): { r: string; s: string } => { | ||
/* | ||
DER signature format: | ||
0x30 <length total> 0x02 <length r> <r> 0x02 <length s> <s> | ||
*/ | ||
const derBuffer = hexToBuffer(derSignature); | ||
|
||
const rLen = derBuffer[3]!; | ||
const rOffset = 4; | ||
const rBuffer = derBuffer.slice(rOffset, rOffset + rLen); | ||
const sLen = derBuffer[5 + rLen]!; | ||
const sOffset = 6 + rLen; | ||
const sBuffer = derBuffer.slice(sOffset, sOffset + sLen); | ||
|
||
const r = rBuffer.toString('hex'); | ||
const s = sBuffer.toString('hex'); | ||
return { r, s }; | ||
}; |