Skip to content

Commit

Permalink
saga bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielLaanpere committed Jul 11, 2020
1 parent f4c63f3 commit b10d60d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 53 deletions.
2 changes: 1 addition & 1 deletion App.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const App = () => {
const storeSetup = configureStore();
setStore(storeSetup.store);
setPersistor(storeSetup.persistor);
}, [configureStore, initWallet]);
}, []);

useEffect(() => {
if (!!store) {
Expand Down
102 changes: 50 additions & 52 deletions src/sagas/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,67 +56,65 @@ export function* initWallet() {
}

export function* createInitialWallet(action) {
while (true) {
const { payload } = yield take(REHYDRATE);
try {
if (payload?.wallet && !payload.wallet.address) {
const password = '';
let mnemonic = new Mnemonic();
while (!Mnemonic.isValid(mnemonic.toString())) {
mnemonic = new Mnemonic();
}
const xPrivKey = mnemonic.toHDPrivateKey();
const { payload } = yield take(REHYDRATE);
try {
if (payload?.wallet && !payload.wallet.address) {
const password = '';
let mnemonic = new Mnemonic();
while (!Mnemonic.isValid(mnemonic.toString())) {
mnemonic = new Mnemonic();
}
const xPrivKey = mnemonic.toHDPrivateKey();

// Wallet wif
const walletPath = testnet ? "m/44'/1'/0'/0" : "m/44'/0'/0'/0";
const { privateKey: walletPirvateKey } = yield xPrivKey.derive(
walletPath,
);
const walletPrivKeyBuf = yield walletPirvateKey.bn.toBuffer({
size: 32,
});
const walletWif = yield call(toWif, walletPrivKeyBuf, testnet);
// Wallet wif
const walletPath = testnet ? "m/44'/1'/0'/0" : "m/44'/0'/0'/0";
const { privateKey: walletPirvateKey } = yield xPrivKey.derive(
walletPath,
);
const walletPrivKeyBuf = yield walletPirvateKey.bn.toBuffer({
size: 32,
});
const walletWif = yield call(toWif, walletPrivKeyBuf, testnet);

// Address and address wif
const addressPath = testnet ? "m/44'/1'/0'/0/0" : "m/44'/0'/0'/0/0";
const { privateKey } = yield xPrivKey.derive(addressPath);
const publicKeyBuffer = yield privateKey.publicKey.toBuffer();
const publicKey = yield publicKeyBuffer.toString('base64');
const addressPrivKeyBuf = yield privateKey.bn.toBuffer({ size: 32 });
const addressWif = yield call(toWif, addressPrivKeyBuf, testnet);
const address = yield call(getChash160, ['sig', { pubkey: publicKey }]);
// Address and address wif
const addressPath = testnet ? "m/44'/1'/0'/0/0" : "m/44'/0'/0'/0/0";
const { privateKey } = yield xPrivKey.derive(addressPath);
const publicKeyBuffer = yield privateKey.publicKey.toBuffer();
const publicKey = yield publicKeyBuffer.toString('base64');
const addressPrivKeyBuf = yield privateKey.bn.toBuffer({ size: 32 });
const addressWif = yield call(toWif, addressPrivKeyBuf, testnet);
const address = yield call(getChash160, ['sig', { pubkey: publicKey }]);

yield put(
createInitialWalletSuccess({
password,
address,
walletWif,
addressWif,
xPrivKey,
publicKey,
privateKey,
walletPirvateKey,
seedWords: mnemonic.phrase,
}),
);
yield put(
createInitialWalletSuccess({
password,
address,
walletWif,
addressWif,
xPrivKey,
publicKey,
privateKey,
walletPirvateKey,
seedWords: mnemonic.phrase,
}),
);

yield put(
setToastMessage({
type: 'SUCCESS',
message: 'Your wallet is ready to use!',
}),
);
}
} catch (error) {
console.log('createInitialWallet ERROR: ', error);
yield put(createInitialWalletFail());
yield put(
setToastMessage({
type: 'ERROR',
message: 'Unable to generate new wallet.',
type: 'SUCCESS',
message: 'Your wallet is ready to use!',
}),
);
}
} catch (error) {
console.log('createInitialWallet ERROR: ', error);
yield put(createInitialWalletFail());
yield put(
setToastMessage({
type: 'ERROR',
message: 'Unable to generate new wallet.',
}),
);
}
}

Expand Down

0 comments on commit b10d60d

Please sign in to comment.