Skip to content

Commit

Permalink
[bcs] accept Array<number> in bcs.bytes(n).serialize (MystenLabs#20447)
Browse files Browse the repository at this point in the history
## Description 

Describe the changes or additions included in this PR.

## Test plan 

How did you test the new or updated feature?

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
hayes-mysten authored Nov 26, 2024
1 parent 53387ff commit 1dd7713
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-weeks-rescue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@mysten/bcs': patch
---

Accept arrays of numbers when serializing bcs.bytes()
5 changes: 3 additions & 2 deletions sdk/bcs/src/bcs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,14 @@ export const bcs = {
* bcs.bytes(3).serialize(new Uint8Array([1, 2, 3])).toBytes() // Uint8Array [1, 2, 3]
*/
bytes<T extends number>(size: T, options?: BcsTypeOptions<Uint8Array, Iterable<number>>) {
return fixedSizeBcsType<Uint8Array>({
return fixedSizeBcsType<Uint8Array, Iterable<number>>({
name: `bytes[${size}]`,
size,
read: (reader) => reader.readBytes(size),
write: (value, writer) => {
const array = new Uint8Array(value);
for (let i = 0; i < size; i++) {
writer.write8(value[i] ?? 0);
writer.write8(array[i] ?? 0);
}
},
...options,
Expand Down

0 comments on commit 1dd7713

Please sign in to comment.