Skip to content

Commit 1b6b3f4

Browse files
Merge commit 'ea921a8' into CSI-1535_Swap-Protocol-Integration-in-protocol-module_Arnab-Sen
2 parents 758002c + ea921a8 commit 1b6b3f4

File tree

11 files changed

+295
-971
lines changed

11 files changed

+295
-971
lines changed

.husky/pre-commit

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
#!/bin/sh
22
. "$(dirname "$0")/_/husky.sh"
3-
./render-uml.sh
43
yarn lint-staged

src/flows/addCoin/helper.ts

Lines changed: 59 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,78 @@
11
import {
2-
BtcCoinData,
3-
CoinData,
4-
COINS,
52
EthCoinData,
6-
FeatureName,
7-
hexToAscii,
8-
intToUintByte,
9-
isFeatureEnabled
3+
NearCoinData,
4+
SolanaCoinData
105
} from '@cypherock/communication';
11-
import { Coin } from '@cypherock/database';
12-
13-
import { FlowError, FlowErrorType } from '../flowError';
6+
import { BtcCoinData, COINS, hexToAscii } from '@cypherock/communication';
7+
import { Account, AccountDB } from '@cypherock/database';
8+
import {
9+
BitcoinWallet,
10+
EthereumWallet,
11+
NearWallet,
12+
SolanaWallet
13+
} from '@cypherock/wallet';
1414

1515
export const formatCoinsForDB = async (
1616
walletId: string,
1717
xpubRaw: string,
18-
coinTypes: any
19-
): Promise<Coin[]> => {
20-
const coins: Coin[] = [];
18+
selectedCoin: { accountIndex: number; accountType: string; id: string }
19+
): Promise<Account> => {
2120
let sliceIndex = 0;
22-
for (let i = 0; i < coinTypes.length; i++) {
23-
const x = xpubRaw.slice(sliceIndex, sliceIndex + 222);
24-
let z;
25-
sliceIndex += 224;
21+
const x = xpubRaw.slice(sliceIndex, sliceIndex + 222);
22+
sliceIndex += 224;
2623

27-
const coinData = COINS[coinTypes[i]];
28-
if (coinData instanceof BtcCoinData && coinData.hasSegwit) {
29-
z = xpubRaw.slice(sliceIndex, sliceIndex + 222);
30-
sliceIndex += 224;
31-
}
24+
const coinData = COINS[selectedCoin.id];
3225

33-
const accountXpub = hexToAscii(x);
34-
let accountZpub;
26+
const accountXpub = hexToAscii(x);
3527

36-
if (z) {
37-
accountZpub = hexToAscii(z);
38-
}
39-
40-
const coin: Coin = {
41-
totalBalance: '0',
42-
totalUnconfirmedBalance: '0',
43-
xpubBalance: '0',
44-
xpubUnconfirmedBalance: '0',
45-
slug: coinTypes[i],
46-
walletId,
47-
xpub: accountXpub,
48-
zpub: accountZpub,
49-
price: 0,
50-
priceLastUpdatedAt: undefined
51-
};
52-
coins.push(coin);
53-
}
54-
return coins;
28+
const account: Account = {
29+
name: '',
30+
accountId: '',
31+
accountIndex: selectedCoin.accountIndex,
32+
coinId: coinData.id,
33+
accountType: selectedCoin.accountType,
34+
totalBalance: '0',
35+
totalUnconfirmedBalance: '0',
36+
walletId,
37+
xpub: accountXpub
38+
};
39+
account.accountId = AccountDB.buildAccountIndex(account);
40+
account.name = AccountDB.createAccountName(account);
41+
return account;
5542
};
5643

57-
export const createCoinIndexes = (
58-
sdkVersion: string,
59-
selectedCoins: string[]
44+
export const createCoinIndex = (
45+
_sdkVersion: string,
46+
selectedCoin: { accountIndex: number; accountType: string; id: string }
6047
) => {
61-
const coinLength = intToUintByte(selectedCoins.length, 8);
62-
const coinIndexList = [];
63-
const chainIndexList = [];
48+
const coin = COINS[selectedCoin.id];
6449

65-
for (const elem of selectedCoins) {
66-
const coin = COINS[elem];
67-
68-
if (!(coin instanceof CoinData)) {
69-
throw new FlowError(FlowErrorType.ADD_COIN_UNKNOWN_ASSET, elem);
70-
}
71-
72-
const coinIndex = coin.coinIndex;
73-
coinIndexList.push(coinIndex);
74-
75-
const longChainId = isFeatureEnabled(
76-
FeatureName.EvmLongChainId,
77-
sdkVersion
50+
if (coin instanceof BtcCoinData) {
51+
return BitcoinWallet.getDerivationPath(
52+
selectedCoin.accountIndex,
53+
selectedCoin.accountType,
54+
coin.coinIndex
55+
);
56+
}
57+
if (coin instanceof EthCoinData) {
58+
return EthereumWallet.getDerivationPath(
59+
selectedCoin.accountIndex,
60+
selectedCoin.accountType,
61+
coin.chain
62+
);
63+
}
64+
if (coin instanceof NearCoinData) {
65+
return NearWallet.getDerivationPath(
66+
selectedCoin.accountIndex,
67+
selectedCoin.accountType
68+
);
69+
}
70+
if (coin instanceof SolanaCoinData) {
71+
return SolanaWallet.getDerivationPath(
72+
selectedCoin.accountIndex,
73+
selectedCoin.accountType
7874
);
79-
let chainIndex = longChainId ? '0000000000000000' : '00';
80-
81-
if (coin instanceof EthCoinData) {
82-
chainIndex = intToUintByte(coin.chain, longChainId ? 64 : 8);
83-
}
84-
85-
chainIndexList.push(chainIndex);
8675
}
8776

88-
return coinLength + coinIndexList.join('') + chainIndexList.join('');
77+
throw new Error('Invalid coin type: ' + selectedCoin.id);
8978
};

src/flows/addCoin/index.ts

Lines changed: 13 additions & 130 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import { PacketVersionMap } from '@cypherock/communication';
2-
31
import { commandHandler76 } from '../../handlers';
42
import { logger } from '../../utils';
53
import { CyFlow, CyFlowRunOptions, ExitFlowError } from '../index';
64

7-
import { createCoinIndexes, formatCoinsForDB } from './helper';
5+
import { createCoinIndex, formatCoinsForDB } from './helper';
86

97
export interface CoinAdderRunOptions extends CyFlowRunOptions {
108
walletId: string;
11-
selectedCoins: string[];
12-
isResync: boolean;
9+
selectedCoin: {
10+
accountIndex: number;
11+
accountType: string;
12+
id: string;
13+
};
1314
pinExists: boolean;
1415
passphraseExists: boolean;
1516
}
@@ -41,124 +42,19 @@ export class CoinAdder extends CyFlow {
4142
super();
4243
}
4344

44-
async runLegacy({
45-
connection,
46-
walletId,
47-
selectedCoins,
48-
isResync,
49-
pinExists,
50-
passphraseExists
51-
}: CoinAdderRunOptions) {
52-
const resyncIndex = isResync ? '01' : '00';
53-
await connection.sendData(
54-
45,
55-
walletId + resyncIndex + createCoinIndexes('1.0.0', selectedCoins)
56-
);
57-
58-
const data = await connection.receiveData([46, 75, 76], 30000);
59-
if (data.commandType === 75) {
60-
this.emit('locked');
61-
throw new ExitFlowError();
62-
}
63-
if (data.commandType === 76) {
64-
commandHandler76(data, this);
65-
}
66-
const coinConfirmed = data.data;
67-
if (parseInt(coinConfirmed, 10)) {
68-
this.emit('coinsConfirmed', true);
69-
} else {
70-
this.emit('coinsConfirmed', false);
71-
throw new ExitFlowError();
72-
}
73-
74-
if (passphraseExists) {
75-
const passphraseData = await connection.receiveData([91, 90], 90000);
76-
77-
if (passphraseData.commandType === 91) {
78-
this.emit('coinsConfirmed', false);
79-
throw new ExitFlowError();
80-
}
81-
82-
if (!passphraseData.data.startsWith('01')) {
83-
throw new Error('Invalid data from device.');
84-
}
85-
86-
this.emit('passphraseEntered');
87-
}
88-
89-
if (pinExists) {
90-
const pinData = await connection.receiveData([47, 79, 81], 90000);
91-
if (pinData.commandType === 79) {
92-
this.emit('coinsConfirmed', false);
93-
throw new ExitFlowError();
94-
}
95-
96-
if (pinData.commandType === 81) {
97-
this.emit('noWalletOnCard');
98-
throw new ExitFlowError();
99-
}
100-
101-
const pinEntered = pinData.data;
102-
if (parseInt(pinEntered, 10)) {
103-
this.emit('pinEntered', true);
104-
} else {
105-
this.emit('pinEntered', false);
106-
throw new ExitFlowError();
107-
}
108-
}
109-
110-
const data1 = await connection.receiveData([48, 79, 81, 71], 90000);
111-
if (data1.commandType === 79) {
112-
this.emit('coinsConfirmed', false);
113-
throw new ExitFlowError();
114-
}
115-
if (data1.commandType === 81) {
116-
this.emit('noWalletOnCard');
117-
throw new ExitFlowError();
118-
}
119-
if (data1.commandType === 71) {
120-
this.emit('cardError');
121-
throw new ExitFlowError();
122-
}
123-
124-
this.emit('cardTapped');
125-
126-
const xPubDetails = await connection.receiveData([49], 60000);
127-
if (!xPubDetails) {
128-
//I don't remember why I had put this condition.
129-
this.emit('unknownError');
130-
throw new Error('No xpub details found');
131-
}
132-
133-
await connection.sendData(42, '01');
134-
if (!isResync) {
135-
const xpubList = await formatCoinsForDB(
136-
walletId,
137-
xPubDetails.data,
138-
selectedCoins
139-
);
140-
logger.debug('Xpub list', { xpubList });
141-
this.emit('xpubList', xpubList);
142-
} else {
143-
this.emit('xpubList', []);
144-
}
145-
}
146-
14745
async runOperation({
14846
connection,
14947
sdkVersion,
15048
walletId,
151-
selectedCoins,
152-
isResync,
49+
selectedCoin,
15350
pinExists,
15451
passphraseExists
15552
}: CoinAdderRunOptions) {
156-
const resyncIndex = isResync ? '01' : '00';
15753
const sequenceNumber = connection.getNewSequenceNumber();
54+
const addCoinData = walletId + createCoinIndex(sdkVersion, selectedCoin);
15855
await connection.sendCommand({
15956
commandType: 45,
160-
data:
161-
walletId + resyncIndex + createCoinIndexes(sdkVersion, selectedCoins),
57+
data: addCoinData,
16258
sequenceNumber
16359
});
16460

@@ -247,17 +143,9 @@ export class CoinAdder extends CyFlow {
247143
throw new ExitFlowError();
248144
}
249145

250-
if (!isResync) {
251-
const xpubList = await formatCoinsForDB(
252-
walletId,
253-
data.data,
254-
selectedCoins
255-
);
256-
logger.debug('Xpub list', { xpubList });
257-
this.emit('xpubList', xpubList);
258-
} else {
259-
this.emit('xpubList', []);
260-
}
146+
const xpubList = await formatCoinsForDB(walletId, data.data, selectedCoin);
147+
logger.debug('Xpub list', { xpubList });
148+
this.emit('xpubList', xpubList);
261149
}
262150

263151
/**
@@ -274,12 +162,7 @@ export class CoinAdder extends CyFlow {
274162
const ready = await this.deviceReady(connection);
275163

276164
if (ready) {
277-
const packetVersion = connection.getPacketVersion();
278-
if (packetVersion === PacketVersionMap.v3) {
279-
await this.runOperation(params);
280-
} else if (packetVersion === PacketVersionMap.v2) {
281-
await this.runLegacy(params);
282-
}
165+
await this.runOperation(params);
283166
} else {
284167
this.emit('notReady');
285168
}

src/flows/addWallet/index.ts

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { PacketVersionMap } from '@cypherock/communication';
2-
31
import { commandHandler76 } from '../../handlers';
42
import { logger } from '../../utils';
53
import { CyFlow, CyFlowRunOptions, ExitFlowError } from '../index';
@@ -27,27 +25,6 @@ export class WalletAdder extends CyFlow {
2725
super();
2826
}
2927

30-
private async runLegacy({ connection }: WalletAdderRunOptions) {
31-
await connection.sendData(43, '00');
32-
33-
const data = await connection.receiveData([44, 76], 30000);
34-
if (data.commandType === 76) {
35-
commandHandler76(data, this);
36-
}
37-
38-
const rawWalletDetails = data.data;
39-
if (rawWalletDetails === '00') {
40-
this.emit('walletDetails', null);
41-
throw new ExitFlowError();
42-
}
43-
44-
const walletDetails = extractWalletDetails(rawWalletDetails);
45-
logger.info('Wallet Details', { walletDetails });
46-
this.emit('walletDetails', walletDetails);
47-
48-
await connection.sendData(42, '01');
49-
}
50-
5128
private async runOperation({ connection }: WalletAdderRunOptions) {
5229
const sequenceNumber = connection.getNewSequenceNumber();
5330

@@ -107,12 +84,7 @@ export class WalletAdder extends CyFlow {
10784
const ready = await this.deviceReady(connection);
10885

10986
if (ready) {
110-
const packetVersion = connection.getPacketVersion();
111-
if (packetVersion === PacketVersionMap.v3) {
112-
await this.runOperation(params);
113-
} else {
114-
await this.runLegacy(params);
115-
}
87+
await this.runOperation(params);
11688
} else {
11789
this.emit('notReady');
11890
}

0 commit comments

Comments
 (0)