-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into ns/feat/subscribe-to-transaction-status-ch…
…anges
- Loading branch information
Showing
19 changed files
with
476 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@fuel-ts/abi-coder": minor | ||
--- | ||
|
||
Add support for Bytes and RawSlice |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { Uint8ArrayWithDynamicData } from '../utilities'; | ||
|
||
import { ByteCoder } from './byte'; | ||
|
||
describe('ByteCoder', () => { | ||
it('should encode a byte', () => { | ||
const coder = new ByteCoder(); | ||
const expected: Uint8ArrayWithDynamicData = new Uint8Array([ | ||
0, 0, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 3, | ||
]); | ||
expected.dynamicData = { | ||
0: new Uint8Array([1, 2, 3, 0, 0, 0, 0, 0]), | ||
}; | ||
|
||
const actual = coder.encode([1, 2, 3]); | ||
|
||
expect(actual).toStrictEqual(expected); | ||
}); | ||
|
||
it('should encode a byte [full word]', () => { | ||
const coder = new ByteCoder(); | ||
const expected: Uint8ArrayWithDynamicData = new Uint8Array([ | ||
0, 0, 0, 0, 0, 0, 0, 24, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 8, | ||
]); | ||
expected.dynamicData = { | ||
0: new Uint8Array([1, 2, 3, 4, 5, 6, 7, 8]), | ||
}; | ||
|
||
const actual = coder.encode([1, 2, 3, 4, 5, 6, 7, 8]); | ||
|
||
expect(actual).toStrictEqual(expected); | ||
}); | ||
|
||
it('should decode a byte', () => { | ||
const coder = new ByteCoder(); | ||
const input = new Uint8Array([ | ||
0, 0, 0, 0, 3, 255, 255, 225, 0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 10, 0, 1, 2, 3, 4, | ||
5, 6, 7, 8, 9, | ||
]); | ||
const expected = new Uint8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); | ||
|
||
const [actual, newOffset] = coder.decode(input, 0); | ||
|
||
expect(actual).toEqual(expected); | ||
expect(newOffset).toEqual(24); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { arrayify, concat } from '@ethersproject/bytes'; | ||
import { ErrorCode } from '@fuel-ts/errors'; | ||
import { bn } from '@fuel-ts/math'; | ||
|
||
import { WORD_SIZE } from '../constants'; | ||
import type { Uint8ArrayWithDynamicData } from '../utilities'; | ||
import { BASE_VECTOR_OFFSET, concatWithDynamicData } from '../utilities'; | ||
|
||
import { Coder } from './abstract-coder'; | ||
import { U64Coder } from './u64'; | ||
|
||
export class ByteCoder extends Coder<number[], Uint8Array> { | ||
static memorySize = 1; | ||
constructor() { | ||
super('struct', 'struct Bytes', BASE_VECTOR_OFFSET); | ||
} | ||
|
||
encode(value: number[]): Uint8Array { | ||
if (!Array.isArray(value)) { | ||
this.throwError(ErrorCode.ENCODE_ERROR, `Expected array value.`); | ||
} | ||
|
||
const parts: Uint8Array[] = []; | ||
|
||
// pointer (ptr) | ||
const pointer: Uint8ArrayWithDynamicData = new U64Coder().encode(BASE_VECTOR_OFFSET); | ||
|
||
// pointer dynamicData, encode the byte vector now and attach to its pointer | ||
const data = this.#getPaddedData(value); | ||
pointer.dynamicData = { | ||
0: concatWithDynamicData([data]), | ||
}; | ||
|
||
parts.push(pointer); | ||
|
||
// capacity (cap) | ||
parts.push(new U64Coder().encode(data.byteLength)); | ||
|
||
// length (len) | ||
parts.push(new U64Coder().encode(value.length)); | ||
|
||
return concatWithDynamicData(parts); | ||
} | ||
|
||
#getPaddedData(value: number[]): Uint8Array { | ||
const data: Uint8Array[] = [arrayify(value)]; | ||
|
||
const paddingLength = (WORD_SIZE - (value.length % WORD_SIZE)) % WORD_SIZE; | ||
if (paddingLength) { | ||
data.push(new Uint8Array(paddingLength)); | ||
} | ||
|
||
return concat(data); | ||
} | ||
|
||
decode(data: Uint8Array, offset: number): [Uint8Array, number] { | ||
const len = data.slice(16, 24); | ||
const length = bn(new U64Coder().decode(len, 0)[0]).toNumber(); | ||
const byteData = data.slice(BASE_VECTOR_OFFSET, BASE_VECTOR_OFFSET + length * 8); | ||
|
||
return [byteData, offset + BASE_VECTOR_OFFSET]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import type { BN } from '@fuel-ts/math'; | ||
|
||
import type { Uint8ArrayWithDynamicData } from '../utilities'; | ||
|
||
import { RawSliceCoder } from './raw-slice'; | ||
|
||
describe('RawSliceCoder', () => { | ||
it('should encode a raw-slice', () => { | ||
const coder = new RawSliceCoder(); | ||
const expected: Uint8ArrayWithDynamicData = new Uint8Array([ | ||
0, 0, 0, 0, 0, 0, 0, 16, 0, 0, 0, 0, 0, 0, 0, 24, | ||
]); | ||
expected.dynamicData = { | ||
0: new Uint8Array([0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3]), | ||
}; | ||
|
||
const actual = coder.encode([1, 2, 3]); | ||
|
||
expect(actual).toStrictEqual(expected); | ||
}); | ||
|
||
it('should decode a raw-slice', () => { | ||
const coder = new RawSliceCoder(); | ||
const input = new Uint8Array([ | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, | ||
3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0, 0, | ||
0, 7, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 9, | ||
]); | ||
|
||
const [actual, newOffset] = coder.decode(input, 0); | ||
|
||
expect(actual.map((v: BN) => v.toNumber())).toStrictEqual([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]); | ||
expect(newOffset).toEqual(80); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { ErrorCode } from '@fuel-ts/errors'; | ||
import type { BN } from '@fuel-ts/math'; | ||
|
||
import { WORD_SIZE } from '../constants'; | ||
import type { Uint8ArrayWithDynamicData } from '../utilities'; | ||
import { BASE_RAW_SLICE_OFFSET, concatWithDynamicData } from '../utilities'; | ||
|
||
import { Coder } from './abstract-coder'; | ||
import { ArrayCoder } from './array'; | ||
import { U64Coder } from './u64'; | ||
|
||
export class RawSliceCoder extends Coder<number[], BN[]> { | ||
constructor() { | ||
super('raw untyped slice', 'raw untyped slice', BASE_RAW_SLICE_OFFSET); | ||
} | ||
|
||
encode(value: number[]): Uint8Array { | ||
if (!Array.isArray(value)) { | ||
this.throwError(ErrorCode.ENCODE_ERROR, `Expected array value.`); | ||
} | ||
|
||
const parts: Uint8Array[] = []; | ||
const coder = new U64Coder(); | ||
|
||
// pointer (ptr) | ||
const pointer: Uint8ArrayWithDynamicData = new U64Coder().encode(BASE_RAW_SLICE_OFFSET); | ||
|
||
// pointer dynamicData, encode the vector now and attach to its pointer | ||
pointer.dynamicData = { | ||
0: concatWithDynamicData(value.map((v) => coder.encode(v))), | ||
}; | ||
|
||
parts.push(pointer); | ||
|
||
// length (len) | ||
parts.push(new U64Coder().encode(value.length * WORD_SIZE)); | ||
|
||
return concatWithDynamicData(parts); | ||
} | ||
|
||
decode(data: Uint8Array, offset: number): [BN[], number] { | ||
const internalCoder = new ArrayCoder(new U64Coder(), data.length / 8); | ||
const decoded = internalCoder.decode(data, offset); | ||
|
||
return decoded; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[project] | ||
license = "Apache-2.0" | ||
name = "bytes" |
Oops, something went wrong.