From b66ddfbfc3911fefb2fa68590468ec1c9fc859e2 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 18 Mar 2024 11:11:25 +0200 Subject: [PATCH 1/2] feat: example app derive ldk seed from mnumonic --- example/utils/helpers.ts | 61 ++++++++++++++++++++++++++-------------- example/utils/types.ts | 5 ++++ 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/example/utils/helpers.ts b/example/utils/helpers.ts index e758f209..eace4214 100644 --- a/example/utils/helpers.ts +++ b/example/utils/helpers.ts @@ -5,8 +5,9 @@ import { IAddress, } from '@synonymdev/react-native-ldk'; import { getItem, setItem } from '../ldk'; -import { EAccount } from './types'; +import { EAccount, TWallet } from './types'; import { err, ok, Result } from './result'; +// @ts-ignore import { randomBytes } from 'react-native-randombytes'; import * as bitcoin from 'bitcoinjs-lib'; import { selectedNetwork } from './constants'; @@ -21,16 +22,16 @@ import networks from '@synonymdev/react-native-ldk/dist/utils/networks'; * @param {string} [key] * @param {string} seed */ -export const setAccount = async ({ +export const setWallet = async ({ name = EAccount.name, - seed = randomSeed(), -}: TAccount): Promise => { + mnemonic = createMnemonic(), +}: TWallet): Promise => { try { - const account: TAccount = { + const wallet = { name, - seed, + mnemonic, }; - await Keychain.setGenericPassword(name, JSON.stringify(account), { + await Keychain.setGenericPassword(name, JSON.stringify(wallet), { service: name, }); await setItem(EAccount.currentAccountKey, name); @@ -45,30 +46,37 @@ export const setAccount = async ({ * @param {string} [accountName] * @returns {Promise} */ -export const getAccount = async (accountName?: string): Promise => { +export const getWallet = async (accountName?: string): Promise => { if (!accountName) { accountName = await getCurrentAccountName(); } - const defaultAccount: TAccount = { + const defaultWallet: TWallet = { name: EAccount.name, - seed: randomSeed(), + mnemonic: createMnemonic(), }; try { let result = await Keychain.getGenericPassword({ service: accountName }); if (result && result?.password) { - // Return existing account. - return JSON.parse(result?.password); + return JSON.parse(result.password); } else { // Setup default account. - await setAccount(defaultAccount); - return defaultAccount; + await setWallet({ name: accountName, mnemonic: defaultWallet.mnemonic }); + return defaultWallet; } } catch (e) { console.log(e); - return defaultAccount; + return defaultWallet; } }; +export const getAccount = async (accountName?: string): Promise => { + const wallet = await getWallet(accountName); + return { + name: wallet.name, + seed: await getLdkSeed(wallet.mnemonic), + }; +}; + /** * Returns current account name, if any. * @returns {Promise} @@ -98,11 +106,13 @@ export const createNewAccount = async (): Promise> => { } } const name = `wallet${num}`; + const mnemonic = createMnemonic(); + const account: TAccount = { name, - seed: randomSeed(), + seed: await getLdkSeed(mnemonic), }; - await setAccount(account); + await setWallet({ name, mnemonic }); return ok(account); } catch (e) { console.log(e); @@ -110,8 +120,18 @@ export const createNewAccount = async (): Promise> => { } }; -export const randomSeed = (): string => { - return randomBytes(32).toString('hex'); +export const createMnemonic = (): string => { + return bip39.entropyToMnemonic(randomBytes(32)); +}; + +/** + * Returns seed derived from mnemonic that is compatible with LDK-Node. + * @returns {Promise} + */ +export const getLdkSeed = async (mnumonic: string): Promise => { + const mnemonicSeed = await bip39.mnemonicToSeed(mnumonic); + const root = bip32.fromSeed(mnemonicSeed, getNetwork(selectedNetwork)); + return root.privateKey!.toString('hex'); }; /** @@ -191,8 +211,7 @@ export const getMnemonicPhraseFromSeed = (accountSeed: string): string => { export const getAddress = async (): Promise => { const network = getNetwork(selectedNetwork); - const { seed: accountSeed } = await getAccount(); - const mnemonic = getMnemonicPhraseFromSeed(accountSeed); + const { mnemonic } = await getWallet(); const mnemonicSeed = await bip39.mnemonicToSeed(mnemonic); const root = bip32.fromSeed(mnemonicSeed, network); const keyPair = root.derivePath("m/84'/1'/0'/0/0"); diff --git a/example/utils/types.ts b/example/utils/types.ts index b6d03868..e042dc82 100644 --- a/example/utils/types.ts +++ b/example/utils/types.ts @@ -1,5 +1,10 @@ import { TAvailableNetworks } from '@synonymdev/react-native-ldk'; +export type TWallet = { + name: string; + mnemonic: string; +}; + export enum EAccount { name = 'wallet0', currentAccountKey = 'currentAccount', From 2c1627c41168efa3c90dbea829b92851d93c81b3 Mon Sep 17 00:00:00 2001 From: Jason Date: Mon, 18 Mar 2024 11:11:39 +0200 Subject: [PATCH 2/2] fix: remove flipper --- example/ios/Podfile.lock | 103 --------------------------------------- example/rn-setup.js | 2 +- 2 files changed, 1 insertion(+), 104 deletions(-) diff --git a/example/ios/Podfile.lock b/example/ios/Podfile.lock index c4ce9057..688821d3 100644 --- a/example/ios/Podfile.lock +++ b/example/ios/Podfile.lock @@ -10,71 +10,12 @@ PODS: - React-Core (= 0.72.4) - React-jsi (= 0.72.4) - ReactCommon/turbomodule/core (= 0.72.4) - - Flipper (0.182.0): - - Flipper-Folly (~> 2.6) - - Flipper-Boost-iOSX (1.76.0.1.11) - - Flipper-DoubleConversion (3.2.0.1) - - Flipper-Fmt (7.1.7) - - Flipper-Folly (2.6.10): - - Flipper-Boost-iOSX - - Flipper-DoubleConversion - - Flipper-Fmt (= 7.1.7) - - Flipper-Glog - - libevent (~> 2.1.12) - - OpenSSL-Universal (= 1.1.1100) - - Flipper-Glog (0.5.0.5) - - Flipper-PeerTalk (0.0.4) - - FlipperKit (0.182.0): - - FlipperKit/Core (= 0.182.0) - - FlipperKit/Core (0.182.0): - - Flipper (~> 0.182.0) - - FlipperKit/CppBridge - - FlipperKit/FBCxxFollyDynamicConvert - - FlipperKit/FBDefines - - FlipperKit/FKPortForwarding - - SocketRocket (~> 0.6.0) - - FlipperKit/CppBridge (0.182.0): - - Flipper (~> 0.182.0) - - FlipperKit/FBCxxFollyDynamicConvert (0.182.0): - - Flipper-Folly (~> 2.6) - - FlipperKit/FBDefines (0.182.0) - - FlipperKit/FKPortForwarding (0.182.0): - - CocoaAsyncSocket (~> 7.6) - - Flipper-PeerTalk (~> 0.0.4) - - FlipperKit/FlipperKitHighlightOverlay (0.182.0) - - FlipperKit/FlipperKitLayoutHelpers (0.182.0): - - FlipperKit/Core - - FlipperKit/FlipperKitHighlightOverlay - - FlipperKit/FlipperKitLayoutTextSearchable - - FlipperKit/FlipperKitLayoutIOSDescriptors (0.182.0): - - FlipperKit/Core - - FlipperKit/FlipperKitHighlightOverlay - - FlipperKit/FlipperKitLayoutHelpers - - YogaKit (~> 1.18) - - FlipperKit/FlipperKitLayoutPlugin (0.182.0): - - FlipperKit/Core - - FlipperKit/FlipperKitHighlightOverlay - - FlipperKit/FlipperKitLayoutHelpers - - FlipperKit/FlipperKitLayoutIOSDescriptors - - FlipperKit/FlipperKitLayoutTextSearchable - - YogaKit (~> 1.18) - - FlipperKit/FlipperKitLayoutTextSearchable (0.182.0) - - FlipperKit/FlipperKitNetworkPlugin (0.182.0): - - FlipperKit/Core - - FlipperKit/FlipperKitReactPlugin (0.182.0): - - FlipperKit/Core - - FlipperKit/FlipperKitUserDefaultsPlugin (0.182.0): - - FlipperKit/Core - - FlipperKit/SKIOSNetworkPlugin (0.182.0): - - FlipperKit/Core - - FlipperKit/FlipperKitNetworkPlugin - fmt (6.2.1) - glog (0.3.5) - hermes-engine (0.72.4): - hermes-engine/Pre-built (= 0.72.4) - hermes-engine/Pre-built (0.72.4) - libevent (2.1.12) - - OpenSSL-Universal (1.1.1100) - RCT-Folly (2021.07.22.00): - boost - DoubleConversion @@ -502,38 +443,15 @@ PODS: - React-Core - SocketRocket (0.6.1) - Yoga (1.14.0) - - YogaKit (1.18.1): - - Yoga (~> 1.14) DEPENDENCIES: - boost (from `../node_modules/react-native/third-party-podspecs/boost.podspec`) - DoubleConversion (from `../node_modules/react-native/third-party-podspecs/DoubleConversion.podspec`) - FBLazyVector (from `../node_modules/react-native/Libraries/FBLazyVector`) - FBReactNativeSpec (from `../node_modules/react-native/React/FBReactNativeSpec`) - - Flipper (= 0.182.0) - - Flipper-Boost-iOSX (= 1.76.0.1.11) - - Flipper-DoubleConversion (= 3.2.0.1) - - Flipper-Fmt (= 7.1.7) - - Flipper-Folly (= 2.6.10) - - Flipper-Glog (= 0.5.0.5) - - Flipper-PeerTalk (= 0.0.4) - - FlipperKit (= 0.182.0) - - FlipperKit/Core (= 0.182.0) - - FlipperKit/CppBridge (= 0.182.0) - - FlipperKit/FBCxxFollyDynamicConvert (= 0.182.0) - - FlipperKit/FBDefines (= 0.182.0) - - FlipperKit/FKPortForwarding (= 0.182.0) - - FlipperKit/FlipperKitHighlightOverlay (= 0.182.0) - - FlipperKit/FlipperKitLayoutPlugin (= 0.182.0) - - FlipperKit/FlipperKitLayoutTextSearchable (= 0.182.0) - - FlipperKit/FlipperKitNetworkPlugin (= 0.182.0) - - FlipperKit/FlipperKitReactPlugin (= 0.182.0) - - FlipperKit/FlipperKitUserDefaultsPlugin (= 0.182.0) - - FlipperKit/SKIOSNetworkPlugin (= 0.182.0) - glog (from `../node_modules/react-native/third-party-podspecs/glog.podspec`) - hermes-engine (from `../node_modules/react-native/sdks/hermes-engine/hermes-engine.podspec`) - libevent (~> 2.1.12) - - OpenSSL-Universal (= 1.1.1100) - RCT-Folly (from `../node_modules/react-native/third-party-podspecs/RCT-Folly.podspec`) - RCTRequired (from `../node_modules/react-native/Libraries/RCTRequired`) - RCTTypeSafety (from `../node_modules/react-native/Libraries/TypeSafety`) @@ -541,7 +459,6 @@ DEPENDENCIES: - React-callinvoker (from `../node_modules/react-native/ReactCommon/callinvoker`) - React-Codegen (from `build/generated/ios`) - React-Core (from `../node_modules/react-native/`) - - React-Core/DevSupport (from `../node_modules/react-native/`) - React-Core/RCTWebSocket (from `../node_modules/react-native/`) - React-CoreModules (from `../node_modules/react-native/React/CoreModules`) - React-cxxreact (from `../node_modules/react-native/ReactCommon/cxxreact`) @@ -580,19 +497,9 @@ DEPENDENCIES: SPEC REPOS: trunk: - CocoaAsyncSocket - - Flipper - - Flipper-Boost-iOSX - - Flipper-DoubleConversion - - Flipper-Fmt - - Flipper-Folly - - Flipper-Glog - - Flipper-PeerTalk - - FlipperKit - fmt - libevent - - OpenSSL-Universal - SocketRocket - - YogaKit EXTERNAL SOURCES: boost: @@ -695,19 +602,10 @@ SPEC CHECKSUMS: DoubleConversion: 5189b271737e1565bdce30deb4a08d647e3f5f54 FBLazyVector: 5d4a3b7f411219a45a6d952f77d2c0a6c9989da5 FBReactNativeSpec: 3fc2d478e1c4b08276f9dd9128f80ec6d5d85c1f - Flipper: 6edb735e6c3e332975d1b17956bcc584eccf5818 - Flipper-Boost-iOSX: fd1e2b8cbef7e662a122412d7ac5f5bea715403c - Flipper-DoubleConversion: 2dc99b02f658daf147069aad9dbd29d8feb06d30 - Flipper-Fmt: 60cbdd92fc254826e61d669a5d87ef7015396a9b - Flipper-Folly: 584845625005ff068a6ebf41f857f468decd26b3 - Flipper-Glog: 70c50ce58ddaf67dc35180db05f191692570f446 - Flipper-PeerTalk: 116d8f857dc6ef55c7a5a75ea3ceaafe878aadc9 - FlipperKit: 2efad7007d6745a3f95e4034d547be637f89d3f6 fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9 glog: 04b94705f318337d7ead9e6d17c019bd9b1f6b1b hermes-engine: 81191603c4eaa01f5e4ae5737a9efcf64756c7b2 libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913 - OpenSSL-Universal: ebc357f1e6bc71fa463ccb2fe676756aff50e88c RCT-Folly: 424b8c9a7a0b9ab2886ffe9c3b041ef628fd4fb1 RCTRequired: c0569ecc035894e4a68baecb30fe6a7ea6e399f9 RCTTypeSafety: e90354072c21236e0bcf1699011e39acd25fea2f @@ -749,7 +647,6 @@ SPEC CHECKSUMS: RNKeychain: a65256b6ca6ba6976132cc4124b238a5b13b3d9c SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17 Yoga: 3efc43e0d48686ce2e8c60f99d4e6bd349aff981 - YogaKit: f782866e155069a2cca2517aafea43200b01fd5a PODFILE CHECKSUM: 940323d07de591a59a2ab39fc0ef7b7d6dc89c0d diff --git a/example/rn-setup.js b/example/rn-setup.js index 0e157a9c..7ff62d74 100644 --- a/example/rn-setup.js +++ b/example/rn-setup.js @@ -5,7 +5,7 @@ const baseCommand = 'rn-nodeify --install buffer,stream,assert,events,crypto,vm,process --hack'; function postInstallMac() { - exec(`${baseCommand} && cd ios && pod install && cd ..`); + exec(`${baseCommand} && cd ios && NO_FLIPPER=1 pod install && cd ..`); } function postInstallLinWin() { exec(baseCommand);