openwallet-foundation / credo-ts

Typescript framework for building decentralized identity and verifiable credential solutions
https://credo.js.org
Apache License 2.0
252 stars 188 forks source link

Generate seeds using @stablelib/random #957

Open TimoGlastra opened 2 years ago

TimoGlastra commented 2 years ago

We could use @stablelib/random for this right? https://github.com/StableLib/stablelib/blob/d8b909d49245c040e5e4a59ff8c9b269fc1781ee/packages/random/random.ts#L52

_Originally posted by @blu3beri in https://github.com/hyperledger/aries-framework-javascript/pull/953#discussion_r925283552_

We need to check whether @stablelib/random works in react native through react-native-get-random-values

berendsliedrecht commented 2 years ago

the bbs-signatures library from mattrglobal uses it for the randomness of the seed.

https://github.com/mattrglobal/bbs-signatures/blob/3a55cffdda65f894e4e9a2bd89aa9ce483c611c0/src/js/wasm_module.js#L16

@stablelib/random detects platform and sets randomnesssource here: https://github.com/StableLib/stablelib/blob/d8b909d49245c040e5e4a59ff8c9b269fc1781ee/packages/random/source/system.ts#L8

I am not sure how react-native-get-random-values polyfills, but if its injected here we could make it work. Otherwise opening a PR adding React Native support would be trivial with react-native-get-random-values.

TimoGlastra commented 2 years ago

react-native-get-random-values polyfill the browser crypto object, so it should just work. The only thing where it could fail is if stablelib/random fails to detect the RN envrionment as a browser environment. I'll quickly test it/

It doesn't work :(

Error: System random byte generator is not available., js engine: hermes
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:95:4 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:141:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/@react-native/polyfills/error-guard.js:49:36 in ErrorUtils.reportFatalError
at node_modules/metro-runtime/src/polyfills/require.js:203:6 in guardedLoadModule
at node_modules/metro-runtime/src/polyfills/require.js:127:23 in metroRequire
at http://10.254.0.138:8081/index.bundle?platform=ios&dev=true&hot=false&strict=false&minify=false:312426:3 in global
berendsliedrecht commented 2 years ago

Okay thats sad. I am not sure if @stablelib is big on RN support but we could add it.

TimoGlastra commented 2 years ago

Yeah we don't even need to add a dependency, just extend the browser check to also work in the case of RN

https://cs.github.com/StableLib/stablelib/blob/d8b909d49245c040e5e4a59ff8c9b269fc1781ee/packages/random/source/system.ts#L8

I think the issue is that is checks for self.crypto, and we need to check for just the crypto global

berendsliedrecht commented 2 years ago

I think the issue is that is checks for self.crypto, and we need to check for just the crypto global

Yeah that seems very likely. Could you test that in your test app? just patch the node module.

TimoGlastra commented 2 years ago

It does work 🎉, I just imported the react-native-get-random-values library at the wrong place so @stablelib/random got imported before. Adding it as the first thing in index.js fixed it and it seems to work out of the box.

So we can just make a PR and start using that as the seed generator