Skip to content

Commit 0593cc6

Browse files
committed
Add serialization functions for TokenId
1 parent ab1fc80 commit 0593cc6

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

packages/sdk/src/plt/TokenId.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,3 +136,25 @@ export function toProto(tokenId: Type): Proto.TokenId {
136136
symbol: tokenId.symbol,
137137
};
138138
}
139+
140+
/**
141+
* Encode a TokenId to UTF-8 bytes. This is the serialization format used for token IDs in transactions.
142+
*
143+
* @param {TokenId} tokenId - The TokenId to encode.
144+
* @returns {Uint8Array} The UTF-8 byte representation of the TokenId.
145+
*/
146+
export function toBytes(tokenId: TokenId): Uint8Array {
147+
return new TextEncoder().encode(tokenId.symbol);
148+
}
149+
150+
/**
151+
* Decode UTF-8 bytes to a TokenId. This can be used to deserialize token IDs in transactions.
152+
*
153+
* @param {Uint8Array} bytes - The UTF-8 byte array to decode.
154+
* @returns {TokenId} The decoded TokenId.
155+
* @throws {Err} If the decoded string is longer than 256 utf-8 encoded bytes.
156+
*/
157+
export function fromBytes(bytes: ArrayBuffer): TokenId {
158+
const symbol = new TextDecoder().decode(bytes);
159+
return fromString(symbol);
160+
}

0 commit comments

Comments
 (0)