Skip to content

Commit

Permalink
test key file
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed Apr 24, 2024
1 parent 2259d57 commit b86fa5c
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/massa-web3/src/experimental/basicElements/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class PrivateKey {
privateKey.version
)
} catch (e) {
throw new Error(`invalid private key string: ${e.message}`)
throw new Error(`Invalid private key string: ${e.message}`)
}
return privateKey
}
Expand Down
8 changes: 6 additions & 2 deletions packages/massa-web3/test/experimental/unit/internal.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,12 @@ describe('Internal functions tests', () => {
})

test('checkPrefix throws error for invalid prefix', () => {
expect(() => checkPrefix('invalid_prefix', 'valid_prefix')).toThrow(
'invalid prefix: invalid_prefix. valid_prefix was expected.'
expect(() => checkPrefix('IP_invalid_prefix', 'AS')).toThrow(
'invalid prefix: IP. AS was expected.'
)

expect(() => checkPrefix('IP_invalid_prefix', 'AS', 'AU')).toThrow(
'invalid prefix: IP. one of AS or AU was expected.'
)
})
})
129 changes: 129 additions & 0 deletions packages/massa-web3/test/experimental/unit/key.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { Address } from '../../../src/experimental/basicElements/address'
import {
PrivateKey,
PublicKey,
} from '../../../src/experimental/basicElements/keys'
import { Version } from '../../../src/experimental/crypto/interfaces/versioner'

class TestPublicKey extends PublicKey {
public static callInitFromVersion(version: Version) {
return this.initFromVersion(version)
}
}

class TestPrivateKey extends PrivateKey {
public static callInitFromVersion(version: Version) {
return this.initFromVersion(version)
}
}

describe('PrivateKey and PublicKey tests', () => {
let privateKey: PrivateKey
let publicKey: PublicKey

beforeAll(async () => {
privateKey = PrivateKey.generate()
publicKey = await PublicKey.fromPrivateKey(privateKey)
})

describe('Conversion to and from Bytes', () => {
test('PrivateKey and PublicKey toBytes and fromBytes', async () => {
const privateKey = PrivateKey.generate()
const newPKeyBytes = PrivateKey.fromBytes(privateKey.bytes)
expect(newPKeyBytes.bytes).toEqual(privateKey.bytes)

const publicKey = await PublicKey.fromPrivateKey(privateKey)
const newPubKeyBytes = PublicKey.fromBytes(publicKey.bytes)
expect(newPubKeyBytes.bytes).toEqual(publicKey.bytes)
})
})

describe('Address Generation', () => {
test('PublicKey getAddress', () => {
// TODO be sure address is right
const address = publicKey.getAddress()
expect(address).toBeInstanceOf(Address)
})
})

describe('Signing and Verification', () => {
test('PrivateKey sign and PublicKey verify', async () => {
const message = new TextEncoder().encode('Hello, world!')
const signature = await privateKey.sign(message)
const isValid = await publicKey.verify(message, signature)

expect(isValid).toBe(true)
})
})

describe('From String', () => {
test('fromString returns PrivateKey when valid private key string', () => {
const privateKeyString = privateKey.toString()
const key = PrivateKey.fromString(privateKeyString)
expect(key).toBeInstanceOf(PrivateKey)
expect(key.toString()).toBe(privateKey.toString())
})

test('fromString returns PublicKey when valid public key string', () => {
const publicKeyString = publicKey.toString()
const key = PublicKey.fromString(publicKeyString)
expect(key).toBeInstanceOf(PublicKey)
expect(key.toString()).toBe(publicKey.toString())
})

test('fromString throws error for invalid private key string', () => {
const invalidPrivateKeyString = 'invalidPrivateKey'
expect(() => PrivateKey.fromString(invalidPrivateKeyString)).toThrow(
/Invalid private key string/
)
})

it('should throw an error when given an invalid string', () => {
const invalidPublicKeyString = 'invalidPublicKey'

expect(() =>
PublicKey.fromString(invalidPublicKeyString, Version.V0)
).toThrow(/Invalid public key string/)
})
})

describe('From Environment Variables', () => {
test('fromEnv returns PrivateKey when PRIVATE_KEY environment variable set', () => {
process.env.PRIVATE_KEY = privateKey.toString()
const key = PrivateKey.fromEnv()
expect(key).toBeInstanceOf(PrivateKey)
expect(key.toString()).toBe(privateKey.toString())
})

test('fromEnv throws error when PRIVATE_KEY environment variable is not set', () => {
delete process.env.PRIVATE_KEY
expect(() => PrivateKey.fromEnv()).toThrow(
'missing `PRIVATE_KEY` environment variable'
)
})
})

describe('From Version', () => {
test('initFromVersion returns correct PublicKey for supported version', () => {
const publicKey = TestPublicKey.callInitFromVersion(Version.V0)
expect(publicKey).toBeInstanceOf(PublicKey)
expect(publicKey.version).toEqual(Version.V0)
})

test('initFromVersion returns correct Private for supported version', () => {
const privateKey = TestPrivateKey.callInitFromVersion(Version.V0)
expect(privateKey).toBeInstanceOf(PrivateKey)
expect(privateKey.version).toEqual(Version.V0)
})

test('initFromVersion throws error for unsupported version', () => {
expect(() => TestPublicKey.callInitFromVersion(Version.V1)).toThrow(
'unsupported version: 1'
)

expect(() => TestPrivateKey.callInitFromVersion(Version.V1)).toThrow(
'unsupported version: 1'
)
})
})
})

1 comment on commit b86fa5c

@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 41.9% 595/1420
🔴 Branches 18.33% 68/371
🔴 Functions 32.28% 92/285
🔴 Lines 41.67% 583/1399

Test suite run success

36 tests passing in 7 suites.

Report generated by 🧪jest coverage report action from b86fa5c

Please sign in to comment.