-
Notifications
You must be signed in to change notification settings - Fork 29
/
shim.js
44 lines (37 loc) · 1.21 KB
/
shim.js
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
import { Platform } from 'react-native';
import 'react-native-quick-crypto';
import * as bi from 'big-integer';
// Used in @digitalcredentials/vc-status-list
import { Buffer } from '@craftzdog/react-native-buffer';
import * as ExpoCrypto from 'expo-crypto';
// eslint-disable-next-line no-undef
global.crypto = {};
// eslint-disable-next-line no-undef
global.Buffer = Buffer;
const subtle = {
digest: (algorithm, data)=>{
// @digitalcredentials/jsonld-signatures calls this fn with an object
// rfd-canonize calls this with string
// both are valid options but expo-crypto only accepts string
const actualAlgorithm = typeof algorithm === 'string' ? algorithm : algorithm.name;
return ExpoCrypto.digest(actualAlgorithm.toUpperCase(), data);
},
// no other subtle methods appear to be needed
};
crypto.subtle = subtle;
function patchedBigInt(value) {
if (typeof value === 'string') {
const match = value.match(/^0([xo])([0-9a-f]+)$/i);
if (match) {
return bi(match[2], match[1].toLowerCase() === 'x' ? 16 : 8);
}
}
return bi(value);
}
if (typeof BigInt === 'undefined') {
if (Platform.OS === 'android') {
global.BigInt = patchedBigInt;
} else {
global.BigInt = bi;
}
}