Skip to content

Commit b7e6858

Browse files
authored
Added check on Account.signData and PublicAccount.verifySignature for hexadecimal (#528)
* Fixed #252 - Added check on Account.signData and PublicAccount.verifySignature for hexadecimal * trigger travis * Added isHexadecimal argument to account.signData and publicAccount.verifySignature * Fixed #525 * Removed unused condition * refactored signData and verifySignature
1 parent e6562d5 commit b7e6858

File tree

6 files changed

+69
-16
lines changed

6 files changed

+69
-16
lines changed

src/model/account/PublicAccount.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,20 +68,16 @@ export class PublicAccount {
6868
if (!signature) {
6969
throw new Error('Missing argument');
7070
}
71-
7271
if (signature.length / 2 !== Hash512) {
7372
throw new Error('Signature length is incorrect');
7473
}
75-
7674
if (!Convert.isHexString(signature)) {
7775
throw new Error('Signature must be hexadecimal only');
7876
}
7977
// Convert signature key to Uint8Array
8078
const convertedSignature = Convert.hexToUint8(signature);
81-
8279
// Convert to Uint8Array
83-
84-
const convertedData = Convert.hexToUint8(Convert.isHexString(data) ? data : Convert.utf8ToHex(data));
80+
const convertedData = Convert.hexToUint8(Convert.utf8ToHex(data));
8581
return KeyPair.verify(Convert.hexToUint8(this.publicKey), convertedData, convertedSignature);
8682
}
8783

src/model/wallet/SimpleWallet.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ export class SimpleWallet extends Wallet {
127127
* @returns {ISimpleWalletDTO}
128128
*/
129129
public toDTO(): ISimpleWalletDTO {
130-
return JSON.parse(JSON.stringify(this))
130+
return JSON.parse(JSON.stringify(this));
131131
}
132132

133133
/**

test/core/crypto/utilities.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,11 @@ import {Convert} from '../../../src/core/format';
2020
describe('crypto utilities', () => {
2121
it('words2ua creates an ua from a word array', () => {
2222
// Arrange:
23-
const key = '4344645752e57065f814b51713d05810'
23+
const key = '4344645752e57065f814b51713d05810';
2424
const words = [ 1128555607, 1390768229, -132860649, 332421136 ];
2525

2626
// Act:
27-
const ua = words2ua(new Uint8Array(16), {words})
27+
const ua = words2ua(new Uint8Array(16), {words});
2828

2929
// Assert:
3030
expect(ua).deep.equal(Convert.hexToUint8(key));

test/model/account/Account.spec.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,5 +97,64 @@ describe('Account', () => {
9797
expect(publicAccount.verifySignature('0xAA', signed))
9898
.to.be.true;
9999
});
100+
101+
it('hexa without 0x previx', () => {
102+
const account = Account.createFromPrivateKey(
103+
'AB860ED1FE7C91C02F79C02225DAC708D7BD13369877C1F59E678CC587658C47',
104+
NetworkType.MIJIN_TEST,
105+
);
106+
const publicAccount = account.publicAccount;
107+
const signed = account.signData('66128B29E8197352A2FEB51B50CF5D02F1D05B20D44B3F7953B98ACD2BCA15D4');
108+
expect(publicAccount.verifySignature('66128B29E8197352A2FEB51B50CF5D02F1D05B20D44B3F7953B98ACD2BCA15D4', signed))
109+
.to.be.true;
110+
});
111+
112+
it('hexa without 0x previx should be the same as with 0x', () => {
113+
const account = Account.createFromPrivateKey(
114+
'AB860ED1FE7C91C02F79C02225DAC708D7BD13369877C1F59E678CC587658C47',
115+
NetworkType.MIJIN_TEST,
116+
);
117+
const publicAccount = account.publicAccount;
118+
const signed = account.signData('AA');
119+
const signedWith0x = account.signData('0xAA');
120+
expect(publicAccount.verifySignature('AA', signed))
121+
.to.be.true;
122+
expect(publicAccount.verifySignature('0xAA', signedWith0x))
123+
.to.be.true;
124+
});
125+
126+
it('hexa without 0x previx should be the same as with 0x', () => {
127+
const account = Account.createFromPrivateKey(
128+
'AB860ED1FE7C91C02F79C02225DAC708D7BD13369877C1F59E678CC587658C47',
129+
NetworkType.MIJIN_TEST,
130+
);
131+
const publicAccount = account.publicAccount;
132+
const signed = account.signData('ff60983e0c5d21d2fb83c67598d560f3cf0e28ae667b5616aaa58a059666cd8cf826b026243c92cf');
133+
const signedWith0x =
134+
account.signData('0xff60983e0c5d21d2fb83c67598d560f3cf0e28ae667b5616aaa58a059666cd8cf826b026243c92cf');
135+
expect(
136+
publicAccount
137+
.verifySignature('ff60983e0c5d21d2fb83c67598d560f3cf0e28ae667b5616aaa58a059666cd8cf826b026243c92cf', signed))
138+
.to.be.true;
139+
expect(publicAccount
140+
.verifySignature('0xff60983e0c5d21d2fb83c67598d560f3cf0e28ae667b5616aaa58a059666cd8cf826b026243c92cf', signedWith0x))
141+
.to.be.true;
142+
});
143+
144+
it('sign empty', () => {
145+
const account = Account.createFromPrivateKey(
146+
'AB860ED1FE7C91C02F79C02225DAC708D7BD13369877C1F59E678CC587658C47',
147+
NetworkType.MIJIN_TEST,
148+
);
149+
const publicAccount = account.publicAccount;
150+
const signed = account.signData('');
151+
const signedWith0x = account.signData('0x');
152+
expect(
153+
publicAccount.verifySignature('', signed))
154+
.to.be.true;
155+
expect(publicAccount
156+
.verifySignature('0x', signedWith0x))
157+
.to.be.true;
158+
});
100159
});
101160
});

test/model/account/PublicAccount.spec.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { expect } from 'chai';
1818
import { Account } from '../../../src/model/account/Account';
1919
import { PublicAccount } from '../../../src/model/account/PublicAccount';
2020
import { NetworkType } from '../../../src/model/network/NetworkType';
21+
import { TestingAccount } from '../../conf/conf.spec';
2122

2223
describe('PublicAccount', () => {
2324
const publicKey = 'b4f12e7c9f6946091e2cb8b6d3a12b50d17ccbbf646386ea27ce2946a7423dcf';
@@ -32,12 +33,9 @@ describe('PublicAccount', () => {
3233
describe('Signature verification', () => {
3334
it('Can verify a signature', () => {
3435
// Arrange:'
35-
const signerPublicAccount = PublicAccount.createFromPublicKey(
36-
'16FB59F907524009730BCB9F860C8C5A1109A9E8F194275DA0B9F5A2085E2D02',
37-
NetworkType.MIJIN_TEST);
36+
const signerPublicAccount = TestingAccount.publicAccount;
3837
const data = 'ff60983e0c5d21d2fb83c67598d560f3cf0e28ae667b5616aaa58a059666cd8cf826b026243c92cf';
39-
const signature = '2E32A8A934C2B8BC54A1594643A866CCDB3166BD41B6DE3E0C9FC779E7F3F421A0BCC798408ACCC92F47A3A45EF237D5CB7473D768991EE79AC659E1DA8CBB0C'; // tslint:disable-line
40-
38+
const signature = TestingAccount.signData(data);
4139
// Act & Assert:
4240
expect(signerPublicAccount.verifySignature(data, signature)).to.be.true;
4341
});

test/model/wallet/SimpleWallet.spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ describe('SimpleWallet', () => {
6565
const privateKey = '5149a02ca2b2610138376717daaff8477f1639796aa108b7eee83e99e585b250';
6666
const password = new Password('password');
6767
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-
})
68+
const simpleWalletDTO = simpleWallet.toDTO();
69+
expect(simpleWalletDTO).to.deep.equal(JSON.parse(JSON.stringify(simpleWallet)));
70+
});
7171
});

0 commit comments

Comments
 (0)