/
RawKey.ts
35 lines (31 loc) · 924 Bytes
/
RawKey.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import SHA256 from 'crypto-js/sha256';
import * as secp256k1 from 'secp256k1';
import { Key } from './Key';
/**
* An implementation of the Key interfaces that uses a raw private key.
*/
export class RawKey extends Key {
/**
* Raw private key, in bytes.
*/
public privateKey: Buffer;
constructor(privateKey: Buffer) {
const publicKey = secp256k1.publicKeyCreate(
new Uint8Array(privateKey),
true
);
super(Buffer.from(publicKey));
this.privateKey = privateKey;
}
public ecdsaSign(payload: Buffer): { signature: Uint8Array; recid: number } {
const hash = Buffer.from(SHA256(payload.toString()).toString(), 'hex');
return secp256k1.ecdsaSign(
Uint8Array.from(hash),
Uint8Array.from(this.privateKey)
);
}
public async sign(payload: Buffer): Promise<Buffer> {
const { signature } = this.ecdsaSign(payload);
return Buffer.from(signature);
}
}