Skip to content

Commit

Permalink
add SimpleWallet.toDTO, fixes #504 (#508)
Browse files Browse the repository at this point in the history
* add SimpleWallet.toDTO, fixes #504

* add word2ua test
  • Loading branch information
decentraliser committed Apr 1, 2020
1 parent cb0039b commit 1119a0c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/model/wallet/SimpleWallet.ts
Expand Up @@ -122,6 +122,14 @@ export class SimpleWallet extends Wallet {
);
}

/**
* Creates a SimpleWallet DTO
* @returns {ISimpleWalletDTO}
*/
public toDTO(): ISimpleWalletDTO {
return JSON.parse(JSON.stringify(this))
}

/**
* Open a wallet and generate an Account
* @param password - Password to decrypt private key
Expand Down
32 changes: 32 additions & 0 deletions test/core/crypto/utilities.spec.ts
@@ -0,0 +1,32 @@
/*
* Copyright 2019 NEM
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {expect} from 'chai';
import {words2ua} from '../../../src/core/crypto/Utilities';
import {Convert} from '../../../src/core/format';

describe('crypto utilities', () => {
it('words2ua creates an ua from a word array', () => {
// Arrange:
const key = '4344645752e57065f814b51713d05810'
const words = [ 1128555607, 1390768229, -132860649, 332421136 ];

// Act:
const ua = words2ua(new Uint8Array(16), {words})

// Assert:
expect(ua).deep.equal(Convert.hexToUint8(key));
});
});
8 changes: 8 additions & 0 deletions test/model/wallet/SimpleWallet.spec.ts
Expand Up @@ -60,4 +60,12 @@ describe('SimpleWallet', () => {
const account2 = simpleWallet2.open(password);
expect(account).to.deep.equal(account2);
});

it('should create a simple wallet DTO', () => {
const privateKey = '5149a02ca2b2610138376717daaff8477f1639796aa108b7eee83e99e585b250';
const password = new Password('password');
const simpleWallet = SimpleWallet.createFromPrivateKey('wallet-name', password, privateKey, NetworkType.MIJIN_TEST);
const simpleWalletDTO = simpleWallet.toDTO()
expect(simpleWalletDTO).to.deep.equal(JSON.parse(JSON.stringify(simpleWallet)))
})
});

0 comments on commit 1119a0c

Please sign in to comment.