Skip to content

Commit 8be3796

Browse files
committed
create SimpleWallet from DTO, fixes #392
1 parent a636a12 commit 8be3796

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
* Catapult REST Endpoints
18+
* No description provided (generated by Openapi Generator https://github.com/openapitools/openapi-generator)
19+
*
20+
* The version of the OpenAPI document: 0.7.21
21+
*
22+
*
23+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
24+
* https://openapi-generator.tech
25+
* Do not edit the class manually.
26+
*/
27+
28+
/**
29+
* Used to instantiate a SimpleWallet
30+
*/
31+
export interface ISimpleWalletDTO {
32+
name: string;
33+
network: number;
34+
address: {
35+
address: string
36+
networkType: number,
37+
};
38+
creationDate: string;
39+
schema: string;
40+
encryptedPrivateKey: {
41+
encryptedKey: string
42+
iv: string,
43+
};
44+
}

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/model/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)