Skip to content

Commit 1119a0c

Browse files
add SimpleWallet.toDTO, fixes #504 (#508)
* add SimpleWallet.toDTO, fixes #504 * add word2ua test
1 parent cb0039b commit 1119a0c

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

src/model/wallet/SimpleWallet.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ export class SimpleWallet extends Wallet {
122122
);
123123
}
124124

125+
/**
126+
* Creates a SimpleWallet DTO
127+
* @returns {ISimpleWalletDTO}
128+
*/
129+
public toDTO(): ISimpleWalletDTO {
130+
return JSON.parse(JSON.stringify(this))
131+
}
132+
125133
/**
126134
* Open a wallet and generate an Account
127135
* @param password - Password to decrypt private key

test/core/crypto/utilities.spec.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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+
import {expect} from 'chai';
17+
import {words2ua} from '../../../src/core/crypto/Utilities';
18+
import {Convert} from '../../../src/core/format';
19+
20+
describe('crypto utilities', () => {
21+
it('words2ua creates an ua from a word array', () => {
22+
// Arrange:
23+
const key = '4344645752e57065f814b51713d05810'
24+
const words = [ 1128555607, 1390768229, -132860649, 332421136 ];
25+
26+
// Act:
27+
const ua = words2ua(new Uint8Array(16), {words})
28+
29+
// Assert:
30+
expect(ua).deep.equal(Convert.hexToUint8(key));
31+
});
32+
});

test/model/wallet/SimpleWallet.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,12 @@ describe('SimpleWallet', () => {
6060
const account2 = simpleWallet2.open(password);
6161
expect(account).to.deep.equal(account2);
6262
});
63+
64+
it('should create a simple wallet DTO', () => {
65+
const privateKey = '5149a02ca2b2610138376717daaff8477f1639796aa108b7eee83e99e585b250';
66+
const password = new Password('password');
67+
const simpleWallet = SimpleWallet.createFromPrivateKey('wallet-name', password, privateKey, NetworkType.MIJIN_TEST);
68+
const simpleWalletDTO = simpleWallet.toDTO()
69+
expect(simpleWalletDTO).to.deep.equal(JSON.parse(JSON.stringify(simpleWallet)))
70+
})
6371
});

0 commit comments

Comments
 (0)