Skip to content

Commit

Permalink
various fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
TheTrunk committed Oct 11, 2023
1 parent 6832300 commit 04a6adf
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 21 deletions.
4 changes: 4 additions & 0 deletions src/blockchains.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ declare module '@storage/blockchains' {
scriptHash: string;
wif: string;
logo: string;
bip32: {
public: number;
private: number;
};
}
type blockchains = Record<string, Blockchain>;
let blockchains: blockchains;
Expand Down
11 changes: 9 additions & 2 deletions src/components/SyncRequest/SyncRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,21 @@ import { useTranslation } from 'react-i18next';
import { useTheme } from '../../hooks';
import Authentication from '../Authentication/Authentication';

import { blockchains } from '@storage/blockchains';

import { cryptos } from '../../types';

const SyncRequest = (props: {
chain: string;
chain: keyof cryptos;
actionStatus: (status: boolean) => void;
}) => {
// so we need our xpubkey, then generate address and show user the address. If not the same, tell user to restore or create wallet from scratch.
const { t } = useTranslation(['home', 'common']);
const { Fonts, Gutters, Layout, Colors, Common } = useTheme();
const [authenticationOpen, setAuthenticationOpen] = useState(false);

const blockchainConfig = blockchains[props.chain];

const approve = () => {
console.log('Approve');
props.actionStatus(true);
Expand Down Expand Up @@ -60,7 +66,8 @@ const SyncRequest = (props: {
]}
>
{t('home:ssp_sync_request', {
chain: props.chain.toUpperCase(),
chain: blockchainConfig.name,
symbol: blockchainConfig.symbol,
})}
</Text>
</View>
Expand Down
35 changes: 28 additions & 7 deletions src/lib/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ function generatexPubxPriv(
coin: number,
account = 0,
type = 'p2sh',
chain: keyof cryptos,
): xPrivXpub {
const scriptType = getScriptType(type);

const seed = bip39.mnemonicToSeedSync(mnemonic);
const masterKey = HDKey.fromMasterSeed(seed);
const bipParams = blockchains[chain].bip32;
const masterKey = HDKey.fromMasterSeed(seed, bipParams);
const externalChain = masterKey.derive(
`m/${bip}'/${coin}'/${account}'/${scriptType}'`,
);
Expand All @@ -52,8 +54,16 @@ export function getMasterXpub(
coin: number,
account = 0,
type = 'p2sh',
chain: keyof cryptos,
): string {
const xPubxPriv = generatexPubxPriv(mnemonic, bip, coin, account, type);
const xPubxPriv = generatexPubxPriv(
mnemonic,
bip,
coin,
account,
type,
chain,
);
return xPubxPriv.xpub;
}

Expand All @@ -64,8 +74,16 @@ export function getMasterXpriv(
coin: number,
account = 0,
type = 'p2sh',
chain: keyof cryptos,
): string {
const xPubxPriv = generatexPubxPriv(mnemonic, bip, coin, account, type);
const xPubxPriv = generatexPubxPriv(
mnemonic,
bip,
coin,
account,
type,
chain,
);
return xPubxPriv.xpriv;
}

Expand All @@ -77,8 +95,9 @@ export function generateMultisigAddress(
addressIndex: number,
chain: keyof cryptos,
): multisig {
const externalChain1 = HDKey.fromExtendedKey(xpub1);
const externalChain2 = HDKey.fromExtendedKey(xpub2);
const bipParams = blockchains[chain].bip32;
const externalChain1 = HDKey.fromExtendedKey(xpub1, bipParams);
const externalChain2 = HDKey.fromExtendedKey(xpub2, bipParams);

const externalAddress1 = externalChain1
.deriveChild(typeIndex)
Expand Down Expand Up @@ -128,7 +147,8 @@ export function generateAddressKeypair(
chain: keyof cryptos,
): keyPair {
const libID = getLibId(chain);
const externalChain = HDKey.fromExtendedKey(xpriv);
const bipParams = blockchains[chain].bip32;
const externalChain = HDKey.fromExtendedKey(xpriv, bipParams);

const externalAddress = externalChain
.deriveChild(typeIndex)
Expand Down Expand Up @@ -157,7 +177,8 @@ export function generateIdentityAddress(
const addressIndex = 0; // identity index

const libID = getLibId(chain);
const externalChain = HDKey.fromExtendedKey(xpub);
const bipParams = blockchains[chain].bip32;
const externalChain = HDKey.fromExtendedKey(xpub, bipParams);

const externalAddress = externalChain
.deriveChild(typeIndex)
Expand Down
2 changes: 2 additions & 0 deletions src/screens/Create/Create.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,13 +170,15 @@ function Create({ navigation }: Props) {
blockchainConfig.slip,
0,
blockchainConfig.scriptType,
identityChain,
); // takes ~3 secs
const xpub = getMasterXpub(
mnemonicPhrase,
48,
blockchainConfig.slip,
0,
blockchainConfig.scriptType,
identityChain,
); // takes ~3 secs
const xprivBlob = CryptoJS.AES.encrypt(
xpriv,
Expand Down
27 changes: 16 additions & 11 deletions src/screens/Home/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type Props = {
navigation: any;
};

const xpubRegex = /^([xyYzZtuUvV]pub[1-9A-HJ-NP-Za-km-z]{79,108})$/;

function Home({ navigation }: Props) {
// focusability of inputs
const alreadyMounted = useRef(false); // as of react strict mode, useEffect is triggered twice. This is a hack to prevent that without disabling strict mode
Expand Down Expand Up @@ -97,8 +99,6 @@ function Home({ navigation }: Props) {

const { newTx, clearTx } = useSocket();

const blockchainConfig = blockchains[activeChain];

useEffect(() => {
if (alreadyMounted.current) {
return;
Expand Down Expand Up @@ -152,6 +152,9 @@ function Home({ navigation }: Props) {
};

const checkXpubXpriv = async () => {
// todo loading animation on chain sync approval
const chainToUse = activeChain as keyof cryptos;
const blockchainConfigToUse = blockchains[chainToUse];
if (!xpubKey || !xprivKey) {
// just a precaution to make sure xpub and xpriv are set. Should acutally never end up here
getUniqueId()
Expand All @@ -161,20 +164,22 @@ function Home({ navigation }: Props) {
const pwForEncryption = id + password;
const mmm = CryptoJS.AES.decrypt(seedPhrase, pwForEncryption);
const mnemonicPhrase = mmm.toString(CryptoJS.enc.Utf8);
// generate master xpriv for flux
// generate master xpriv, xpub for chain
const xpriv = getMasterXpriv(
mnemonicPhrase,
48,
blockchainConfig.slip,
blockchainConfigToUse.slip,
0,
blockchainConfig.scriptType,
blockchainConfigToUse.scriptType,
chainToUse,
); // takes ~3 secs
const xpub = getMasterXpub(
mnemonicPhrase,
48,
blockchainConfig.slip,
blockchainConfigToUse.slip,
0,
blockchainConfig.scriptType,
blockchainConfigToUse.scriptType,
chainToUse,
); // takes ~3 secs
const xprivBlob = CryptoJS.AES.encrypt(
xpriv,
Expand All @@ -184,8 +189,8 @@ function Home({ navigation }: Props) {
xpub,
pwForEncryption,
).toString();
setXprivKey(activeChain, xprivBlob);
setXpubKey(activeChain, xpubBlob);
setXprivKey(chainToUse, xprivBlob);
setXpubKey(chainToUse, xpubBlob);
})
.catch((error) => {
console.log(error.message);
Expand Down Expand Up @@ -504,7 +509,7 @@ function Home({ navigation }: Props) {
// only data
dataToProcess = splittedInput[0];
}
if (dataToProcess.startsWith('xpub')) {
if (xpubRegex.test(dataToProcess)) {
// xpub
const xpubw = dataToProcess;
handleSyncRequest(xpubw, chain);
Expand Down Expand Up @@ -568,7 +573,7 @@ function Home({ navigation }: Props) {
dataToProcess = splittedInput[0];
}
// check if input is xpub or transaction
if (dataToProcess.startsWith('xpub')) {
if (xpubRegex.test(dataToProcess)) {
// xpub
const xpubw = scannedData;
handleSyncRequest(xpubw, chain);
Expand Down
2 changes: 2 additions & 0 deletions src/screens/Restore/Restore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -176,13 +176,15 @@ function Restore({ navigation }: Props) {
blockchainConfig.slip,
0,
blockchainConfig.scriptType,
identityChain,
); // takes ~3 secs
const xpub = getMasterXpub(
mnemonicPhrase,
48,
blockchainConfig.slip,
0,
blockchainConfig.scriptType,
identityChain,
); // takes ~3 secs
const xprivBlob = CryptoJS.AES.encrypt(
xpriv,
Expand Down
12 changes: 12 additions & 0 deletions src/storage/blockchains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ const flux = {
scriptHash: '1cbd',
wif: '80',
logo: '/src/assets/flux.svg',
bip32: {
public: 0x0488b21e,
private: 0x0488ade4,
},
};

const fluxTestnet = {
Expand All @@ -30,6 +34,10 @@ const fluxTestnet = {
scriptHash: '1cba',
wif: 'ef',
logo: '/src/assets/flux.svg',
bip32: {
public: 0x043587cf,
private: 0x04358394,
},
};

const rvn = {
Expand All @@ -46,6 +54,10 @@ const rvn = {
scriptHash: '7a',
wif: '80',
logo: '/src/assets/rvn.svg',
bip32: {
public: 0x0488b21e,
private: 0x0488ade4,
},
};

export const blockchains = {
Expand Down
2 changes: 1 addition & 1 deletion src/translations/resources/en/home.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"sync_qr_needed": "Please scan QR code to synchronise your SSP Key first.",
"err_tx_decode": "Error decoding transaction. Rejected.",
"sending_request": "Sending {{amount}} {{symbol}} to {{address}}",
"ssp_sync_request": "SSP Wallet would like to link and synchronise {{chain}} chain to your SSP Key.",
"ssp_sync_request": "SSP Wallet would like to link and synchronise {{chain}} ({{symbol}}) chain to your SSP Key.",
"ssp_key_info": "SSP Key is a second authentication factor for your SSP Wallet.",
"manual_input_info": "Input your transaction to sign or xpub of your wallet to sync.",
"ssp_help_about": "Your Second Key Factor authentication for your SSP Wallet.",
Expand Down

0 comments on commit 04a6adf

Please sign in to comment.