Skip to content

Commit

Permalink
Improve key address generation test
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben-Rey committed Apr 24, 2024
1 parent b86fa5c commit a6d7741
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
16 changes: 6 additions & 10 deletions packages/massa-web3/test/experimental/unit/address.spec.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Account } from '../../../src/experimental/account'
import { Address } from '../../../src/experimental/basicElements'
import { Version } from '../../../src/experimental/crypto/interfaces/versioner'

import { Address } from '../../../src/experimental/basicElements'
import { Address as LegacyAddress } from '../../../src/utils/keyAndAddresses'

const unsupported_version = -1 as Version

const contractAddress = 'AS1eK3SEXGDAWN6pZhdr4Q7WJv6UHss55EB14hPy4XqBpiktfPu6'
class TestAddress extends Address {
public static callInitFromVersion(version: Version = Version.V0): Address {
return this.initFromVersion(version)
}
}

describe('Address tests', () => {
let account: Account

Expand Down Expand Up @@ -39,19 +39,17 @@ describe('Address tests', () => {

test('fromString throws error for invalid address string', () => {
expect(() => Address.fromString('invalid_address_string')).toThrow(
'invalid address string:'
/invalid address string:/
)
})

test('toString returns string with user prefix for EOA', () => {
const address = Address.fromPublicKey(account.publicKey)
address.isEOA = true
expect(address.toString()).toMatch(/^AU/)
})

test('toString returns string with contract prefix for contract account', () => {
const address = Address.fromPublicKey(account.publicKey)
address.isEOA = false
const address = Address.fromString(contractAddress)
expect(address.toString()).toMatch(/^AS/)
})

Expand All @@ -64,9 +62,7 @@ describe('Address tests', () => {
})

test('toBytes returns bytes with contract prefix for contract account', () => {
const address = Address.fromPublicKey(account.publicKey)
address.isEOA = false

const address = Address.fromString(contractAddress)
const bytes = address.toBytes()
// The first byte should be 1 for contract account
expect(bytes[0]).toBe(1)
Expand Down
38 changes: 27 additions & 11 deletions packages/massa-web3/test/experimental/unit/key.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
PublicKey,
} from '../../../src/experimental/basicElements/keys'
import { Version } from '../../../src/experimental/crypto/interfaces/versioner'
import { SecretKey as LegacyPrivateKey } from '../../../src/utils/keyAndAddresses'
import { Address as LegacyAddress } from '../../../src/utils/keyAndAddresses'

class TestPublicKey extends PublicKey {
public static callInitFromVersion(version: Version) {
Expand All @@ -27,22 +29,37 @@ describe('PrivateKey and PublicKey tests', () => {
})

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)

test('PublicKey toBytes and fromBytes', async () => {
const publicKey = await PublicKey.fromPrivateKey(privateKey)
const newPubKeyBytes = PublicKey.fromBytes(publicKey.bytes)
expect(newPubKeyBytes.bytes).toEqual(publicKey.bytes)
})

test('PrivateKey toBytes and fromBytes', async () => {
const privateKey = PrivateKey.generate()
const newPKeyBytes = PrivateKey.fromBytes(privateKey.bytes)
expect(newPKeyBytes.bytes).toEqual(privateKey.bytes)
})
})

describe('Address Generation', () => {
test('PublicKey getAddress', () => {
// TODO be sure address is right
const address = publicKey.getAddress()
expect(address).toBeInstanceOf(Address)
test('PublicKey getAddress', async () => {
// Generate address from Legacy keys objects
const legacyPrivKey = new LegacyPrivateKey(
'S1eK3SEXGDAWN6pZhdr4Q7WJv6UHss55EB14hPy4XqBpiktfPu6'
)
const legacyPubKey = await legacyPrivKey.getPublicKey()
const legacyAddress = LegacyAddress.fromPublicKey(legacyPubKey)

// Generate address from new keys objects
const privKey = PrivateKey.fromString(
'S1eK3SEXGDAWN6pZhdr4Q7WJv6UHss55EB14hPy4XqBpiktfPu6'
)
const pubKey = await PublicKey.fromPrivateKey(privKey)
const address = Address.fromPublicKey(pubKey)

// Verify that the addresses are the same
expect(address.toString()).toBe(legacyAddress.base58Encoded)
})
})

Expand Down Expand Up @@ -78,7 +95,7 @@ describe('PrivateKey and PublicKey tests', () => {
)
})

it('should throw an error when given an invalid string', () => {
it('fromString throws error for invalid public key string', () => {
const invalidPublicKeyString = 'invalidPublicKey'

expect(() =>
Expand All @@ -91,7 +108,6 @@ describe('PrivateKey and PublicKey tests', () => {
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())
})

Expand Down
10 changes: 6 additions & 4 deletions packages/massa-web3/test/experimental/unit/storage.spec.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { StorageCost } from '../../../src/experimental/basicElements/storage'
import { fromMAS } from '@massalabs/web3-utils'
import {
BASE_ACCOUNT_CREATION_COST,
STORAGE_BYTE_COST,
} from '@massalabs/web3-utils'

describe('StorageCost', () => {
describe('bytes', () => {
it('should calculate the cost of a given number of bytes', () => {
const numberOfBytes = 10
const expectedCost = BigInt(numberOfBytes) * fromMAS(0.0001)
const expectedCost = BigInt(numberOfBytes) * STORAGE_BYTE_COST
expect(StorageCost.bytes(numberOfBytes)).toEqual(expectedCost)
})
})

describe('account', () => {
it('should calculate the cost of creating a new account', () => {
const expectedCost = fromMAS(0.001)
expect(StorageCost.account()).toEqual(expectedCost)
expect(StorageCost.account()).toEqual(BASE_ACCOUNT_CREATION_COST)
})
})

Expand Down

0 comments on commit a6d7741

Please sign in to comment.