1
- import { jest } from '@jest/globals'
2
- import { Account , Address , PrivateKey , RecordCiphertext , ViewKey } from '../src/node'
3
- import { seed , message , beaconPrivateKeyString , beaconViewKeyString , beaconAddressString , recordCiphertextString , foreignCiphertextString , recordPlaintextString } from './data/account-data' ;
4
-
1
+ import sinon from "sinon" ;
2
+ import { expect } from "chai" ;
3
+ import { Account , Address , PrivateKey , RecordCiphertext , ViewKey } from "../src/node" ;
4
+ import { seed , message , beaconPrivateKeyString , beaconViewKeyString , beaconAddressString , recordCiphertextString , foreignCiphertextString , recordPlaintextString } from "./data/account-data" ;
5
5
6
6
describe ( 'Account' , ( ) => {
7
+ afterEach ( ( ) => {
8
+ sinon . restore ( ) ;
9
+ } ) ;
10
+
7
11
describe ( 'constructors' , ( ) => {
8
- test ( 'creates a new account if no parameters are passed' , ( ) => {
12
+ it ( 'creates a new account if no parameters are passed' , ( ) => {
9
13
// Generate account from rng
10
14
const account = new Account ( ) ;
11
15
12
16
// Test object member type consistency
13
- expect ( account . _privateKey ) . toBeInstanceOf ( PrivateKey ) ;
14
- expect ( account . _viewKey ) . toBeInstanceOf ( ViewKey ) ;
15
- expect ( account . _address ) . toBeInstanceOf ( Address ) ;
17
+ expect ( account . _privateKey ) . instanceof ( PrivateKey ) ;
18
+ expect ( account . _viewKey ) . instanceof ( ViewKey ) ;
19
+ expect ( account . _address ) . instanceof ( Address ) ;
16
20
// Test convenience method type consistency
17
- expect ( account . privateKey ( ) ) . toBeInstanceOf ( PrivateKey ) ;
18
- expect ( account . viewKey ( ) ) . toBeInstanceOf ( ViewKey ) ;
19
- expect ( account . address ( ) ) . toBeInstanceOf ( Address ) ;
21
+ expect ( account . privateKey ( ) ) . instanceof ( PrivateKey ) ;
22
+ expect ( account . viewKey ( ) ) . instanceof ( ViewKey ) ;
23
+ expect ( account . address ( ) ) . instanceof ( Address ) ;
20
24
} ) ;
21
25
22
- test ( 'creates a new from seed' , ( ) => {
26
+ it ( 'creates a new from seed' , ( ) => {
23
27
// Generate account from a seed
24
28
const account = new Account ( { seed : seed } ) ;
25
29
26
30
// Test object member type consistency
27
- expect ( account . _privateKey ) . toBeInstanceOf ( PrivateKey ) ;
28
- expect ( account . _viewKey ) . toBeInstanceOf ( ViewKey ) ;
29
- expect ( account . _address ) . toBeInstanceOf ( Address ) ;
31
+ expect ( account . _privateKey ) . instanceof ( PrivateKey ) ;
32
+ expect ( account . _viewKey ) . instanceof ( ViewKey ) ;
33
+ expect ( account . _address ) . instanceof ( Address ) ;
30
34
// Test convenience method type consistency
31
- expect ( account . privateKey ( ) ) . toBeInstanceOf ( PrivateKey ) ;
32
- expect ( account . viewKey ( ) ) . toBeInstanceOf ( ViewKey ) ;
33
- expect ( account . address ( ) ) . toBeInstanceOf ( Address ) ;
35
+ expect ( account . privateKey ( ) ) . instanceof ( PrivateKey ) ;
36
+ expect ( account . viewKey ( ) ) . instanceof ( ViewKey ) ;
37
+ expect ( account . address ( ) ) . instanceof ( Address ) ;
34
38
// Test that expected output is generated
35
- expect ( account . privateKey ( ) . to_string ( ) ) . toEqual ( beaconPrivateKeyString ) ;
36
- expect ( account . viewKey ( ) . to_string ( ) ) . toEqual ( beaconViewKeyString ) ;
37
- expect ( account . address ( ) . to_string ( ) ) . toEqual ( beaconAddressString ) ;
38
- expect ( account . toString ( ) ) . toEqual ( beaconAddressString ) ;
39
+ expect ( account . privateKey ( ) . to_string ( ) ) . equal ( beaconPrivateKeyString ) ;
40
+ expect ( account . viewKey ( ) . to_string ( ) ) . equal ( beaconViewKeyString ) ;
41
+ expect ( account . address ( ) . to_string ( ) ) . equal ( beaconAddressString ) ;
42
+ expect ( account . toString ( ) ) . equal ( beaconAddressString ) ;
39
43
} ) ;
40
44
41
- test ( 'throws an error if parameters are invalid' , ( ) => {
42
- expect ( ( ) => new Account ( { privateKey : 'invalidPrivateKey' } ) ) . toThrow ( ) ;
45
+ it ( 'throws an error if parameters are invalid' , ( ) => {
46
+ expect ( ( ) => new Account ( { privateKey : 'invalidPrivateKey' } ) ) . throw ( ) ;
43
47
} ) ;
44
48
45
- test ( 'creates an account object from a valid private key string' , ( ) => {
49
+ it ( 'creates an account object from a valid private key string' , ( ) => {
46
50
// Generate account from valid private key string
47
51
const account = new Account ( { privateKey : beaconPrivateKeyString } ) ;
48
52
49
53
// Test object member type consistency
50
- expect ( account . _privateKey ) . toBeInstanceOf ( PrivateKey ) ;
51
- expect ( account . _viewKey ) . toBeInstanceOf ( ViewKey ) ;
52
- expect ( account . _address ) . toBeInstanceOf ( Address ) ;
54
+ expect ( account . _privateKey ) . instanceof ( PrivateKey ) ;
55
+ expect ( account . _viewKey ) . instanceof ( ViewKey ) ;
56
+ expect ( account . _address ) . instanceof ( Address ) ;
53
57
// Test convenience method type consistency
54
- expect ( account . privateKey ( ) ) . toBeInstanceOf ( PrivateKey ) ;
55
- expect ( account . viewKey ( ) ) . toBeInstanceOf ( ViewKey ) ;
56
- expect ( account . address ( ) ) . toBeInstanceOf ( Address ) ;
58
+ expect ( account . privateKey ( ) ) . instanceof ( PrivateKey ) ;
59
+ expect ( account . viewKey ( ) ) . instanceof ( ViewKey ) ;
60
+ expect ( account . address ( ) ) . instanceof ( Address ) ;
57
61
// Test that expected output is generated
58
- expect ( account . privateKey ( ) . to_string ( ) ) . toEqual ( beaconPrivateKeyString ) ;
59
- expect ( account . viewKey ( ) . to_string ( ) ) . toEqual ( beaconViewKeyString ) ;
60
- expect ( account . address ( ) . to_string ( ) ) . toEqual ( beaconAddressString ) ;
61
- expect ( account . toString ( ) ) . toEqual ( beaconAddressString ) ;
62
+ expect ( account . privateKey ( ) . to_string ( ) ) . equal ( beaconPrivateKeyString ) ;
63
+ expect ( account . viewKey ( ) . to_string ( ) ) . equal ( beaconViewKeyString ) ;
64
+ expect ( account . address ( ) . to_string ( ) ) . equal ( beaconAddressString ) ;
65
+ expect ( account . toString ( ) ) . equal ( beaconAddressString ) ;
62
66
} ) ;
63
67
64
- test ( 'can encrypt an account and decrypt to the same account' , ( ) => {
68
+ it ( 'can encrypt an account and decrypt to the same account' , ( ) => {
65
69
const newAccount = new Account ( ) ;
66
70
const privateKeyCiphertext = newAccount . encryptAccount ( "mypassword" ) ;
67
71
const privateKeyCiphertextString = privateKeyCiphertext . toString ( ) ;
@@ -72,14 +76,14 @@ describe('Account', () => {
72
76
73
77
for ( const account of [ accountFromString , accountFromObject ] ) {
74
78
// Test that expected output is generated
75
- expect ( account . privateKey ( ) . to_string ( ) ) . toEqual ( newAccount . privateKey ( ) . to_string ( ) ) ;
76
- expect ( account . viewKey ( ) . to_string ( ) ) . toEqual ( newAccount . viewKey ( ) . to_string ( ) ) ;
77
- expect ( account . address ( ) . to_string ( ) ) . toEqual ( newAccount . toString ( ) ) ;
78
- expect ( account . toString ( ) ) . toEqual ( newAccount . toString ( ) ) ;
79
+ expect ( account . privateKey ( ) . to_string ( ) ) . equal ( newAccount . privateKey ( ) . to_string ( ) ) ;
80
+ expect ( account . viewKey ( ) . to_string ( ) ) . equal ( newAccount . viewKey ( ) . to_string ( ) ) ;
81
+ expect ( account . address ( ) . to_string ( ) ) . equal ( newAccount . toString ( ) ) ;
82
+ expect ( account . toString ( ) ) . equal ( newAccount . toString ( ) ) ;
79
83
}
80
84
} ) ;
81
85
82
- test ( 'fails to create an account from a bad password' , ( ) => {
86
+ it ( 'fails to create an account from a bad password' , ( ) => {
83
87
const newAccount = new Account ( ) ;
84
88
const privateKeyCiphertext = newAccount . encryptAccount ( "mypassword" ) ;
85
89
const privateKeyCiphertextString = privateKeyCiphertext . toString ( ) ;
@@ -89,28 +93,30 @@ describe('Account', () => {
89
93
Account . fromCiphertext ( privateKeyCiphertext , "badpassword" ) ;
90
94
91
95
// Should not get here
92
- expect ( true ) . toBe ( false ) ;
96
+ expect ( true ) . equal ( false ) ;
93
97
} catch ( err ) {
94
98
// The account should fail to decrypt
95
- expect ( true ) . toBe ( true ) ;
99
+ expect ( true ) . equal ( true ) ;
96
100
}
97
101
} ) ;
98
102
} ) ;
99
103
100
104
describe ( 'View Key Record Decryption' , ( ) => {
101
- test ( 'decrypts a record in ciphertext form' , ( ) => {
105
+ it ( 'decrypts a record in ciphertext form' , ( ) => {
102
106
const account = new Account ( { privateKey : "APrivateKey1zkpJkyYRGYtkeHDaFfwsKtUJzia7csiWhfBWPXWhXJzy9Ls" } ) ;
103
- const decrypt_spy = jest . spyOn ( account . _viewKey , 'decrypt' ) ;
107
+
108
+ const decrypt_spy = sinon . spy ( account . _viewKey , 'decrypt' ) ;
109
+
104
110
// Decrypt record the private key owns
105
111
const decryptedRecord = account . decryptRecord ( recordCiphertextString ) ;
106
112
107
113
// Ensure the underlying wasm is being called with the right data
108
- expect ( decrypt_spy ) . toHaveBeenCalledWith ( recordCiphertextString ) ;
114
+ expect ( decrypt_spy . calledWith ( recordCiphertextString ) ) . equal ( true ) ;
109
115
// Ensure it decrypts to the correct data
110
- expect ( decryptedRecord ) . toBe ( recordPlaintextString ) ;
116
+ expect ( decryptedRecord ) . equal ( recordPlaintextString ) ;
111
117
} ) ;
112
118
113
- test ( 'doesnt decrypt records from other accounts nor identifies them as the record owner' , ( ) => {
119
+ it ( 'doesnt decrypt records from other accounts nor identifies them as the record owner' , ( ) => {
114
120
function tryDecrypt ( ) {
115
121
try {
116
122
return account . decryptRecord ( foreignCiphertextString ) ;
@@ -119,62 +125,63 @@ describe('Account', () => {
119
125
}
120
126
}
121
127
const account = new Account ( { privateKey : "APrivateKey1zkpJkyYRGYtkeHDaFfwsKtUJzia7csiWhfBWPXWhXJzy9Ls" } ) ;
122
- const decrypt_spy = jest . spyOn ( account . _viewKey , 'decrypt' ) ;
128
+ const decrypt_spy = sinon . spy ( account . _viewKey , 'decrypt' ) ;
123
129
const recordCiphertext = RecordCiphertext . fromString ( foreignCiphertextString ) ;
124
130
125
131
// Ensure a foreign record decryption attempt throws
126
- expect ( tryDecrypt ) . toThrow ( ) ;
132
+ expect ( tryDecrypt ) . throw ( ) ;
127
133
// Ensure the underlying wasm is being called with the right data
128
- expect ( decrypt_spy ) . toHaveBeenCalledWith ( foreignCiphertextString ) ;
134
+ expect ( decrypt_spy . calledWith ( foreignCiphertextString ) ) . equal ( true ) ;
129
135
// Ensure the account doesn't identify the record ciphertext as its own from both string and object forms
130
- expect ( account . ownsRecordCiphertext ( foreignCiphertextString ) ) . toBe ( false ) ;
131
- expect ( account . ownsRecordCiphertext ( recordCiphertext ) ) . toBe ( false ) ;
136
+ expect ( account . ownsRecordCiphertext ( foreignCiphertextString ) ) . equal ( false ) ;
137
+ expect ( account . ownsRecordCiphertext ( recordCiphertext ) ) . equal ( false ) ;
132
138
133
139
} ) ;
134
140
135
- test ( 'decrypts an array of records in ciphertext form' , ( ) => {
141
+ it ( 'decrypts an array of records in ciphertext form' , ( ) => {
136
142
const account = new Account ( { privateKey : "APrivateKey1zkpJkyYRGYtkeHDaFfwsKtUJzia7csiWhfBWPXWhXJzy9Ls" } ) ;
137
143
const ciphertexts = [ recordCiphertextString , recordCiphertextString ] ;
138
- const decrypt_spy = jest . spyOn ( account . _viewKey , 'decrypt' ) ;
144
+ const decrypt_spy = sinon . spy ( account . _viewKey , 'decrypt' ) ;
139
145
const decryptedRecords = account . decryptRecords ( ciphertexts ) ;
140
146
141
147
// Ensure the right number of calls were called and right inputs were passed
142
- expect ( decrypt_spy ) . toHaveBeenCalledTimes ( 2 ) ;
143
- expect ( decrypt_spy ) . toHaveBeenCalledWith ( ciphertexts [ 0 ] ) ;
144
- expect ( decrypt_spy ) . toHaveBeenCalledWith ( ciphertexts [ 1 ] ) ;
148
+ expect ( decrypt_spy . callCount ) . equal ( 2 ) ;
149
+ expect ( decrypt_spy . calledWith ( ciphertexts [ 0 ] ) ) . equal ( true ) ;
150
+ expect ( decrypt_spy . calledWith ( ciphertexts [ 1 ] ) ) . equal ( true ) ;
145
151
// Ensure the records were decrypted to the correct data
146
- expect ( decryptedRecords ) . toEqual ( [ recordPlaintextString , recordPlaintextString ] ) ;
152
+ expect ( decryptedRecords ) . deep . equal ( [ recordPlaintextString , recordPlaintextString ] ) ;
147
153
} ) ;
148
154
} ) ;
149
155
150
156
describe ( 'Sign and Verify' , ( ) => {
151
157
152
- test ( "verifies the signature on a message" , ( ) => {
158
+ it ( "verifies the signature on a message" , ( ) => {
153
159
const account = new Account ( ) ;
154
160
const other_message = Uint8Array . from ( [ 104 , 101 , 108 , 108 , 111 , 32 , 120 , 121 , 114 , 108 , 99 ] ) ;
155
- const sign_spy = jest . spyOn ( account . _privateKey , 'sign' ) ;
161
+ const sign_spy = sinon . spy ( account . _privateKey , 'sign' ) ;
156
162
const signature = account . sign ( message ) ;
157
163
158
164
// Ensure the signature was called with the right message
159
- expect ( sign_spy ) . lastCalledWith ( message ) ;
165
+ expect ( sign_spy . calledWith ( message ) ) . equal ( true ) ;
160
166
const other_signature = account . sign ( other_message ) ;
161
167
// Ensure the signature was called with the right message and the right number of times
162
- expect ( sign_spy ) . lastCalledWith ( other_message ) ;
163
- expect ( sign_spy ) . toHaveBeenCalledTimes ( 2 ) ;
164
- const verify_spy = jest . spyOn ( account . _address , 'verify' ) ;
168
+ expect ( sign_spy . calledWith ( other_message ) ) . equal ( true ) ;
169
+ expect ( sign_spy . callCount ) . equal ( 2 ) ;
170
+
171
+ const verify_spy = sinon . spy ( account . _address , 'verify' ) ;
165
172
const isValid = account . verify ( message , signature ) ;
166
- expect ( verify_spy ) . lastCalledWith ( message , signature ) ;
173
+ expect ( verify_spy . calledWith ( message , signature ) ) . equal ( true ) ;
167
174
const isSigValidForWrongMessage = account . verify ( other_message , signature ) ;
168
- expect ( verify_spy ) . lastCalledWith ( other_message , signature ) ;
175
+ expect ( verify_spy . calledWith ( other_message , signature ) ) . equal ( true ) ;
169
176
const isSigValidForMultipleMessages = account . verify ( other_message , other_signature ) ;
170
- expect ( verify_spy ) . lastCalledWith ( other_message , other_signature ) ;
171
- expect ( verify_spy ) . toHaveBeenCalledTimes ( 3 ) ;
177
+ expect ( verify_spy . calledWith ( other_message , other_signature ) ) . equal ( true ) ;
178
+ expect ( verify_spy . callCount ) . equal ( 3 ) ;
172
179
// Ensure the signature was valid
173
- expect ( isValid ) . toBe ( true ) ;
180
+ expect ( isValid ) . equal ( true ) ;
174
181
// Ensure the second message was valid
175
- expect ( isSigValidForMultipleMessages ) . toBe ( true ) ;
182
+ expect ( isSigValidForMultipleMessages ) . equal ( true ) ;
176
183
// Ensure a signature & message mismatch is invalid
177
- expect ( isSigValidForWrongMessage ) . toBe ( false ) ;
184
+ expect ( isSigValidForWrongMessage ) . equal ( false ) ;
178
185
} ) ;
179
186
} ) ;
180
187
} ) ;
0 commit comments