Skip to content

Commit bd49458

Browse files
authored
3.0.0: Support special case raw binary strings (#878)
1 parent d9e608f commit bd49458

26 files changed

+1429
-189
lines changed

examples/app.ts

+28-13
Original file line numberDiff line numberDiff line change
@@ -132,29 +132,44 @@ async function main() {
132132

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

138-
// decode b64 string key with Buffer
139-
const globalKey = algosdk.base64ToString(globalState.key);
143+
const globalKey = algosdk.base64ToBytes(globalState[0].key);
140144
// show global value
141-
const globalValue = globalState.value.bytes;
145+
const globalValue = algosdk.base64ToBytes(globalState[0].value.bytes);
142146

143-
console.log(`Decoded global state - ${globalKey}: ${globalValue}`);
147+
console.log(
148+
`Decoded global state - ${algosdk.bytesToBase64(globalKey)}: ${algosdk.bytesToBase64(globalValue)}`
149+
);
144150

145151
const accountAppInfo = await algodClient
146152
.accountApplicationInformation(caller.addr, appId)
147153
.do();
154+
if (
155+
!accountAppInfo.appLocalState ||
156+
!accountAppInfo.appLocalState.keyValue ||
157+
accountAppInfo.appLocalState.keyValue.length === 0
158+
) {
159+
throw new Error('Local state values not present');
160+
}
161+
const localState = accountAppInfo.appLocalState.keyValue;
162+
console.log(
163+
`Raw local state - ${localState.map((kv) => algosdk.encodeJSON(kv))}`
164+
);
148165

149-
const localState = accountAppInfo.appLocalState.keyValue[0];
150-
console.log(`Raw local state - ${algosdk.stringifyJSON(localState)}`);
151-
152-
// decode b64 string key with Buffer
153-
const localKey = algosdk.base64ToString(localState.key);
166+
const localKey = algosdk.base64ToBytes(localState[0].key);
154167
// get uint value directly
155-
const localValue = localState.value.uint;
168+
const localValue = localState[0].value.uint;
156169

157-
console.log(`Decoded local state - ${localKey}: ${localValue}`);
170+
console.log(
171+
`Decoded local state - ${algosdk.bytesToBase64(localKey)}: ${localValue}`
172+
);
158173
// example: APP_READ_STATE
159174

160175
// example: APP_CLOSEOUT

examples/codec.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ async function main() {
2020

2121
// example: CODEC_BASE64
2222
const b64Encoded = 'SGksIEknbSBkZWNvZGVkIGZyb20gYmFzZTY0';
23-
const b64Decoded = algosdk.base64ToString(b64Encoded);
23+
const b64Decoded = algosdk.base64ToBytes(b64Encoded);
2424
console.log(b64Encoded, b64Decoded);
2525
// example: CODEC_BASE64
2626

package-lock.json

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"url": "git://github.com/algorand/js-algorand-sdk.git"
4949
},
5050
"dependencies": {
51-
"algorand-msgpack": "^1.0.1",
51+
"algorand-msgpack": "^1.1.0",
5252
"hi-base32": "^0.5.1",
5353
"js-sha256": "^0.9.0",
5454
"js-sha3": "^0.8.0",

src/encoding/binarydata.ts

+8-12
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,6 @@ export function base64ToBytes(base64String: string): Uint8Array {
1313
return Uint8Array.from(binString, (m) => m.codePointAt(0)!);
1414
}
1515

16-
/**
17-
* Decode a base64 string for Node.js and browser environments.
18-
* @returns A decoded string
19-
*/
20-
export function base64ToString(base64String: string): string {
21-
if (isNode()) {
22-
return Buffer.from(base64String, 'base64').toString();
23-
}
24-
const binString = base64ToBytes(base64String);
25-
return new TextDecoder().decode(binString);
26-
}
27-
2816
/**
2917
* Convert a Uint8Array to a base64 string for Node.js and browser environments.
3018
* @returns A base64 string
@@ -40,6 +28,14 @@ export function bytesToBase64(byteArray: Uint8Array): string {
4028
return btoa(binString);
4129
}
4230

31+
/**
32+
* Convert a byte array to a UTF-8 string. Warning: not all byte arrays are valid UTF-8.
33+
* @returns A decoded string
34+
*/
35+
export function bytesToString(byteArray: Uint8Array): string {
36+
return new TextDecoder().decode(byteArray);
37+
}
38+
4339
/**
4440
* Returns a Uint8Array given an input string or Uint8Array.
4541
* @returns A base64 string

0 commit comments

Comments
 (0)