UNPKG

1.02 kBPlain TextView Raw
1
2import * as crypto from '../crypto/index'
3import * as utils from 'keystore-idb/utils'
4import { didToPublicKey } from './transformers'
5import { KeyType } from './types'
6
7
8/**
9 * Verify the signature of some data (string, ArrayBuffer or Uint8Array), given a DID.
10 */
11export async function verifySignedData({ charSize = 16, data, did, signature }: {
12 charSize?: number
13 data: string
14 did: string
15 signature: string
16}): Promise<boolean> {
17 try {
18 const { type, publicKey } = didToPublicKey(did)
19
20 const sigBytes = new Uint8Array(utils.base64ToArrBuf(signature))
21 const dataBytes = new Uint8Array(utils.normalizeUnicodeToBuf(data, charSize))
22 const keyBytes = new Uint8Array(utils.base64ToArrBuf(publicKey))
23
24 switch (type) {
25
26 case KeyType.Edwards:
27 return await crypto.ed25519.verify(dataBytes, sigBytes, keyBytes)
28
29 case KeyType.RSA:
30 return await crypto.rsa.verify(dataBytes, sigBytes, keyBytes)
31
32 default: return false
33 }
34
35 } catch (_) {
36 return false
37
38 }
39}
40