Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate wallet tests to `tape #2742

Merged
merged 22 commits into from
Jun 5, 2023
Merged

Migrate wallet tests to `tape #2742

merged 22 commits into from
Jun 5, 2023

Conversation

acolytec3
Copy link
Contributor

Replaced mocha with tape as test runner for wallet tests and decomposes for better readability

@codecov
Copy link

codecov bot commented May 31, 2023

Codecov Report

Merging #2742 (1744d6b) into master (6553978) will decrease coverage by 0.06%.
The diff coverage is n/a.

Additional details and impacted files

Impacted file tree graph

Flag Coverage Δ
block 90.82% <ø> (ø)
blockchain 90.72% <ø> (ø)
client 86.80% <ø> (-0.04%) ⬇️
common 97.00% <ø> (ø)
devp2p 89.40% <ø> (-0.08%) ⬇️
ethash ∅ <ø> (∅)
evm 79.98% <ø> (ø)
rlp ∅ <ø> (?)
statemanager 86.28% <ø> (ø)
trie 89.59% <ø> (-0.62%) ⬇️
tx 95.76% <ø> (ø)
util 81.13% <ø> (ø)
vm 81.07% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

@acolytec3 acolytec3 marked this pull request as ready for review June 1, 2023 15:25
Comment on lines 35 to 57
tape('.fromExtendedKey()', (t) => {
const hdnode1 = EthereumHDKey.fromExtendedKey(
'xpub661MyMwAqRbcGout4B6s29b6gGQsowyoiF6UgXBEr7eFCWYfXuZDvRxP9zEh1Kwq3TLqDQMbkbaRpSnoC28oWvjLeshoQz1StZ9YHM1EpcJ'
)
t.deepEqual(
hdnode1.publicExtendedKey(),
'xpub661MyMwAqRbcGout4B6s29b6gGQsowyoiF6UgXBEr7eFCWYfXuZDvRxP9zEh1Kwq3TLqDQMbkbaRpSnoC28oWvjLeshoQz1StZ9YHM1EpcJ'
)
t.throws(function () {
hdnode1.privateExtendedKey()
}, /^Error: No private key$/)
const hdnode2 = EthereumHDKey.fromExtendedKey(
'xprv9s21ZrQH143K4KqQx9Zrf1eN8EaPQVFxM2Ast8mdHn7GKiDWzNEyNdduJhWXToy8MpkGcKjxeFWd8oBSvsz4PCYamxR7TX49pSpp3bmHVAY'
)
t.deepEqual(
hdnode2.publicExtendedKey(),
'xpub661MyMwAqRbcGout4B6s29b6gGQsowyoiF6UgXBEr7eFCWYfXuZDvRxP9zEh1Kwq3TLqDQMbkbaRpSnoC28oWvjLeshoQz1StZ9YHM1EpcJ'
)
t.deepEqual(
hdnode2.privateExtendedKey(),
'xprv9s21ZrQH143K4KqQx9Zrf1eN8EaPQVFxM2Ast8mdHn7GKiDWzNEyNdduJhWXToy8MpkGcKjxeFWd8oBSvsz4PCYamxR7TX49pSpp3bmHVAY'
)
t.end()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we perhaps add some comments that makes these two cases a bit clearer (i.e. hdnode1 testing fromExtendedKey with a public key, and hdnode2 is testing it with a private key) ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Could just be added as a third arg to deepEqual)

Comment on lines 127 to 132

// t.test('.toV3() should fail', function () {
// assert.throws(function () {
// Wallet.fromPublicKey(pubKey).toV3()
// }, /^Error: This is a public key only wallet$/)
// })
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this test commented out? can we just remove it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure but I added it back in and it passes.

'should generate an account compatible with ICAP Direct'
)
const addr = bytesToHex(wallet.getAddress())
t.equal(BigInt('0x' + addr) <= max, true)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t.equal(BigInt('0x' + addr) <= max, true)
t.equal(BigInt('0x' + addr) <= max, true, 'should generate an account compatible with ICAP Direct')

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could also remove it from the previous test, personally I think it makes more sense as the last message rather than the first (and that's what you seem to be doing in other tests)

return permus
}
const strKdfOptions = { iv, salt, uuid }
const buffKdfOptions = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super nit, but could be renamed to bytesKdfOptions

Comment on lines 273 to 280
for (const perm of permutations) {
const encFixtureWallet = await fixtureWallet.toV3String(pw, {
kdf: 'pbkdf2',
c: n,
uuid: perm.uuid,
salt: perm.salt,
iv: perm.iv,
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it might be more efficient to keep the Promise.all rather than looping through promises, since with Promise.all all promises will be started simultaneously, while here they will be run sequentially.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, ok, let me look at it with it changed back. This whole section of the tests was almost incomprehensible to me until I unwound everything.

} catch (err: any) {
t.ok(
err.message.includes(
'Invalid salt, must be a string (empty or a non-zero even number of hex characters) or buffer'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
'Invalid salt, must be a string (empty or a non-zero even number of hex characters) or buffer'
'Invalid salt, must be a string (empty or a non-zero even number of hex characters) or uint8array'

Comment on lines 629 to 680
describe('.fromV1()', function () {
it('should work', function () {
tape('.fromV1()', (t) => {
t.test('should work', function () {
const sample = '{"Address":"d4584b5f6229b7be90727b0fc8c6b91bb427821f","Crypto":{"CipherText":"07533e172414bfa50e99dba4a0ce603f654ebfa1ff46277c3e0c577fdc87f6bb4e4fe16c5a94ce6ce14cfa069821ef9b","IV":"16d67ba0ce5a339ff2f07951253e6ba8","KeyHeader":{"Kdf":"scrypt","KdfParams":{"DkLen":32,"N":262144,"P":1,"R":8,"SaltLen":32},"Version":"1"},"MAC":"8ccded24da2e99a11d48cda146f9cc8213eb423e2ea0d8427f41c3be414424dd","Salt":"06870e5e6a24e183a5c807bd1c43afd86d573f7db303ff4853d135cd0fd3fe91"},"Id":"0498f19a-59db-4d54-ac95-33901b4f1870","Version":"1"}'
const wallet = Wallet.fromV1(sample, 'foo')
assert.strictEqual(wallet.getAddressString(), '0xd4584b5f6229b7be90727b0fc8c6b91bb427821f')
t.deepEqual(wallet.getAddressString(), '0xd4584b5f6229b7be90727b0fc8c6b91bb427821f')
})
})
*/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still need to keep this commented out test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure. Let me look at the comments from @paulmillr when the big upgrade was done a few months back. It fails if I uncomment it so not sure if it's necessary or not.

Copy link
Contributor Author

@acolytec3 acolytec3 Jun 2, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked at the git history for this test and it's been commented out for at least 4 years, so I think we're safe to remove it.

Copy link
Contributor

@g11tech g11tech left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm!

@g11tech g11tech merged commit af33768 into master Jun 5, 2023
@holgerd77 holgerd77 deleted the wallet-tests-to-tape branch June 6, 2023 08:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants