Skip to content

Commit

Permalink
Support arbitrary-sized bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Jan 22, 2024
1 parent 3d98ab7 commit 105f9f9
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 3 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@ All notable changes to this project will be documented in [Github Release](https

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Security
### Fixed
### Added
- Add support for `bytes()` arbitrary-sized type from FATE 3.
### Changed
### Deprecated
### Removed
1 change: 1 addition & 0 deletions contracts/Test.aes
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ main contract Test =
entrypoint test_int(a:int, b:int, c:int, d:int) = a + b + c + d
entrypoint test_bytes(a:bytes(2)) = a
entrypoint test_bytes32(a:bytes(32)) = a
entrypoint test_bytes_any_size(a:bytes) = a
entrypoint test_account_address(a:address) = a
entrypoint test_contract_address(a:RemoteTest) = a
entrypoint test_oracle_address(a:oracle_id) = a
Expand Down
4 changes: 2 additions & 2 deletions src/types/FateBytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ class FateBytes extends FateData {
constructor(value, size, name = 'bytes') {
super(name)

this._value = toByteArray(value, size)
this._value = toByteArray(value, size === 'any' ? undefined : size)

if (size && this._value.byteLength !== size) {
if (size && size !== 'any' && this._value.byteLength !== size) {
throw new FateTypeError(
name,
`Invalid length: got ${this._value.byteLength} bytes instead of ${size} bytes`
Expand Down
9 changes: 9 additions & 0 deletions tests/Decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ test('Decode bytes return', t => {
)
})

test('Decode bytes any size return', t => {
t.plan(1)
t.deepEqual(
encoder.decode(CONTRACT, 'test_bytes_any_size', 'cb_nwENwP/u2an5/Q=='),
new Uint8Array([0xc0, 0xff, 0xee]),
'test_bytes_any_size(Bytes.to_any_size(#c0ffee))'
)
})

test('Decode string return', t => {
t.plan(1)
t.deepEqual(
Expand Down
6 changes: 6 additions & 0 deletions tests/Encoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ test('Encode bytes arguments', t => {
t.is(encoded, 'cb_KxEe407MG58BCb7vI/elQA==', 'test_bytes(#beef)')
})

test('Encode bytes any size arguments', t => {
t.plan(1)
const encoded = encoder.encode(CONTRACT, 'test_bytes_any_size', [0xc0ffee])
t.is(encoded, 'cb_KxGHDGcIG58BDcD/7uo6XoQ=', 'test_bytes_any_size(Bytes.to_any_size(#c0ffee))')
})

test('Encode string arguments', t => {
t.plan(1)
const encoded = encoder.encode(CONTRACT, 'test_string', ["whoolymoly"])
Expand Down
7 changes: 6 additions & 1 deletion tests/Serializers/BytesSerializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const FateBytes = require('../../src/types/FateBytes')
const s = new BytesSerializer()

test('Serialize', t => {
t.plan(7)
t.plan(8)
t.deepEqual(
s.serialize(new FateBytes([0xbe, 0xef])),
[159,1,9,190,239]
Expand All @@ -16,6 +16,11 @@ test('Serialize', t => {
[159,1,9,190,239]
)

t.deepEqual(
s.serialize(new FateBytes(0xbeef, 'any')),
[159,1,9,190,239]
)

t.deepEqual(
s.serialize(new FateBytes("beef")),
[159,1,9,190,239]
Expand Down
8 changes: 8 additions & 0 deletions tests/TypeResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ test('Resolve bytes', t => {
)
})

test('Resolve bytes any size', t => {
t.plan(1)
t.deepEqual(
resolver.resolveType({bytes: 'any'}),
FateTypeBytes('any')
)
})

test('Resolve list', t => {
t.plan(1)
t.deepEqual(
Expand Down
1 change: 1 addition & 0 deletions tests/integration/decoder.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ test_decoder 'test_unit' 'cb_P4fvHVw='
test_decoder 'test_bool' 'cb_/8CwV/U='
test_decoder 'test_single_int' 'cb_b4MC7W/bKkpn'
test_decoder 'test_bytes' 'cb_nwEJvu+rlRrs'
test_decoder 'test_bytes_any_size' 'cb_nwENwP/u2an5/Q=='
test_decoder 'test_string' 'cb_KXdob29seW1vbHlGazSE'
test_decoder 'test_hash' 'cb_nwGBAAECAwQFBgcICQoLDA0ODwABAgMEBQYHCAkKCwwNDg/55Yfk'
test_decoder 'test_signature' 'cb_nwEBAAABAgMEBQYHCAkKCwwNDg8AAQIDBAUGBwgJCgsMDQ4PAAECAwQFBgcICQoLDA0ODwABAgMEBQYHCAkKCwwNDg/EV2+8'
Expand Down
1 change: 1 addition & 0 deletions tests/integration/encoder.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ test_encoder 'test_single_int(63)'
test_encoder 'test_single_int(-63)'
test_encoder 'test_int(63, -63, 64, -64)'
test_encoder 'test_bytes(#beef)'
test_encoder 'test_bytes_any_size(Bytes.to_any_size(#c0ffee))'
test_encoder 'test_string("whoolymoly")'
test_encoder 'test_hash(#000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f)'
test_encoder 'test_signature(#000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f000102030405060708090a0b0c0d0e0f)'
Expand Down

0 comments on commit 105f9f9

Please sign in to comment.