Skip to content

Commit 51c2392

Browse files
authored
Merge branch 'master' into task/g381_repositoryInterfacesAndFactory
2 parents 8bc9597 + dd7ad3b commit 51c2392

File tree

3 files changed

+63
-1
lines changed

3 files changed

+63
-1
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Copyright 2019 NEM
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
/**
18+
* Used to instantiate a SimpleWallet
19+
*/
20+
export interface ISimpleWalletDTO {
21+
name: string;
22+
network: number;
23+
address: {
24+
address: string
25+
networkType: number,
26+
};
27+
creationDate: string;
28+
schema: string;
29+
encryptedPrivateKey: {
30+
encryptedKey: string
31+
iv: string,
32+
};
33+
}

src/model/wallet/SimpleWallet.ts

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@
1616

1717
import {LocalDateTime} from 'js-joda';
1818
import {Crypto, KeyPair, SHA3Hasher} from '../../core/crypto';
19-
import { Convert as convert} from '../../core/format';
19+
import {Convert as convert} from '../../core/format';
20+
import {ISimpleWalletDTO} from '../../infrastructure/wallet/simpleWalletDTO';
2021
import {Account} from '../account/Account';
2122
import {Address} from '../account/Address';
2223
import {NetworkType} from '../blockchain/NetworkType';
@@ -105,6 +106,23 @@ export class SimpleWallet extends Wallet {
105106
return new SimpleWallet(name, network, address, LocalDateTime.now(), encryptedPrivateKey);
106107
}
107108

109+
/**
110+
* Instantiate a SimpleWallet from a DTO
111+
* @param simpleWalletDTO simple wallet without prototype
112+
*/
113+
static createFromDTO(simpleWalletDTO: ISimpleWalletDTO) {
114+
return new SimpleWallet(
115+
simpleWalletDTO.name,
116+
simpleWalletDTO.network,
117+
Address.createFromRawAddress(simpleWalletDTO.address.address),
118+
LocalDateTime.now(),
119+
new EncryptedPrivateKey(
120+
simpleWalletDTO.encryptedPrivateKey.encryptedKey,
121+
simpleWalletDTO.encryptedPrivateKey.iv,
122+
),
123+
);
124+
}
125+
108126
/**
109127
* Open a wallet and generate an Account
110128
* @param password - Password to decrypt private key

test/model/wallet/SimpleWallet.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,15 @@ describe('SimpleWallet', () => {
4949
const account = simpleWallet.open(new Password('password'));
5050
expect(simpleWallet.address.plain()).to.be.equal(account.address.plain());
5151
});
52+
53+
it('should open a simple wallet from a simple wallet without prototype', () => {
54+
const privateKey = '5149a02ca2b2610138376717daaff8477f1639796aa108b7eee83e99e585b250';
55+
const password = new Password('password');
56+
const simpleWallet = SimpleWallet.createFromPrivateKey('wallet-name', password, privateKey, NetworkType.MIJIN_TEST);
57+
const account = simpleWallet.open(new Password('password'));
58+
const simpleWalletNoProto = JSON.parse(JSON.stringify(simpleWallet));
59+
const simpleWallet2 = SimpleWallet.createFromDTO(simpleWalletNoProto);
60+
const account2 = simpleWallet2.open(password);
61+
expect(account).to.deep.equal(account2);
62+
});
5263
});

0 commit comments

Comments
 (0)