Skip to content

Commit

Permalink
update pr
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Apr 26, 2024
1 parent 7c889ce commit 32dd742
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 31 deletions.
48 changes: 29 additions & 19 deletions packages/massa-web3/src/experimental/basicElements/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ const UNDERLYING_HASH_LEN = 32

const DEFAULT_VERSION = Version.V0

export enum AddressType {
EOA = 0,
Contract = 1,
}

/**
* A class representing an address.
*
Expand Down Expand Up @@ -57,7 +62,7 @@ export class Address {
}
}

private getPrefix(str: string): string {
private static getPrefix(str: string): string {
const expected = [ADDRESS_USER_PREFIX, ADDRESS_CONTRACT_PREFIX]
for (let prefix of expected) {
if (str.startsWith(prefix)) {
Expand All @@ -79,30 +84,31 @@ export class Address {
* @throws If the address string is invalid.
*/
public static fromString(str: string): Address {
const version = Address.getVersion(str)
const address = Address.initFromVersion(version)

try {
const prefix = address.getPrefix(str)
const version = Address.getVersion(str)
const address = Address.initFromVersion(version)
const prefix = Address.getPrefix(str)

address.isEOA = prefix === ADDRESS_USER_PREFIX
const versionedBytes = address.serializer.deserialize(
str.slice(prefix.length)
)

address.bytes = Uint8Array.from([
...varint.encode(address.isEOA ? 0 : 1),
...varint.encode(
address.isEOA ? AddressType.EOA : AddressType.Contract
),
...versionedBytes,
])
return address
} catch (e) {
throw new Error(`invalid address string: ${e.message}`)
}
return address
}

/**
* Get the address type from bytes.
* *
*
* @returns the address type enum.
*/
private getType(): number {
Expand All @@ -114,7 +120,7 @@ export class Address {

/**
* Get the address version.
* *
*
* @returns the address type enum.
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
Expand All @@ -131,15 +137,15 @@ export class Address {
*
* @returns A new address object.
*/
public static fromPublicKey(
publicKey: PublicKey,
version = DEFAULT_VERSION
): Address {
const address = Address.initFromVersion(version)
public static fromPublicKey(publicKey: PublicKey): Address {
if (publicKey.version !== Version.V0) {
throw new Error(`unsupported public key version: ${publicKey.version}`)
}
const address = Address.initFromVersion(Version.V0)
const rawBytes = publicKey.hasher.hash(publicKey.toBytes())
address.bytes = Uint8Array.from([
0 /* EOA*/,
...address.versioner.attach(version, rawBytes),
AddressType.EOA,
...address.versioner.attach(Version.V0, rawBytes),
])
address.isEOA = true
return address
Expand All @@ -156,7 +162,7 @@ export class Address {
const version = Address.getVersion(bytes)
const address = Address.initFromVersion(version)
address.bytes = bytes
address.isEOA = address.getType() === 0
address.isEOA = address.getType() === AddressType.EOA
return address
}

Expand Down Expand Up @@ -194,7 +200,10 @@ export class Address {
*
* @returns The address in bytes format.
*/
static extractFromBuffer(data: Uint8Array, offset = 0): Uint8Array {
static extractFromBuffer(
data: Uint8Array,
offset = 0
): { data: Uint8Array; length: number } {
// addr type
varint.decode(data, offset)
let addrByteLen = varint.decode.bytes
Expand All @@ -203,6 +212,7 @@ export class Address {
addrByteLen += varint.decode.bytes

addrByteLen += UNDERLYING_HASH_LEN
return data.slice(offset, offset + addrByteLen)
const extractedData = data.slice(offset, offset + addrByteLen)
return { data: extractedData, length: addrByteLen }
}
}
13 changes: 13 additions & 0 deletions packages/massa-web3/src/experimental/basicElements/internal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import Serializer from '../crypto/interfaces/serializer'
import { Versioner, Version } from '../crypto/interfaces/versioner'

/**
* Deserialize a string data into non versioned bytes and checks that expected version match.
*
* @returns the extracted data.
*/
export function extractData(
serializer: Serializer,
versioner: Versioner,
Expand All @@ -17,6 +22,14 @@ export function extractData(
return extractedData
}

/**
* Get the prefix of a string and validate it against the expected ones.
*
* @remarks
* If several prefixes are expected, their length must be the same.
*
* @returns the extracted prefix.
*/
export function mustExtractPrefix(str: string, ...expected: string[]): string {
const prefix = str.slice(0, expected[0].length)
if (!expected.includes(prefix)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,12 @@ export class OperationManager {

switch (operationDetails.type) {
case OperationType.Transaction: {
const addrBytes = Address.extractFromBuffer(data, offset)
const { data: addrBytes, length } = Address.extractFromBuffer(
data,
offset
)
const recipientAddress = Address.fromBytes(addrBytes)
offset += addrBytes.length
offset += length
return {
...operationDetails,
recipientAddress,
Expand Down
13 changes: 3 additions & 10 deletions packages/massa-web3/test/experimental/unit/address.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Account } from '../../../src/experimental/account'
import { Address } from '../../../src/experimental/basicElements'
import { Version } from '../../../src/experimental/crypto/interfaces/versioner'
import { Address as LegacyAddress } from '../../../src/utils/keyAndAddresses'

const contractAddress = 'AS1eK3SEXGDAWN6pZhdr4Q7WJv6UHss55EB14hPy4XqBpiktfPu6'
Expand Down Expand Up @@ -31,13 +30,6 @@ describe('Address tests', () => {
)
})

test('fromPublicKey throws error for invalid version', () => {
const invalidVersion = -1 as Version
expect(() =>
Address.fromPublicKey(account.publicKey, invalidVersion)
).toThrow(/unsupported version:/)
})

test('toString returns string with user prefix for EOA', () => {
const address = Address.fromPublicKey(account.publicKey)
expect(address.toString()).toMatch(/^AU/)
Expand Down Expand Up @@ -65,8 +57,9 @@ describe('Address tests', () => {
test('extractFromBuffer returns extracted address bytes', () => {
const address = Address.fromPublicKey(account.publicKey)
const buffer = Uint8Array.from([...address.toBytes(), 1, 2, 3, 4])
const addressBytes = Address.extractFromBuffer(buffer)
const { data, length } = Address.extractFromBuffer(buffer)

expect(addressBytes).toStrictEqual(address.toBytes())
expect(data).toStrictEqual(address.toBytes())
expect(length).toStrictEqual(address.toBytes().length)
})
})

1 comment on commit 32dd742

@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 46.31% 671/1449
🔴 Branches 21.99% 84/382
🔴 Functions 38.38% 114/297
🔴 Lines 46.11% 658/1427

Test suite run success

47 tests passing in 7 suites.

Report generated by 🧪jest coverage report action from 32dd742

Please sign in to comment.