-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move encode/decode to dedicated module
- Loading branch information
Showing
2 changed files
with
93 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { IDL } from '@dfinity/candid'; | ||
|
||
import { AzleVec, AzleOpt, AzleTuple } from '../../lib_new'; | ||
import { toIDLType } from '../../lib_new/utils'; | ||
import { | ||
DecodeVisitor, | ||
EncodeVisitor | ||
} from '../../lib_new/visitors/encode_decode'; | ||
import { CandidType } from '../'; | ||
|
||
/** | ||
* Encodes the provided value as candid blob of the designated type. | ||
* | ||
* Intended to be a rich replacement for `IDL.encode` from `@dfinity/candid`, | ||
* adding support for complex Azle IDL wrappers such as {@link AzleOpt}, | ||
* {@link AzleVec}, and {@link AzleTuple}. It recursively visits all "inner" | ||
* values, converting any Azle values to official IDL values. | ||
* | ||
* @param data the value to encode | ||
* @param fakeIdl either a built-in IDL data type, or an Azle-defined super-type | ||
* @returns candid bytes | ||
*/ | ||
export function encode( | ||
data: any, | ||
fakeIdl: IDL.Type<any> | CandidType | ||
): Uint8Array { | ||
// TODO: there is a discrepancy between CandidType and CandidClass that | ||
// needs to be aligned so that this isn't an error. Both are representing | ||
// candid IDLs, either from the @dfinity/candid library or the | ||
// Azle-augmented ones | ||
const realIDL = toIDLType(fakeIdl, []); | ||
|
||
const encodeReadyKey = realIDL.accept(new EncodeVisitor(), { | ||
js_class: fakeIdl, | ||
js_data: data | ||
}); | ||
|
||
return new Uint8Array(IDL.encode([realIDL], [encodeReadyKey])); | ||
} | ||
|
||
/** | ||
* Decodes the provided buffer into the designated JS value. | ||
* | ||
* Intended to be a rich replacement for `IDL.decode` from `@dfinity/candid` | ||
* adding support for complex Azle IDL wrappers such as {@link AzleOpt}, | ||
* {@link AzleVec}, and {@link AzleTuple}. It recursively visits all "inner" | ||
* values, converting them from their native shape to the shape that Azle expects. | ||
* | ||
* @param data the value to decode | ||
* @param fakeIdl either a built-in IDL data type, or an Azle-defined super-type | ||
* @returns the Azle representation of the data | ||
*/ | ||
export function decode( | ||
data: ArrayBuffer, | ||
fakeIdl: IDL.Type<any> | CandidType | ||
): any { | ||
// TODO: there is a discrepancy between CandidType and CandidClass that | ||
// needs to be aligned so that this isn't an error. Both are representing | ||
// candid IDLs, either from the @dfinity/candid library or the | ||
// Azle-augmented ones | ||
const realIDL = toIDLType(fakeIdl, []); | ||
|
||
const candidDecodedValue = IDL.decode([realIDL], data)[0] as any; | ||
|
||
return realIDL.accept(new DecodeVisitor(), { | ||
js_class: fakeIdl, | ||
js_data: candidDecodedValue | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters