Skip to content

Commit 904af5c

Browse files
committed
chore: add update @fluent-wallet/account test
1 parent 06cff4b commit 904af5c

File tree

7 files changed

+920
-264
lines changed

7 files changed

+920
-264
lines changed

package.json

+3
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
"test:integration": "cross-env NODE_OPTIONS=--experimental-vm-modules NODE_ENV=test node ./scripts/integration-test.js --no-cache --config='./integration.jest.config.js'",
5959
"contract:compile": "bash ./scripts/solc.sh",
6060
"contract:watch": "watchexec --restart --on-busy-update queue -w ./contracts/ --debounce 500 yarn run contract:compile",
61+
"test:vitest:unit": "vitest",
6162
"dev:chrome": "cross-env NODE_ENV=development cross-env TARGET_BROWSER=chrome webpack --config ./scripts/webpack.config.dev.mjs",
6263
"dev:firefox": "cross-env NODE_ENV=development cross-env TARGET_BROWSER=firefox webpack --config ./scripts/webpack.config.dev.mjs",
6364
"dev:edge": "cross-env NODE_ENV=development cross-env TARGET_BROWSER=edge webpack --config ./scripts/webpack.config.dev.mjs",
@@ -90,6 +91,7 @@
9091
"@types/js-string-escape": "^1",
9192
"@types/serve-static": "^1",
9293
"@types/webpack-bundle-analyzer": "^4",
94+
"@vitest/coverage-v8": "2.0.5",
9395
"@yarnpkg/esbuild-plugin-pnp": "3.0.0-rc.2",
9496
"@yqrashawn/snowpack": "3.8.6-fix-6",
9597
"autoprefixer": "10.4.20",
@@ -152,6 +154,7 @@
152154
"stylelint-config-prettier": "^9.0.3",
153155
"stylelint-config-standard": "^26.0.0",
154156
"tailwindcss": "^3.1.1",
157+
"vitest": "2.0.5",
155158
"wait-for-expect": "3.0.2",
156159
"web-ext": "7.0.0",
157160
"webpack": "5.93.0",

packages/account/index.js

+35-7
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,48 @@ import {getAddress as toChecksumAddress} from '@ethersproject/address'
22
import {computeAddress} from '@ethersproject/transactions'
33
import {Wallet} from '@ethersproject/wallet'
44
import {randomInt, addHexPrefix} from '@fluent-wallet/utils'
5-
import {compL, threadFirst} from '@fluent-wallet/compose'
65
import {
76
NULL_HEX_ADDRESS,
87
INTERNAL_CONTRACTS_HEX_ADDRESS,
98
ADDRESS_TYPES,
109
} from '@fluent-wallet/consts'
1110

11+
/**
12+
* @param {Object} options - An object that can contain a privateKey property.
13+
* @param {string} options.pk - A hexadecimal string representing a private key.
14+
* @return {Wallet} A new Wallet instance.
15+
*/
1216
export const create = ({pk} = {}) => {
1317
const kp = pk ? new Wallet(pk) : Wallet.createRandom()
1418
return kp
1519
}
20+
/**
21+
*
22+
* @param {string} address hex address
23+
* @returns {string} checksum address
24+
*/
25+
export const toChecksum = address => toChecksumAddress(addHexPrefix(address)) // compL(addHexPrefix, toChecksumAddress)
1626

17-
export const toChecksum = compL(addHexPrefix, toChecksumAddress)
18-
export const isChecksummed = compL(addHexPrefix, addr => {
27+
/**
28+
*
29+
* @param {string} addr
30+
* @returns {boolean}
31+
*/
32+
export const isChecksummed = addr => {
1933
try {
20-
return Boolean(toChecksumAddress(addr))
34+
return Boolean(toChecksumAddress(addHexPrefix(addr)))
2135
} catch (err) {
2236
return false
2337
}
24-
})
25-
38+
}
39+
/**
40+
* @param {string} pk - private key
41+
* @returns {Object}
42+
* @property {string} address
43+
* @property {string} privateKey
44+
*/
2645
export const fromPrivate = pk => ({
27-
address: threadFirst(pk, addHexPrefix, computeAddress),
46+
address: computeAddress(addHexPrefix(pk)), //threadFirst(pk, addHexPrefix, computeAddress),
2847
privateKey: addHexPrefix(pk),
2948
})
3049

@@ -36,6 +55,15 @@ export const toContractAddress = address => {
3655
return address.replace(/^0x./, '0x8')
3756
}
3857

58+
/**
59+
* Generate a random hex address of the specified type.
60+
*
61+
* @param {string} [type] - The type of the address. Can be 'builtin', 'null', 'user', or 'contract'.
62+
* If not specified, a random type will be chosen.
63+
* @param {boolean} [checksum=false] - Whether to return a checksummed address.
64+
* @throws {Error} If the specified type is invalid.
65+
* @returns {string} A random hex address.
66+
*/
3967
export const randomHexAddress = (type, checksum = false) => {
4068
if (type && !ADDRESS_TYPES.includes(type))
4169
throw new Error(`Invalid address type ${type}`)

packages/account/index.test.js

+64-21
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// 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'
33
import {
44
fromPrivate,
55
toChecksum,
@@ -12,6 +12,11 @@ import {
1212
isBuiltInAddress,
1313
isCfxHexAddress,
1414
validateHexAddress,
15+
isChecksummed,
16+
randomAddressType,
17+
randomCfxHexAddress,
18+
randomPrivateKey,
19+
validatePrivateKey,
1520
} from './'
1621
import {
1722
NULL_HEX_ADDRESS,
@@ -30,24 +35,24 @@ const checksumAddress = '0xED54a7C1d8634BB589f24Bb7F05a5554b36F9618'
3035
// const s = '0x129ff05af364204442bdb53ab6f18a99ab48acc9326fa689f228040429e3ca66'
3136
// const v = '0x1b' // 27
3237

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 () => {
3641
expect(fromPrivate(ecprivkey)).toEqual({
3742
address: checksumAddress,
3843
privateKey: ecprivkey,
3944
})
4045
})
4146
})
4247

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 () => {
4550
expect(toChecksum(address)).toEqual(checksumAddress)
4651
})
4752
})
4853

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 () => {
5156
const account = create()
5257
expect(account.address).toEqual(
5358
expect.stringMatching(/^0x[0-9a-fA-F]{40}$/),
@@ -56,28 +61,31 @@ describe('account', function () {
5661
})
5762
})
5863

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 () => {
6166
expect(
6267
INTERNAL_CONTRACTS_HEX_ADDRESS.includes(randomHexAddress('builtin')),
6368
).toBe(true)
6469
})
6570

66-
it('should generate the null address', async function () {
71+
it('should generate the null address', async () => {
6772
expect(randomHexAddress('null')).toEqual(NULL_HEX_ADDRESS)
6873
})
6974

70-
it('should generate a contract address', async function () {
75+
it('should generate a contract address', async () => {
7176
expect(randomHexAddress('contract').startsWith('0x8')).toBe(true)
7277
})
7378

74-
it('should generate a account address', async function () {
79+
it('should generate a account address', async () => {
7580
expect(randomHexAddress('user').startsWith('0x1')).toBe(true)
7681
})
82+
it('no args', () => {
83+
expect(randomHexAddress()).toBeDefined()
84+
})
7785
})
7886

79-
describe('checks', function () {
80-
it('isHexAddress', function () {
87+
describe('checks', () => {
88+
it('isHexAddress', () => {
8189
expect(isHexAddress('0x0000000000000000000000000000000000000000')).toBe(
8290
true,
8391
)
@@ -86,13 +94,13 @@ describe('account', function () {
8694
)
8795
})
8896

89-
it('isNullHexAddress', function () {
97+
it('isNullHexAddress', () => {
9098
expect(
9199
isNullHexAddress('0x0000000000000000000000000000000000000000'),
92100
).toBe(true)
93101
})
94102

95-
it('isContractAddress', function () {
103+
it('isContractAddress', () => {
96104
expect(
97105
isContractAddress('0x8000000000000000000000000000000000000000'),
98106
).toBe(true)
@@ -104,7 +112,7 @@ describe('account', function () {
104112
).toBe(false)
105113
})
106114

107-
it('isUserHexAddress', function () {
115+
it('isUserHexAddress', () => {
108116
expect(
109117
isUserHexAddress('0x8000000000000000000000000000000000000000'),
110118
).toBe(false)
@@ -116,7 +124,7 @@ describe('account', function () {
116124
).toBe(false)
117125
})
118126

119-
it('isBuiltInAddress', function () {
127+
it('isBuiltInAddress', () => {
120128
expect(
121129
isBuiltInAddress('0x0888000000000000000000000000000000000000'),
122130
).toBe(true)
@@ -134,7 +142,7 @@ describe('account', function () {
134142
).toBe(false)
135143
})
136144

137-
it('isCfxHexAddress', function () {
145+
it('isCfxHexAddress', () => {
138146
expect(
139147
isCfxHexAddress('0x0888000000000000000000000000000000000000'),
140148
).toBe(true)
@@ -155,7 +163,7 @@ describe('account', function () {
155163
).toBe(false)
156164
})
157165

158-
it('validateHexAddress', function () {
166+
it('validateHexAddress', () => {
159167
expect(
160168
validateHexAddress('0x0888000000000000000000000000000000000000'),
161169
).toBe(true)
@@ -214,4 +222,39 @@ describe('account', function () {
214222
).toThrow('Invalid address, must be a 0x-prefixed string')
215223
})
216224
})
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+
})
217260
})

packages/account/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"@ethersproject/address": "5.6.1",
88
"@ethersproject/transactions": "5.6.2",
99
"@ethersproject/wallet": "5.6.2",
10-
"@fluent-wallet/compose": "workspace:packages/compose",
1110
"@fluent-wallet/consts": "workspace:packages/consts",
1211
"@fluent-wallet/utils": "workspace:packages/utils",
1312
"elliptic": "6.5.4"

vite.config.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import {defineConfig} from 'vitest/config'
2+
export default defineConfig({
3+
test: {},
4+
})

vitest.workspace.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import {defineWorkspace} from 'vitest/config'
2+
3+
export default defineWorkspace(['packages/*'])

0 commit comments

Comments
 (0)