-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathsignData.ts
More file actions
47 lines (40 loc) · 1.19 KB
/
signData.ts
File metadata and controls
47 lines (40 loc) · 1.19 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
import { JWKInterface } from 'arweave/node/lib/wallet';
import crypto, { createPrivateKey, createPublicKey, KeyLike, KeyObject } from 'crypto';
import { toB64Url } from './base64';
export async function signData(privateKey: KeyLike, dataToSign: string): Promise<Uint8Array> {
const pem = ((privateKey as unknown) as crypto.KeyObject).export({
format: 'pem',
type: 'pkcs1'
});
const sign = crypto.createSign('sha256');
sign.update(dataToSign);
const signature = sign.sign({
key: pem,
padding: crypto.constants.RSA_PKCS1_PSS_PADDING,
saltLength: 0 // We do not need to salt the signature since we combine with a random UUID
});
return Promise.resolve(signature);
}
export function jwkInterfaceToPublicKey(jwk: JWKInterface): KeyObject {
const publicKey = createPublicKey({
key: {
...jwk,
kty: 'RSA'
},
format: 'jwk'
});
return publicKey;
}
export function jwkInterfaceToPrivateKey(jwk: JWKInterface): KeyObject {
const privateKey = createPrivateKey({
key: {
...jwk,
kty: 'RSA'
},
format: 'jwk'
});
return privateKey;
}
export function publicKeyToHeader(publicKey: KeyObject) {
return toB64Url(Buffer.from(JSON.stringify(publicKey.export({ format: 'jwk' }))));
}