Created
March 5, 2021 06:40
-
-
Save taurenshaman/86f96ae81a3f26188bffa735218a70c0 to your computer and use it in GitHub Desktop.
pw-core/src/signers/blake2b-signer.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
import { Hasher } from '.'; | |
import { Reader } from 'ckb-js-toolkit'; | |
import blake2b from 'blake2b'; | |
export class Blake2bHasher extends Hasher { | |
constructor(key: string = null) { | |
const keyData = !key || key.length === 0 ? null : Buffer.from(key); | |
const h = blake2b( | |
32, | |
keyData, | |
null, | |
new Uint8Array(Reader.fromRawString('ckb-default-hash').toArrayBuffer()) | |
); | |
super(h); | |
} | |
update(data: string | ArrayBuffer): Hasher { | |
let array: Buffer; | |
if (data instanceof Reader) { | |
/** Reader type params not enter this branch, it's weired */ | |
array = Buffer.from(data.serializeJson().replace('0x', '')); | |
} else if (data instanceof ArrayBuffer) { | |
array = Buffer.from(new Uint8Array(data)); | |
} else if (typeof data === 'string') { | |
array = Buffer.from(data); | |
} else { | |
array = Buffer.from(new Uint8Array(new Reader(data).toArrayBuffer())); | |
} | |
this.h.update(array); | |
return this; | |
} | |
digest(): Reader { | |
const out = new Uint8Array(32); | |
this.h.digest(out); | |
return new Reader(out.buffer); | |
} | |
reset(): void { | |
this.h = blake2b( | |
32, | |
null, | |
null, | |
new Uint8Array(Reader.fromRawString('ckb-default-hash').toArrayBuffer()) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment