Skip to content

Commit

Permalink
3.0.0: Support special case raw binary strings (#878)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonpaulos authored Aug 5, 2024
1 parent d9e608f commit bd49458
Show file tree
Hide file tree
Showing 26 changed files with 1,429 additions and 189 deletions.
41 changes: 28 additions & 13 deletions examples/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,29 +132,44 @@ async function main() {

// example: APP_READ_STATE
const appInfo = await algodClient.getApplicationByID(appId).do();
const globalState = appInfo.params.globalState[0];
console.log(`Raw global state - ${algosdk.stringifyJSON(globalState)}`);
if (!appInfo.params.globalState || appInfo.params.globalState.length === 0) {
throw new Error('Global state not present');
}
const { globalState } = appInfo.params;
console.log(
`Raw global state - ${globalState.map((kv) => algosdk.encodeJSON(kv))}`
);

// decode b64 string key with Buffer
const globalKey = algosdk.base64ToString(globalState.key);
const globalKey = algosdk.base64ToBytes(globalState[0].key);
// show global value
const globalValue = globalState.value.bytes;
const globalValue = algosdk.base64ToBytes(globalState[0].value.bytes);

console.log(`Decoded global state - ${globalKey}: ${globalValue}`);
console.log(
`Decoded global state - ${algosdk.bytesToBase64(globalKey)}: ${algosdk.bytesToBase64(globalValue)}`
);

const accountAppInfo = await algodClient
.accountApplicationInformation(caller.addr, appId)
.do();
if (
!accountAppInfo.appLocalState ||
!accountAppInfo.appLocalState.keyValue ||
accountAppInfo.appLocalState.keyValue.length === 0
) {
throw new Error('Local state values not present');
}
const localState = accountAppInfo.appLocalState.keyValue;
console.log(
`Raw local state - ${localState.map((kv) => algosdk.encodeJSON(kv))}`
);

const localState = accountAppInfo.appLocalState.keyValue[0];
console.log(`Raw local state - ${algosdk.stringifyJSON(localState)}`);

// decode b64 string key with Buffer
const localKey = algosdk.base64ToString(localState.key);
const localKey = algosdk.base64ToBytes(localState[0].key);
// get uint value directly
const localValue = localState.value.uint;
const localValue = localState[0].value.uint;

console.log(`Decoded local state - ${localKey}: ${localValue}`);
console.log(
`Decoded local state - ${algosdk.bytesToBase64(localKey)}: ${localValue}`
);
// example: APP_READ_STATE

// example: APP_CLOSEOUT
Expand Down
2 changes: 1 addition & 1 deletion examples/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ async function main() {

// example: CODEC_BASE64
const b64Encoded = 'SGksIEknbSBkZWNvZGVkIGZyb20gYmFzZTY0';
const b64Decoded = algosdk.base64ToString(b64Encoded);
const b64Decoded = algosdk.base64ToBytes(b64Encoded);
console.log(b64Encoded, b64Decoded);
// example: CODEC_BASE64

Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"url": "git://github.com/algorand/js-algorand-sdk.git"
},
"dependencies": {
"algorand-msgpack": "^1.0.1",
"algorand-msgpack": "^1.1.0",
"hi-base32": "^0.5.1",
"js-sha256": "^0.9.0",
"js-sha3": "^0.8.0",
Expand Down
20 changes: 8 additions & 12 deletions src/encoding/binarydata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,6 @@ export function base64ToBytes(base64String: string): Uint8Array {
return Uint8Array.from(binString, (m) => m.codePointAt(0)!);
}

/**
* Decode a base64 string for Node.js and browser environments.
* @returns A decoded string
*/
export function base64ToString(base64String: string): string {
if (isNode()) {
return Buffer.from(base64String, 'base64').toString();
}
const binString = base64ToBytes(base64String);
return new TextDecoder().decode(binString);
}

/**
* Convert a Uint8Array to a base64 string for Node.js and browser environments.
* @returns A base64 string
Expand All @@ -40,6 +28,14 @@ export function bytesToBase64(byteArray: Uint8Array): string {
return btoa(binString);
}

/**
* Convert a byte array to a UTF-8 string. Warning: not all byte arrays are valid UTF-8.
* @returns A decoded string
*/
export function bytesToString(byteArray: Uint8Array): string {
return new TextDecoder().decode(byteArray);
}

/**
* Returns a Uint8Array given an input string or Uint8Array.
* @returns A base64 string
Expand Down
Loading

0 comments on commit bd49458

Please sign in to comment.