-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathnit.ts
51 lines (44 loc) · 1.73 KB
/
nit.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { ethers, sha256 } from 'ethers';
import {
ProofMetadata,
getSerializedSortedProofMetadata,
} from '../../shared/repositories/proof/proof';
export async function generateIntegritySha(proofMetadata: ProofMetadata) {
const serializedSortedProofMetadata =
getSerializedSortedProofMetadata(proofMetadata);
const dataBytes = ethers.toUtf8Bytes(serializedSortedProofMetadata);
/**
* WORKAROUND: <TODO-paste-related-url-link>
* Ideally we should use nit module to get integritySha as show below
* ```
* import * as nit from "@numbersprotocol/nit";
* const dataBytes = ethers.toUtf8Bytes(data);
* const integritySha = await nit.getIntegrityHash(dataBytes);
* ```
* However installing "@numbersprotocol/nit" causing ionic build errors.
* Once "@numbersprotocol/nit" become browser friendly we can
* remove this workaround and completely rely on "@numbersprotocol/nit"
*/
// eslint-disable-next-line @typescript-eslint/no-magic-numbers
const integritySha = sha256(dataBytes).substring(2);
return integritySha;
}
export async function signWithIntegritySha(
sha256sum: string,
privateKey: string
) {
const signer = new ethers.Wallet(privateKey);
/**
* WORKAROUND: <TODO-paste-related-url-link>
* Ideally we should use nit module to get sign integrity hash as show below
* ```
* import * as nit from "@numbersprotocol/nit";
* const signature = await nit.signIntegrityHash(sha256, signer);
* ```
* However installing "@numbersprotocol/nit" causing ionic build errors.
* Once "@numbersprotocol/nit" become browser friendly we can
* remove this workaround and completely rely on "@numbersprotocol/nit"
*/
const signature = await signer.signMessage(sha256sum);
return signature;
}