Skip to content

Commit f1a4182

Browse files
committed
Added test for verify signature
1 parent 2f30890 commit f1a4182

File tree

2 files changed

+87
-7
lines changed

2 files changed

+87
-7
lines changed

src/model/account/PublicAccount.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,14 @@ export class PublicAccount {
6868
throw new Error('Missing argument');
6969
}
7070

71-
if (convert.isHexString(signature)) {
72-
throw new Error('Signature must be hexadecimal only');
73-
}
74-
7571
if (signature.length !== 128) {
7672
throw new Error('Signature length is incorrect');
7773
}
7874

75+
if (!convert.isHexString(signature)) {
76+
throw new Error('Signature must be hexadecimal only');
77+
}
78+
7979
// Convert signature key to Uint8Array
8080
const convertedSignature = convert.hexToUint8(signature);
8181

test/model/account/PublicAccount.spec.ts

Lines changed: 83 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@
1414
* limitations under the License.
1515
*/
1616

17-
import {expect} from 'chai';
18-
import {PublicAccount} from '../../../src/model/account/PublicAccount';
19-
import {NetworkType} from '../../../src/model/blockchain/NetworkType';
17+
import { expect } from 'chai';
18+
import { PublicAccount } from '../../../src/model/account/PublicAccount';
19+
import { NetworkType } from '../../../src/model/blockchain/NetworkType';
2020

2121
describe('PublicAccount', () => {
2222
const publicKey = 'b4f12e7c9f6946091e2cb8b6d3a12b50d17ccbbf646386ea27ce2946a7423dcf';
@@ -27,3 +27,83 @@ describe('PublicAccount', () => {
2727
expect(publicAccount.address.plain()).to.be.equal('SARNASAS2BIAB6LMFA3FPMGBPGIJGK6IJETM3ZSP');
2828
});
2929
});
30+
31+
describe('Signature verification', () => {
32+
it('Can verify a signature', () => {
33+
// Arrange:'
34+
const signerPublicAccount = PublicAccount.createFromPublicKey('22816F825B4CACEA334723D51297D8582332D8B875A5829908AAE85831ABB508',
35+
NetworkType.MIJIN_TEST);
36+
const data = 'I am so so so awesome as always';
37+
const signature = 'B01DCA6484026C2ECDF3C822E64DEAAFC15EBCCE337EEE209C28513CB5351CDED8863A8E7B855CD471B55C91FAE611C548625C9A5916A555A24F72F3526FA508'; // tslint:disable-line
38+
39+
// Act & Assert:
40+
expect(PublicAccount.verifySignature(signerPublicAccount, data, signature)).equal(true);
41+
});
42+
43+
it('Throw error if signature has invalid length', () => {
44+
// Arrange:
45+
const signerPublicAccount = PublicAccount.createFromPublicKey('22816F825B4CACEA334723D51297D8582332D8B875A5829908AAE85831ABB508',
46+
NetworkType.MIJIN_TEST);
47+
const data = 'I am so so so awesome as always';
48+
const signature = 'B01DCA6484026C2ECDF3C822E64DEAAFC15EBCCE337EEE209C28513CB5351CDED8863A8E7B855CD471B55C91FAE611C5486'; // tslint:disable-line
49+
50+
// Act & Assert:
51+
expect(() => { PublicAccount.verifySignature(signerPublicAccount, data, signature); }).to.throw('Signature length is incorrect');
52+
});
53+
54+
it('Throw error if signature is not strictly hexadecimal', () => {
55+
// Arrange:
56+
const signerPublicAccount = PublicAccount.createFromPublicKey('22816F825B4CACEA334723D51297D8582332D8B875A5829908AAE85831ABB508',
57+
NetworkType.MIJIN_TEST);
58+
const data = 'I am so so so awesome as always';
59+
const signature = 'B01DCA6484026C2ECDF3C822E64DEAAFC15EBCCE337EEE209C28513CB5351CDED8863A8E7B855CD471B55C91FAE611C548625C9A5916A555A24F72F35a1wwwww';// tslint:disable-line
60+
61+
// Act & Assert:
62+
expect(() => { PublicAccount.verifySignature(signerPublicAccount, data, signature); })
63+
.to.throw('Signature must be hexadecimal only');
64+
});
65+
66+
it('Return false if wrong public key provided', () => {
67+
// Arrange:
68+
const signerPublicAccount = PublicAccount.createFromPublicKey('12816F825B4CACEA334723D51297D8582332D8B875A5829908AAE85831ABB509',
69+
NetworkType.MIJIN_TEST);
70+
const data = 'I am so so so awesome as always';
71+
const signature = 'B01DCA6484026C2ECDF3C822E64DEAAFC15EBCCE337EEE209C28513CB5351CDED8863A8E7B855CD471B55C91FAE611C548625C9A5916A555A24F72F3526FA508';// tslint:disable-line
72+
73+
// Act & Assert:
74+
expect(PublicAccount.verifySignature(signerPublicAccount, data, signature)).equal(false);
75+
});
76+
77+
it('Return false if data is not corresponding to signature provided', () => {
78+
// Arrange:
79+
const signerPublicAccount = PublicAccount.createFromPublicKey('22816F825B4CACEA334723D51297D8582332D8B875A5829908AAE85831ABB508',
80+
NetworkType.MIJIN_TEST);
81+
const data = 'I am awesome as always';
82+
const signature = 'B01DCA6484026C2ECDF3C822E64DEAAFC15EBCCE337EEE209C28513CB5351CDED8863A8E7B855CD471B55C91FAE611C548625C9A5916A555A24F72F3526FA508';// tslint:disable-line
83+
84+
// Act & Assert:
85+
expect(PublicAccount.verifySignature(signerPublicAccount, data, signature)).equal(false);
86+
});
87+
88+
it('Return false if signature is not corresponding to data provided', () => {
89+
// Arrange:
90+
const signerPublicAccount = PublicAccount.createFromPublicKey('22816F825B4CACEA334723D51297D8582332D8B875A5829908AAE85831ABB508',
91+
NetworkType.MIJIN_TEST);
92+
const data = 'I am so so so awesome as always';
93+
const signature = 'A01DCA6484026C2ECDF3C822E64DEAAFC15EBCCE337EEE209C28513CB5351CDED8863A8E7B855CD471B55C91FAE611C548625C9A5916A555A24F72F3526FA509';// tslint:disable-line
94+
95+
// Act & Assert:
96+
expect(PublicAccount.verifySignature(signerPublicAccount, data, signature)).equal(false);
97+
});
98+
99+
it('Throw error if signature verification is missing a parameter', () => {
100+
// Arrange:
101+
const signerPublicAccount = PublicAccount.createFromPublicKey('22816F825B4CACEA334723D51297D8582332D8B875A5829908AAE85831ABB508',
102+
NetworkType.MIJIN_TEST);
103+
const data = '';
104+
const signature = 'B01DCA6484026C2ECDF3C822E64DEAAFC15EBCCE337EEE209C28513CB5351CDED8863A8E7B855CD471B55C91FAE611C548625C9A5916A555A24F72F3526FA508';// tslint:disable-line
105+
106+
// Act & Assert:
107+
expect(() => { PublicAccount.verifySignature(signerPublicAccount, data, signature); }).to.throw('Missing argument');
108+
});
109+
});

0 commit comments

Comments
 (0)