Kaynağa Gözat

fix(target_chains/starknet): fix ByteBuffer.fromHex and add more conversions (#1717)

Pavel Strakhov 1 yıl önce
ebeveyn
işleme
40a63bf8f8

+ 1 - 1
target_chains/starknet/sdk/js/package.json

@@ -1,6 +1,6 @@
 {
   "name": "@pythnetwork/pyth-starknet-js",
-  "version": "0.1.0",
+  "version": "0.2.0",
   "description": "Pyth Network Starknet Utilities",
   "homepage": "https://pyth.network",
   "author": {

+ 12 - 2
target_chains/starknet/sdk/js/src/index.ts

@@ -21,7 +21,9 @@ export class ByteBuffer {
   data: string[] = [];
 
   /** Create a `ByteBuffer` from an array of `bytes31`.
-   * Use `ByteBuffer::fromHex` to create `ByteBuffer` from HEX representation of binary data.
+   * - Use `ByteBuffer::fromBuffer` to create `ByteBuffer` from a `Buffer`.
+   * - Use `ByteBuffer::fromBase64` to create `ByteBuffer` from Base64 representation of binary data.
+   * - Use `ByteBuffer::fromHex` to create `ByteBuffer` from HEX representation of binary data.
    */
   constructor(num_last_bytes: number, data: string[]) {
     this.num_last_bytes = num_last_bytes;
@@ -30,8 +32,16 @@ export class ByteBuffer {
 
   /** Create a `ByteBuffer` from HEX representation of binary data. */
   public static fromHex(hexData: string): ByteBuffer {
-    const buffer = Buffer.from(hexData, "base64");
+    return ByteBuffer.fromBuffer(Buffer.from(hexData, "hex"));
+  }
+
+  /** Create a `ByteBuffer` from Base64 representation of binary data. */
+  public static fromBase64(hexData: string): ByteBuffer {
+    return ByteBuffer.fromBuffer(Buffer.from(hexData, "base64"));
+  }
 
+  /** Create a `ByteBuffer` from a `Buffer`. */
+  public static fromBuffer(buffer: Buffer): ByteBuffer {
     let pos = 0;
     const data = [];
     while (pos < buffer.length) {