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 d3b457d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 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
23 changes: 19 additions & 4 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 Down Expand Up @@ -39,10 +41,23 @@ describe('PrivateKey and PublicKey tests', () => {
})

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

0 comments on commit d3b457d

Please sign in to comment.