Skip to content

Commit

Permalink
Merge pull request #30 from docknetwork/DCKA-760-wallet-accounts-and-…
Browse files Browse the repository at this point in the history
…cred-data-disapears-after-updating-wallet-155

fixing data migration issues
  • Loading branch information
maycon-mello authored Jun 6, 2022
2 parents be7c2e0 + 9c845ca commit be3c3c1
Show file tree
Hide file tree
Showing 16 changed files with 105 additions and 65 deletions.
19 changes: 5 additions & 14 deletions packages/core/lib/core/storage.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
export type StorageInterface = string;

// setItem(key, data) {

// }

// getItem(key) {

// }

// removeItem(key) {

// }
// }
export interface StorageInterface {
setItem(key: string, item: string): Promise<void>;
getItem(): Promise<string>;
remoteItem(key: string): Promise<void>;
}

let storage: StorageInterface;

Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/modules/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export class Account {
*/
async loadDetails() {
this.details = await this.accounts.wallet.getDocumentById(this.address);
this.name = this.details.name;
this.name = this.details && this.details.name;
}

getAddress() {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/modules/accounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ export class Accounts {
);

if (json) {
assert(!!password, 'password is required');
assert(typeof password === 'string', 'password is required');
}

const mnemonic =
Expand Down
37 changes: 25 additions & 12 deletions packages/core/lib/modules/data-migration.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,33 @@ export async function migrate({wallet}: MigrateParams) {
account.correlation.find(id => id === doc.id),
);
const mnemonicDoc = relatedDocs.find(doc => doc.type === 'Mnemonic');
const keyPairDoc = relatedDocs.find(doc => doc.type === 'KeyPair');

if (!mnemonicDoc) {
return;
try {
if (mnemonicDoc) {
await wallet.remove(mnemonicDoc.id);
await wallet.remove(account.id);
await wallet.accounts.create({
mnemonic: mnemonicDoc.value,
name: account.meta.name,
type: account.meta.keypairType,
derivationPath: account.meta.derivationPath,
});
} else if (keyPairDoc) {
console.log(keyPairDoc);
await wallet.remove(keyPairDoc.id);
await wallet.remove(account.id);
await wallet.accounts.create({
name: account.meta.name,
json: keyPairDoc.value,
password: '',
});
}
} catch (err) {
Logger.error(`failed to migrate account ${account.id}`);
Logger.error(err);
throw err;
}

await wallet.remove(mnemonicDoc.id);
await wallet.remove(account.id);

await wallet.accounts.create({
mnemonic: mnemonicDoc.value,
name: account.meta.name,
type: account.meta.keypairType,
derivationPath: account.meta.derivationPath,
});
}),
);

Expand Down
2 changes: 1 addition & 1 deletion packages/core/lib/modules/data-migration.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('DataMigration', () => {

it('expect to migrate accounts', async () => {
const accounts = await wallet.accounts.getAccounts();
expect(accounts.length).toBe(2);
expect(accounts.length).toBe(3);
expect(wallet.migrated).toBeTruthy();
});

Expand Down
6 changes: 2 additions & 4 deletions packages/core/lib/modules/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import {NetworkManager} from './network-manager';
import {migrate} from './data-migration';
import {Logger} from '../core/logger';

// import {getEnvironment} from 'realm/lib/utils';

/** Wallet events */
export const WalletEvents = {
ready: 'ready',
Expand Down Expand Up @@ -156,13 +154,12 @@ class Wallet {
async deleteWallet() {
this.eventManager.emit(WalletEvents.walletDeleted);
clearCacheData();
getStorage().removeItem(this.walletId);
await getStorage().removeItem(this.walletId);
await walletService.create({
walletId: this.walletId,
});
await walletService.load();
await walletService.sync();
await migrate({wallet: this});
}

/**
Expand Down Expand Up @@ -342,6 +339,7 @@ class Wallet {
}

async importWallet({json, password}) {
await this.deleteWallet();
await walletService.importWallet({json, password});
this.migrated = await migrate({wallet: this});
}
Expand Down
2 changes: 0 additions & 2 deletions packages/core/lib/rpc-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ export const rpcRequest = async (method, ...params) => {
};

export function initRpcClient(requestHandler) {
console.log('Rpc client initialized', global.client);

global.client = new JSONRPCClient(requestHandler);

global.client.__request = global.client.request;
Expand Down
5 changes: 0 additions & 5 deletions packages/core/lib/services/dock/service.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,19 +50,14 @@ export class DockService {
assert(!this.connectionInProgress, 'there is a connection in progress');
assert(!this.isDockReady, 'dock is already initialized');

console.log('connectionInProgress: true');
this.connectionInProgress = true;

Logger.info(`Attempt to initialized substrate at: ${params.address}`);

const result = await this.dock.init(params).finally(() => {
console.log('connectionInProgress: false');

this.connectionInProgress = false;
});

console.log('result connection');

Logger.debug(`Substrate initialized at: ${params.address}`);

this._setDockReady(true);
Expand Down
1 change: 0 additions & 1 deletion packages/core/lib/services/substrate/api-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export const extrisicErrorsFilter = ({event}) => {
};

export const mapEventToErrorMessage = ({event}) => {
console.log('Failed events', event);
assert(!!event, 'event is required');

const [error] = event.data;
Expand Down
3 changes: 3 additions & 0 deletions packages/core/lib/setup-tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {getRpcClient, initRpcClient} from './rpc-client';
import rpcServer from './rpc-server';
import {setStorage} from './core/storage';

setStorage(global.localStorage);

initRpcClient(req => {
return rpcServer.receive(req).then(result => {
Expand Down
79 changes: 61 additions & 18 deletions packages/core/lib/test/fixtures/legacy-wallet-schema.json
Original file line number Diff line number Diff line change
@@ -1,42 +1,85 @@
{
"doc:d8650f2f-c9c1-4e5b-8b7a-0a2fb617e494": {
"doc:d4bd8145-4a56-456e-9b78-71509184f6ed": {
"@context": ["https://w3id.org/wallet/v1"],
"id": "f5c0569f-ee8f-4add-aab9-0a0ddbae516b",
"name": "Test",
"id": "6ee76a80-bdf2-4cf8-9647-4897ed6feadd",
"name": "Account 1",
"type": "Mnemonic",
"value": "have joy fiber inspire doctor such chicken dream orphan anger transfer noble"
"value": "toe crowd portion cook feel minute deny piece feel barrel member swarm"
},
"doc:c1deff8a-dc55-4a81-8f7f-fe92b3e5687a": {
"doc:74366f2f-a137-46f1-b661-be2956d9afb2": {
"@context": ["https://w3id.org/wallet/v1"],
"id": "3HqV2hrxq27Fw9QhvyKg7PLv2tniZgPRZo4tV4UU58sKqvsw",
"id": "0a7d45d3-8ca4-41a2-93c8-d4bec5652376",
"name": "mnemonic 1",
"type": "Mnemonic",
"value": "exact next frown coconut exit pledge blind program film elephant wife clutch"
},
"doc:670ef0a2-c748-45ab-9515-460d907ba529": {
"@context": ["https://w3id.org/wallet/v1"],
"id": "7e0b6479-59c8-4f76-95d5-d25f7413751f",
"name": "mnemonic1 ",
"type": "Mnemonic",
"value": "exact next frown coconut exit pledge blind program film elephant wife clutch"
},
"doc:497337c2-29fc-4a88-9b78-ee55abd0a500": {
"@context": ["https://w3id.org/wallet/v1"],
"id": "37g1HpqrRjWozRxT5MCpqUDxHQtu2ASPy6yJRD5cqKRVBefS",
"type": "Account",
"correlation": ["f5c0569f-ee8f-4add-aab9-0a0ddbae516b"],
"correlation": ["7e0b6479-59c8-4f76-95d5-d25f7413751f"],
"meta": {
"name": "Test",
"name": "mnemonic1 ",
"keypairType": "sr25519",
"derivationPath": "",
"hasBackup": false,
"hasBackup": true,
"balance": 0
}
},
"doc:eb2a0e70-29b0-498f-80d6-6fb90e84b9ba": {
"doc:069c5733-8516-4dff-aa91-cb3eefc3f16d": {
"@context": ["https://w3id.org/wallet/v1"],
"id": "80f68f17-14a8-4f2d-9865-b1477e605115",
"name": "test 33",
"type": "KeyPair",
"value": {
"encoded": "MFMCAQEwBQYDK2VwBCIEIGhWQpGhRsLAOg8FFbvjUOOZ0UZZX2OJzIKhrTJ2Od1mKEidt13wS9Kxefy98pm/8w9an45To/IuYiKX7PlDyS6hIwMhAP4WQCN9VTz2Hn18GOwXw9ZhVZyCYQnbE5DcZeMamHYt",
"encoding": {
"content": ["pkcs8", "sr25519"],
"type": ["none"],
"version": "3"
},
"address": "3CH1Ce5k516MNoyJvRbq4CtuiSDisa8SF9JBqCcSYRAEzgUk",
"meta": {
"genesisHash": "0x17643cd935f8379c6683c74f739460be4ccc4e4c30b0183bdb2fb9973af242f1",
"isHardware": false,
"name": "test 33",
"tags": [],
"whenCreated": 1654447770598
}
}
},
"doc:d56aa925-2da8-469a-ab4b-c4878e19b9fc": {
"@context": ["https://w3id.org/wallet/v1"],
"id": "3CH1Ce5k516MNoyJvRbq4CtuiSDisa8SF9JBqCcSYRAEzgUk",
"type": "Account",
"correlation": ["80f68f17-14a8-4f2d-9865-b1477e605115"],
"meta": {"name": "test 33", "hasBackup": true, "balance": 0}
},
"doc:69cfaf57-4b04-40b4-9d2a-143c4f05a2e3": {
"@context": ["https://w3id.org/wallet/v1"],
"id": "6abe60ec-c6fd-4acc-8b8b-62edaa548fff",
"name": "Account 3",
"id": "dd3868e5-dab7-4e43-95ac-530a65ef8917",
"name": "ACCOUNT NEW",
"type": "Mnemonic",
"value": "thank mutual remember toilet antique infant cover possible buyer lounge immune hurry"
"value": "surround cotton ecology happy artist opera alert resemble jewel jaguar risk rude"
},
"doc:fad62bc6-d7bd-470c-9445-1b41a31b8387": {
"doc:729e40eb-e36c-460e-a85b-0f9ad2fbf199": {
"@context": ["https://w3id.org/wallet/v1"],
"id": "3CPJGrTMv4XuKm7p2QePJCheZr7ynBn98sueFkH5gmXCLdQV",
"id": "3BcMTtyijuEQYBfSCR45jaZUCgEeCgLCNeJGZm336S638C7G",
"type": "Account",
"correlation": ["6abe60ec-c6fd-4acc-8b8b-62edaa548fff"],
"correlation": ["dd3868e5-dab7-4e43-95ac-530a65ef8917"],
"meta": {
"name": "Account 3",
"name": "ACCOUNT NEW",
"keypairType": "sr25519",
"derivationPath": "",
"hasBackup": false,
"balance": 0
}
}
}
}
2 changes: 1 addition & 1 deletion packages/core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/wallet-sdk-core",
"version": "0.2.1",
"version": "0.2.3",
"license": "MIT",
"repository": {
"type": "git",
Expand Down
4 changes: 2 additions & 2 deletions packages/credentials/package.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "@docknetwork/wallet-sdk-credentials",
"version": "0.2.1",
"version": "0.2.3",
"dependencies": {
"@docknetwork/wallet-sdk-core": "^0.2.1",
"@docknetwork/wallet-sdk-core": "^0.2.3",
"axios": "^0.25.0"
},
"devDependencies": {
Expand Down
2 changes: 1 addition & 1 deletion packages/dids/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@docknetwork/wallet-sdk-dids",
"main": "lib/index.js",
"version": "0.2.1",
"version": "0.2.3",
"dependencies": {
"@digitalbazaar/did-method-key": "^2.0.0",
"uuid": "^8.3.2"
Expand Down
2 changes: 1 addition & 1 deletion packages/react-native/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/wallet-sdk-react-native",
"version": "0.2.1",
"version": "0.2.3",
"dependencies": {
"@polkadot/keyring": "^7.2.1",
"@polkadot/types-known": "5.6.1",
Expand Down
2 changes: 1 addition & 1 deletion packages/transactions/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@docknetwork/wallet-sdk-transactions",
"version": "0.2.1",
"version": "0.2.3",
"dependencies": {
"@polkadot/keyring": "^7.2.1",
"@polkadot/types-known": "5.6.1",
Expand Down

0 comments on commit be3c3c1

Please sign in to comment.