Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3.0.0: Support special case raw binary strings #878

Merged
merged 22 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,21 @@ commands:
<< parameters.sudo >> apt -y install curl make git build-essential jq unzip
- node/install:
node-version: '18'
- run:
name: Install algorand-msgpack from branch (temporary)
command: |
set -e
cd ..
git clone https://github.com/algorand/msgpack-javascript.git
cd msgpack-javascript
git checkout raw-string-encoding
npm i
npm run prepare
npm pack
- run:
name: npm ci
command: |
set -e
npm ci
cp ../msgpack-javascript/algorand-msgpack-1.0.1.tgz algorand-msgpack-1.0.1.tgz
if [ "<< parameters.browser >>" == "chrome" ]; then npm install chromedriver@latest; fi
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
7 changes: 4 additions & 3 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": "file:../msgpack-javascript/algorand-msgpack-1.0.1.tgz",
"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