Skip to content

Commit

Permalink
add account and sealer unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ASAPSegfault committed Apr 29, 2024
1 parent 77a5e25 commit d23d6a4
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 0 deletions.
76 changes: 76 additions & 0 deletions packages/massa-web3/test/experimental/unit/account.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,56 @@ describe('Basic use cases', () => {
expect(account.address.isEOA).toBe(true)
})

test('Account - to and from keystore (V0)', async () => {
const accountv0 = await Account.fromPrivateKey(
'S12jWf59Yzf2LimL89soMnAP2VEBDBpfCbZLoEFo36CxEL3j92rZ',
Version.V0
)

const ks = await accountv0.toKeyStore(
'unsecurePassword',
new Uint8Array([
146, 63, 151, 136, 93, 135, 105, 113, 124, 41, 189, 207, 86, 124, 17,
152,
]),
new Uint8Array([250, 104, 81, 250, 235, 79, 110, 84, 243, 225, 144, 242])
)
expect(ks.Version).toBe(Version.V0)
expect(ks.Address).toBe(
'AU126tkwrhXn9gEG5JPtrNy8NNLbVMwywokgLKshSYyzP8qusqXZL'
)
expect(ks.Nonce).toStrictEqual(
new Uint8Array([250, 104, 81, 250, 235, 79, 110, 84, 243, 225, 144, 242])
)
expect(ks.Salt).toStrictEqual(
new Uint8Array([
146, 63, 151, 136, 93, 135, 105, 113, 124, 41, 189, 207, 86, 124, 17,
152,
])
)
expect(ks.PublicKey).toStrictEqual([
0, 143, 111, 199, 160, 227, 187, 57, 238, 223, 80, 251, 169, 64, 21, 116,
165, 95, 187, 192, 76, 97, 33, 64, 208, 119, 99, 182, 139, 174, 219, 61,
109,
])
expect(ks.CipheredData).toStrictEqual([
191, 181, 5, 198, 76, 145, 242, 89, 253, 215, 151, 10, 245, 32, 241, 9,
171, 181, 76, 103, 121, 184, 33, 16, 227, 83, 53, 133, 194, 38, 20, 217,
34, 61, 169, 108, 156, 217, 196, 74, 34, 127, 129, 33, 103, 215, 117, 66,
78,
])

const account = await Account.fromKeyStore(ks, 'unsecurePassword')

expect(account).toBeDefined()
expect(account.privateKey).toBeDefined()
expect(account.publicKey).toBeDefined()
expect(account.address).toBeDefined()
expect(account.version).toBe(Version.V0)
expect(account.address.version).toBe(Version.V0)
expect(account.address.isEOA).toBe(true)
})

test('Account - from yaml file', async () => {
const walletPath = path.join(__dirname, 'wallet_test_version_1.yaml')

Expand All @@ -91,4 +141,30 @@ describe('Basic use cases', () => {
expect(account.address.version).toBe(Version.V0)
expect(account.address.isEOA).toBe(true)
})

test('Account - from environment variables', async () => {
process.env.PRIVATE_KEY =
'S12jWf59Yzf2LimL89soMnAP2VEBDBpfCbZLoEFo36CxEL3j92rZ'

const account = await Account.fromEnv()

expect(account).toBeDefined()
expect(account.address.toString()).toBe(
'AU126tkwrhXn9gEG5JPtrNy8NNLbVMwywokgLKshSYyzP8qusqXZL'
)
expect(account.publicKey.toString()).toBe(
'P126AtzfcSJwdi6xsAmXbzXhhwVhS9d1hRjFNT4PfrDogt3nAihj'
)
expect(account.version).toBe(Version.V1)
expect(account.address.isEOA).toBe(true)
})

test('Account - sign message and verify signature', async () => {
const account = await Account.fromPrivateKey(
'S12jWf59Yzf2LimL89soMnAP2VEBDBpfCbZLoEFo36CxEL3j92rZ'
)
const messsage = new Uint8Array([1, 2, 3])
const signature = await account.sign(messsage)
expect(account.verify(messsage, signature)).toBeTruthy()
})
})
19 changes: 19 additions & 0 deletions packages/massa-web3/test/experimental/unit/passwordSeal.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { PasswordSeal } from '../../../src/experimental/crypto/passwordSeal'
import randomUint8Array from 'secure-random'

describe('Password Seal tests', () => {
test('creates Sealer from env variable', async () => {
process.env.PASSWORD = 'unsecurePassword'
process.env.SALT = Buffer.from(randomUint8Array(16)).toString('base64')
process.env.NONCE = Buffer.from(randomUint8Array(12)).toString('base64')

const sealer = PasswordSeal.fromEnv()
expect(sealer).toBeDefined()

const originalData = Uint8Array.from([1, 2, 3, 4, 5])
const encryptedData = await sealer.seal(originalData)
const decryptedData = await sealer.unseal(encryptedData)

expect(originalData).toEqual(Uint8Array.from(decryptedData))
})
})

1 comment on commit d23d6a4

@github-actions
Copy link

Choose a reason for hiding this comment

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

Coverage report for experimental massa-web3

St.
Category Percentage Covered / Total
🔴 Statements 47.96% 704/1468
🔴 Branches 22.8% 88/386
🔴 Functions 40.27% 120/298
🔴 Lines 47.79% 691/1446

Test suite run success

51 tests passing in 8 suites.

Report generated by 🧪jest coverage report action from d23d6a4

Please sign in to comment.