This repository has been archived by the owner on Aug 16, 2024. It is now read-only.
-
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.
Merge pull request #6 from klaytn/lewis/fix-pubsig-process
Fix pubsig process
- Loading branch information
Showing
4 changed files
with
78 additions
and
68 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 |
---|---|---|
@@ -1,75 +1,92 @@ | ||
import { Buffer } from "buffer"; | ||
|
||
import base64url from "base64url"; | ||
import _ from "lodash"; | ||
|
||
function bufferToUints(buffer: Buffer, chunkLen: number): string[] { | ||
const result: string[] = []; | ||
_.map(_.chunk(buffer, chunkLen), (slice: Buffer) => { | ||
result.push(BigInt("0x" + Buffer.from(slice).toString("hex")).toString()); | ||
}); | ||
return result; | ||
const numBits = 248; | ||
const numBytes = numBits / 8; | ||
|
||
// fromXXX: convert from XXX type to buffer | ||
// toXXX: convert from buffer to XXX type | ||
|
||
export function fromASCII(str: string): Buffer { | ||
return Buffer.from(str, "ascii"); | ||
} | ||
|
||
function stretch(buf: Buffer, len: number): Buffer { | ||
if (buf.length < len) { | ||
return Buffer.concat([buf, Buffer.alloc(len - buf.length, 0)]); | ||
} | ||
return buf; | ||
export function fromBase64(str: string): Buffer { | ||
return base64url.toBuffer(str); | ||
} | ||
|
||
export function string2Uints(str: string | Buffer, maxLen: number): string[] { | ||
const numBits = 248; | ||
const numBytes = numBits / 8; | ||
export function fromHex(str: string): Buffer { | ||
return Buffer.from(str, "hex"); | ||
} | ||
|
||
export function fromUints(arr: string[]): Buffer { | ||
return Buffer.concat(arr.map((x) => Buffer.from(BigInt(x).toString(16), "hex"))); | ||
} | ||
|
||
let paddedStr = Buffer.from(str); | ||
export function fromQwords(arr: string[]): Buffer { | ||
return Buffer.from(_.reverse(_.map(arr, (x: any) => BigInt(x).toString(16).padStart(16, "0"))).join(""), "hex"); | ||
} | ||
|
||
// Pad the string to the max length | ||
paddedStr = stretch(paddedStr, maxLen); | ||
export function toASCII(buffer: Buffer): string { | ||
return buffer.toString("ascii"); | ||
} | ||
|
||
return bufferToUints(paddedStr, numBytes); | ||
export function toBase64(buffer: Buffer): string { | ||
return base64url(buffer); | ||
} | ||
|
||
export function uints2String(arr: string[]): string { | ||
return arr.map((x) => Buffer.from(BigInt(x).toString(16), "hex").toString("ascii")).join(""); | ||
export function toHex(buffer: Buffer): string { | ||
return buffer.toString("hex"); | ||
} | ||
|
||
export function uints2Buffer(arr: string[]): Buffer { | ||
return Buffer.concat(arr.map((x) => Buffer.from(BigInt(x).toString(16), "hex"))); | ||
export function toUints(buffer: Buffer, maxLen: number): string[] { | ||
const stretched = stretch(buffer, maxLen); | ||
const result: string[] = []; | ||
_.map(_.chunk(stretched, numBytes), (slice: Buffer) => { | ||
result.push(BigInt("0x" + Buffer.from(slice).toString("hex")).toString()); | ||
}); | ||
return result; | ||
} | ||
|
||
export function string2Qwords(str: string): string[] { | ||
const bi = str.startsWith("0x") ? BigInt(str) : BigInt("0x" + str); | ||
export function toQwords(buf: string | Buffer): string[] { | ||
const bi = BigInt("0x" + Buffer.from(buf).toString("hex")); | ||
return _.times(32, (i: any) => ((bi >> BigInt(i * 64)) & (2n ** 64n - 1n)).toString(10)); | ||
} | ||
|
||
export function qwords2String(arr: string[]): string { | ||
return Buffer.from( | ||
_.reverse(_.map(arr, (x: any) => BigInt(x).toString(16).padStart(16, "0"))).join(""), | ||
"hex" | ||
).toString("base64"); | ||
} | ||
// Misc helpers | ||
|
||
export function string2UintsSha256Padded(str: string | Buffer, maxLen: number): string[] { | ||
const numBits = 248; | ||
const numBytes = numBits / 8; | ||
// stretch: pad the buffer to the given length | ||
function stretch(buf: Buffer, len: number): Buffer { | ||
if (buf.length < len) { | ||
return Buffer.concat([buf, Buffer.alloc(len - buf.length, 0)]); | ||
} | ||
return buf; | ||
} | ||
|
||
// Add the SHA256 padding | ||
let paddedStr = Buffer.from(str); | ||
// sha256Pad: add SHA256 padding | ||
// [ string ][ 80 ][ 00..00 ][ len ] | ||
export function sha256Pad(str: string | Buffer): Buffer { | ||
let padded = Buffer.from(str); | ||
const blockSize = 64; // Block size in bytes | ||
|
||
paddedStr = Buffer.concat([paddedStr, Buffer.from([0x80])]); // Append a single '1' bit | ||
padded = Buffer.concat([padded, Buffer.from([0x80])]); // Append a single '1' bit | ||
|
||
const zeroBits = Buffer.alloc( | ||
(paddedStr.length + 8) % blockSize == 0 ? 0 : blockSize - ((paddedStr.length + 8) % blockSize) | ||
(padded.length + 8) % blockSize == 0 ? 0 : blockSize - ((padded.length + 8) % blockSize) | ||
); | ||
paddedStr = Buffer.concat([paddedStr, zeroBits]); // Append the '0' bits | ||
padded = Buffer.concat([padded, zeroBits]); // Append the '0' bits | ||
|
||
const lengthBits = Buffer.alloc(8); | ||
lengthBits.writeBigInt64BE(BigInt(str.length * 8), 0); | ||
paddedStr = Buffer.concat([paddedStr, lengthBits]); // Append the length | ||
padded = Buffer.concat([padded, lengthBits]); // Append the length | ||
|
||
// Pad the string to the max length | ||
paddedStr = stretch(paddedStr, maxLen); | ||
return padded; | ||
} | ||
|
||
return bufferToUints(paddedStr, numBytes); | ||
// sha256BlockLen: calculate the number of SHA256b blocks for given buffer | ||
export function sha256BlockLen(buf: string | Buffer): number { | ||
// 1 is for 0x80, 8 is for length bits (64 bits) | ||
return Math.ceil((buf.length + 1 + 8) / 64); | ||
} |
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