Skip to content

Commit

Permalink
feat: disable blake2b-wasm when the host is iOS 11
Browse files Browse the repository at this point in the history
  • Loading branch information
Keith-CY committed Sep 25, 2020
1 parent 7d6110f commit 4c70554
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 6 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ module.exports = {
}]
},
"globals": {
"BigInt": "readonly"
"BigInt": "readonly",
"globalThis": "readonly",
},
"env": {
"node": true,
Expand Down
52 changes: 52 additions & 0 deletions packages/ckb-sdk-utils/__tests__/crypto/blake2b.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { blake2b, PERSONAL } = require('../..')
const { ready } = require('../../lib/crypto/blake2b')
const fixtures = require('./blake2b.fixtures.json')

describe('blake2b', () => {
Expand Down Expand Up @@ -47,3 +48,54 @@ describe('blake2b', () => {
}
})
})

describe('blake2b-wasm-ready', () => {
afterEach(() => {
delete globalThis.navigator
})

describe("When it's in browser but not iOS 11", () => {
beforeEach(() => {
Object.defineProperty(globalThis, 'navigator', {
value: {
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 12_4_4 like Mac OS X)',
},
})
})

it('callback should be invoked without error', () => {
expect.assertions(1)
ready(err => {
expect(err).toBeUndefined()
})
})
})

describe("When it's not in browser and navigator is undefined", () => {
beforeEach(() => {
delete globalThis.navigator
})
it('callback should be invoked without error', () => {
expect.assertions(1)
ready(err => {
expect(err).toBeUndefined()
})
})
})

describe("When it's iOS 11", () => {
beforeEach(() => {
Object.defineProperty(globalThis, 'navigator', {
value: {
userAgent: 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_2_1 like Mac OS X)',
},
})
})
it('callback should be invoked with error', () => {
expect.assertions(1)
ready(err => {
expect(err).toEqual(new Error('blake2b-wasm is unavailable on iOS 11'))
})
})
})
})
13 changes: 10 additions & 3 deletions packages/ckb-sdk-utils/src/crypto/blake2b.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/* eslint-disable no-param-reassign */
const b2wasm = require('blake2b-wasm')
const {
OutLenTooSmallException,
OutLenTooLargeException,
Expand Down Expand Up @@ -375,8 +374,16 @@ export const blake2b = (
}

export const ready = (cb: Function) => {
b2wasm.ready(() => {
cb()
const iOSVersion = globalThis?.navigator?.userAgent.match(/cpu iphone os (.*?) like mac os/i)
if (iOSVersion?.[1].startsWith('11_')) {
cb(new Error(`blake2b-wasm is unavailable on iOS 11`))
return
}

/* eslint-disable global-require */
const b2wasm = require('blake2b-wasm')
b2wasm.ready((...args: unknown[]) => {
cb(...args)
})
}

Expand Down
4 changes: 2 additions & 2 deletions packages/ckb-sdk-utils/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"moduleResolution": "node",
"target": "es6",
"outDir": "lib",
"lib": ["es2017"],
"lib": ["DOM"],
"typeRoots": ["types", "node_modules/@types", "node_modules"],
"types": ["@nervosnetwork/ckb-types", "node"]
},
"include": ["src"],
"include": ["src"]
}

0 comments on commit 4c70554

Please sign in to comment.