1
1
// eslint-disable-next-line no-unused-vars
2
- import { expect , describe , test , it , jest , afterAll , afterEach , beforeAll , beforeEach } from '@jest/globals' // prettier-ignore
2
+ import { describe , it , expect , test } from 'vitest'
3
3
import {
4
4
fromPrivate ,
5
5
toChecksum ,
@@ -12,6 +12,11 @@ import {
12
12
isBuiltInAddress ,
13
13
isCfxHexAddress ,
14
14
validateHexAddress ,
15
+ isChecksummed ,
16
+ randomAddressType ,
17
+ randomCfxHexAddress ,
18
+ randomPrivateKey ,
19
+ validatePrivateKey ,
15
20
} from './'
16
21
import {
17
22
NULL_HEX_ADDRESS ,
@@ -30,24 +35,24 @@ const checksumAddress = '0xED54a7C1d8634BB589f24Bb7F05a5554b36F9618'
30
35
// const s = '0x129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66'
31
36
// const v = '0x1b' // 27
32
37
33
- describe ( 'account' , function ( ) {
34
- describe ( 'fromPrivate' , function ( ) {
35
- it ( 'should return the right address' , async function ( ) {
38
+ describe ( 'account' , ( ) => {
39
+ describe ( 'fromPrivate' , ( ) => {
40
+ it ( 'should return the right address' , async ( ) => {
36
41
expect ( fromPrivate ( ecprivkey ) ) . toEqual ( {
37
42
address : checksumAddress ,
38
43
privateKey : ecprivkey ,
39
44
} )
40
45
} )
41
46
} )
42
47
43
- describe ( 'toChecksum' , function ( ) {
44
- it ( 'should return the right checksum address' , async function ( ) {
48
+ describe ( 'toChecksum' , ( ) => {
49
+ it ( 'should return the right checksum address' , async ( ) => {
45
50
expect ( toChecksum ( address ) ) . toEqual ( checksumAddress )
46
51
} )
47
52
} )
48
53
49
- describe ( 'create' , function ( ) {
50
- it ( 'should return the private key and address' , async function ( ) {
54
+ describe ( 'create' , ( ) => {
55
+ it ( 'should return the private key and address' , async ( ) => {
51
56
const account = create ( )
52
57
expect ( account . address ) . toEqual (
53
58
expect . stringMatching ( / ^ 0 x [ 0 - 9 a - f A - F ] { 40 } $ / ) ,
@@ -56,28 +61,31 @@ describe('account', function () {
56
61
} )
57
62
} )
58
63
59
- describe ( 'randomHexAddress' , function ( ) {
60
- it ( 'should generate a random builtin address' , async function ( ) {
64
+ describe ( 'randomHexAddress' , ( ) => {
65
+ it ( 'should generate a random builtin address' , async ( ) => {
61
66
expect (
62
67
INTERNAL_CONTRACTS_HEX_ADDRESS . includes ( randomHexAddress ( 'builtin' ) ) ,
63
68
) . toBe ( true )
64
69
} )
65
70
66
- it ( 'should generate the null address' , async function ( ) {
71
+ it ( 'should generate the null address' , async ( ) => {
67
72
expect ( randomHexAddress ( 'null' ) ) . toEqual ( NULL_HEX_ADDRESS )
68
73
} )
69
74
70
- it ( 'should generate a contract address' , async function ( ) {
75
+ it ( 'should generate a contract address' , async ( ) => {
71
76
expect ( randomHexAddress ( 'contract' ) . startsWith ( '0x8' ) ) . toBe ( true )
72
77
} )
73
78
74
- it ( 'should generate a account address' , async function ( ) {
79
+ it ( 'should generate a account address' , async ( ) => {
75
80
expect ( randomHexAddress ( 'user' ) . startsWith ( '0x1' ) ) . toBe ( true )
76
81
} )
82
+ it ( 'no args' , ( ) => {
83
+ expect ( randomHexAddress ( ) ) . toBeDefined ( )
84
+ } )
77
85
} )
78
86
79
- describe ( 'checks' , function ( ) {
80
- it ( 'isHexAddress' , function ( ) {
87
+ describe ( 'checks' , ( ) => {
88
+ it ( 'isHexAddress' , ( ) => {
81
89
expect ( isHexAddress ( '0x0000000000000000000000000000000000000000' ) ) . toBe (
82
90
true ,
83
91
)
@@ -86,13 +94,13 @@ describe('account', function () {
86
94
)
87
95
} )
88
96
89
- it ( 'isNullHexAddress' , function ( ) {
97
+ it ( 'isNullHexAddress' , ( ) => {
90
98
expect (
91
99
isNullHexAddress ( '0x0000000000000000000000000000000000000000' ) ,
92
100
) . toBe ( true )
93
101
} )
94
102
95
- it ( 'isContractAddress' , function ( ) {
103
+ it ( 'isContractAddress' , ( ) => {
96
104
expect (
97
105
isContractAddress ( '0x8000000000000000000000000000000000000000' ) ,
98
106
) . toBe ( true )
@@ -104,7 +112,7 @@ describe('account', function () {
104
112
) . toBe ( false )
105
113
} )
106
114
107
- it ( 'isUserHexAddress' , function ( ) {
115
+ it ( 'isUserHexAddress' , ( ) => {
108
116
expect (
109
117
isUserHexAddress ( '0x8000000000000000000000000000000000000000' ) ,
110
118
) . toBe ( false )
@@ -116,7 +124,7 @@ describe('account', function () {
116
124
) . toBe ( false )
117
125
} )
118
126
119
- it ( 'isBuiltInAddress' , function ( ) {
127
+ it ( 'isBuiltInAddress' , ( ) => {
120
128
expect (
121
129
isBuiltInAddress ( '0x0888000000000000000000000000000000000000' ) ,
122
130
) . toBe ( true )
@@ -134,7 +142,7 @@ describe('account', function () {
134
142
) . toBe ( false )
135
143
} )
136
144
137
- it ( 'isCfxHexAddress' , function ( ) {
145
+ it ( 'isCfxHexAddress' , ( ) => {
138
146
expect (
139
147
isCfxHexAddress ( '0x0888000000000000000000000000000000000000' ) ,
140
148
) . toBe ( true )
@@ -155,7 +163,7 @@ describe('account', function () {
155
163
) . toBe ( false )
156
164
} )
157
165
158
- it ( 'validateHexAddress' , function ( ) {
166
+ it ( 'validateHexAddress' , ( ) => {
159
167
expect (
160
168
validateHexAddress ( '0x0888000000000000000000000000000000000000' ) ,
161
169
) . toBe ( true )
@@ -214,4 +222,39 @@ describe('account', function () {
214
222
) . toThrow ( 'Invalid address, must be a 0x-prefixed string' )
215
223
} )
216
224
} )
225
+
226
+ describe ( 'help methods' , ( ) => {
227
+ test ( 'isChecksummed' , ( ) => {
228
+ expect ( isChecksummed ( checksumAddress ) ) . toBe ( true )
229
+ expect ( isChecksummed ( '0x00000000000000000000000000000000000000011' ) ) . toBe (
230
+ false ,
231
+ )
232
+ } )
233
+ test ( 'randomAddressType' , ( ) => {
234
+ expect ( randomAddressType ( ) ) . toBeDefined ( )
235
+ } )
236
+
237
+ test ( 'randomCfxHexAddress' , ( ) => {
238
+ const address = randomCfxHexAddress ( )
239
+ expect (
240
+ isBuiltInAddress ( address ) ||
241
+ isUserHexAddress ( address ) ||
242
+ isContractAddress ( address ) ||
243
+ isNullHexAddress ( address ) ,
244
+ ) . toBe ( true )
245
+ } )
246
+
247
+ test ( 'randomPrivateKey' , ( ) => {
248
+ const pk = randomPrivateKey ( )
249
+
250
+ expect ( create ( { pk} ) . privateKey ) . toEqual ( pk )
251
+ } )
252
+
253
+ test ( 'validatePrivateKey' , ( ) => {
254
+ const pk = randomPrivateKey ( )
255
+
256
+ expect ( validatePrivateKey ( pk ) ) . toBe ( true )
257
+ expect ( validatePrivateKey ( '0x' ) ) . toBe ( false )
258
+ } )
259
+ } )
217
260
} )
0 commit comments