Skip to content

Commit

Permalink
Merge pull request #225 from synonymdev/seed-derivation
Browse files Browse the repository at this point in the history
Derive LDK seed from mnemonic in example app
  • Loading branch information
Jasonvdb authored Mar 19, 2024
2 parents eff283d + 2c1627c commit 94b0741
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 125 deletions.
103 changes: 0 additions & 103 deletions example/ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -502,46 +443,22 @@ 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`)
- React (from `../node_modules/react-native/`)
- 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`)
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -749,7 +647,6 @@ SPEC CHECKSUMS:
RNKeychain: a65256b6ca6ba6976132cc4124b238a5b13b3d9c
SocketRocket: f32cd54efbe0f095c4d7594881e52619cfe80b17
Yoga: 3efc43e0d48686ce2e8c60f99d4e6bd349aff981
YogaKit: f782866e155069a2cca2517aafea43200b01fd5a

PODFILE CHECKSUM: 940323d07de591a59a2ab39fc0ef7b7d6dc89c0d

Expand Down
2 changes: 1 addition & 1 deletion example/rn-setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
61 changes: 40 additions & 21 deletions example/utils/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<boolean> => {
mnemonic = createMnemonic(),
}: TWallet): Promise<boolean> => {
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);
Expand All @@ -45,30 +46,37 @@ export const setAccount = async ({
* @param {string} [accountName]
* @returns {Promise<string>}
*/
export const getAccount = async (accountName?: string): Promise<TAccount> => {
export const getWallet = async (accountName?: string): Promise<TWallet> => {
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<TAccount> => {
const wallet = await getWallet(accountName);
return {
name: wallet.name,
seed: await getLdkSeed(wallet.mnemonic),
};
};

/**
* Returns current account name, if any.
* @returns {Promise<string>}
Expand Down Expand Up @@ -98,20 +106,32 @@ export const createNewAccount = async (): Promise<Result<TAccount>> => {
}
}
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);
return err(e);
}
};

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<string>}
*/
export const getLdkSeed = async (mnumonic: string): Promise<string> => {
const mnemonicSeed = await bip39.mnemonicToSeed(mnumonic);
const root = bip32.fromSeed(mnemonicSeed, getNetwork(selectedNetwork));
return root.privateKey!.toString('hex');
};

/**
Expand Down Expand Up @@ -191,8 +211,7 @@ export const getMnemonicPhraseFromSeed = (accountSeed: string): string => {
export const getAddress = async (): Promise<IAddress> => {
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");
Expand Down
5 changes: 5 additions & 0 deletions example/utils/types.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down

0 comments on commit 94b0741

Please sign in to comment.