Skip to content

Commit e916f11

Browse files
committed
fix: update webcrypto version
1 parent c579553 commit e916f11

File tree

6 files changed

+196
-220
lines changed

6 files changed

+196
-220
lines changed

App.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import './globals.js'
2-
import './globals-crypto.js'
32
import { StatusBar } from 'expo-status-bar'
43
import { StyleSheet, Text, View } from 'react-native'
54
import { useState, useEffect } from 'react'

README.md

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import 'weakmap-polyfill'
2626
import { TextEncoder, TextDecoder } from 'text-encoding'
2727
import { EventTarget, Event } from 'event-target-shim'
2828
import { Buffer } from '@craftzdog/react-native-buffer'
29+
import { Crypto } from '@peculiar/webcrypto'
2930

3031
global.TextEncoder = TextEncoder
3132
global.TextDecoder = TextDecoder
@@ -39,33 +40,9 @@ global.AbortSignal.timeout = (ms) => {
3940
}, ms)
4041
}
4142
global.Buffer = Buffer
42-
```
43-
44-
3. WebCrypto
45-
46-
It's necessary to shim support for [SubtleCrypto](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto)
47-
until it's added to react-native by default.
48-
49-
First install the necessary dependencies:
50-
51-
```console
52-
$ npm i @peculiar/webcrypto
53-
```
54-
55-
..and create a shim file:
56-
57-
```js
58-
// globals-crypto.js - this should be imported at the top of your App.js file
59-
// but AFTER globals.js
60-
import { Crypto } from '@peculiar/webcrypto'
61-
6243
global.crypto.subtle = new Crypto().subtle
6344
```
6445

65-
IMPORTANT: until [PeculiarVentures/webcrypto#67](https://github.com/PeculiarVentures/webcrypto/pull/67) is
66-
merged, SubtleCrypto has to be shimmed *after* the node Buffer polyfill has
67-
been added to the global context.
68-
6946
3. Enable modern JS features
7047

7148
- libp2p uses [ES2022 private properties](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Classes/Private_properties) in some places, so enable transpiling them for use with React Native. At the time of writing [loose mode](https://2ality.com/2015/12/babel6-loose-mode.html) is also required.
@@ -117,7 +94,7 @@ $ npx expo run:ios --device
11794

11895
## Notes
11996

120-
- By default this demo uses pure-js crypto - it's not efficient enough to run on an actual device, `crypto-browserify` should be replaced with `react-native-quick-crypto` in `bable.config.js` for native builds
97+
- By default this demo uses pure-js crypto - it's not efficient enough to run on an actual device, `crypto-browserify` should be replaced with `react-native-quick-crypto` in `babel.config.js` for native builds
12198
- `@libp2p/webrtc` can also only run on a device since it needs native code
12299

123100
### Debugging

globals-crypto.js

Lines changed: 0 additions & 7 deletions
This file was deleted.

globals.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'weakmap-polyfill'
88
import { TextEncoder, TextDecoder } from 'text-encoding'
99
import { EventTarget, Event } from 'event-target-shim'
1010
import { Buffer } from '@craftzdog/react-native-buffer'
11+
import { Crypto } from '@peculiar/webcrypto'
1112

1213
global.TextEncoder = TextEncoder
1314
global.TextDecoder = TextDecoder
@@ -20,4 +21,11 @@ global.AbortSignal.timeout = (ms) => {
2021
controller.abort(new Error('Aborted'))
2122
}, ms)
2223
}
24+
global.AbortSignal.prototype.throwIfAborted = () => {
25+
if (this.aborted) {
26+
throw new Error('Aborted')
27+
}
28+
}
29+
2330
global.Buffer = Buffer
31+
global.crypto.subtle = new Crypto().subtle

0 commit comments

Comments
 (0)