Skip to content

Commit

Permalink
[xp] fix transfer deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
peterjah committed Apr 23, 2024
1 parent 8c15084 commit 7f50e80
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 9 deletions.
21 changes: 19 additions & 2 deletions packages/massa-web3/src/experimental/basicElements/address.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import varint from 'varint'
const ADDRESS_PREFIX = 'A'
const ADDRESS_USER_PREFIX = 'U'
const ADDRESS_CONTRACT_PREFIX = 'S'
export const BLAKE3_HASH_BYTELENGTH = 32
const UNDERLYING_HASH_LEN = 32
/**
* A class representing an address.
*
Expand Down Expand Up @@ -113,7 +113,7 @@ export class Address {
)
const address = Address.initFromVersion(version)
address.bytes = data
address.isEOA = !!addressType
address.isEOA = addressType === 0
return address
}

Expand Down Expand Up @@ -144,4 +144,21 @@ export class Address {
this.isEOA ? ADDRESS_USER_PREFIX : ADDRESS_CONTRACT_PREFIX
}${this.serializer.serialize(versionedBytes)}`
}

/**
* Get byte length of address in binary format .
*
* @returns The address length in bytes.
*/
static getByteLength(data: Uint8Array): number {

Check warning on line 153 in packages/massa-web3/src/experimental/basicElements/address.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🕹️ Function is not covered

Warning! Not covered function
// addr type
varint.decode(data)

Check warning on line 155 in packages/massa-web3/src/experimental/basicElements/address.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
let addrByteLen = varint.decode.bytes

Check warning on line 156 in packages/massa-web3/src/experimental/basicElements/address.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
// version
varint.decode(data, addrByteLen)

Check warning on line 158 in packages/massa-web3/src/experimental/basicElements/address.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
addrByteLen += varint.decode.bytes

Check warning on line 159 in packages/massa-web3/src/experimental/basicElements/address.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement

addrByteLen += UNDERLYING_HASH_LEN

Check warning on line 161 in packages/massa-web3/src/experimental/basicElements/address.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
return addrByteLen

Check warning on line 162 in packages/massa-web3/src/experimental/basicElements/address.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { unsigned } from 'big-varint'
import { Address, BLAKE3_HASH_BYTELENGTH } from './address'
import { Address } from './address'
import { PrivateKey, PublicKey } from './keys'
import { BlockchainClient } from '../client'
import { Signature } from './signature'
Expand Down Expand Up @@ -95,8 +95,8 @@ export class OperationManager {
// TODO: check that unsigned.encode is equivalent to varint.encode
const components = [
unsigned.encode(operation.fee),
unsigned.encode(BigInt(operation.expirePeriod)),
unsigned.encode(BigInt(operation.type)),
varint.encode(operation.expirePeriod),
varint.encode(operation.type),
]

switch (operation.type) {
Expand Down Expand Up @@ -164,12 +164,11 @@ export class OperationManager {

switch (operationDetails.type) {
case OperationType.Transaction: {
// decode address type to get encoded size
varint.decode(data, offset)
const addrLen = Address.getByteLength(data.slice(offset))

Check warning on line 167 in packages/massa-web3/src/experimental/basicElements/operationManager.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
const recipientAddress = Address.fromBytes(
data.slice(offset, varint.decode.bytes + BLAKE3_HASH_BYTELENGTH)
data.slice(offset, offset + addrLen)

Check warning on line 169 in packages/massa-web3/src/experimental/basicElements/operationManager.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
)
offset += varint.decode.bytes + BLAKE3_HASH_BYTELENGTH
offset += addrLen

Check warning on line 171 in packages/massa-web3/src/experimental/basicElements/operationManager.ts

View workflow job for this annotation

GitHub Actions / Coverage annotations (🧪 jest-coverage-report-action)

🧾 Statement is not covered

Warning! Not covered statement
return {
...operationDetails,
recipientAddress,
Expand Down
23 changes: 23 additions & 0 deletions packages/massa-web3/test/experimental/unit/Address.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { Account } from '../../../src/experimental/account'
import { Address } from '../../../src/experimental/basicElements'
import { Address as LegacyAddress } from '../../../src/utils/keyAndAddresses'

describe('Address tests', () => {
let account: Account
beforeAll(async () => {
account = await Account.generate()
})

test('Serialization', async () => {
const addStr = account.address.toString()
const address = Address.fromString(addStr)
const ref = new LegacyAddress(addStr)
const bytes = address.toBytes()

// serialization
expect(bytes).toStrictEqual(Uint8Array.from(ref.toBytes()))

// deserialization
expect(Address.fromBytes(bytes)).toStrictEqual(Address.fromString(addStr))
})
})

0 comments on commit 7f50e80

Please sign in to comment.