Skip to content

Commit f9cb62b

Browse files
committed
Add dynamic-codecs package
1 parent 62fcd21 commit f9cb62b

File tree

16 files changed

+625
-0
lines changed

16 files changed

+625
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
module.exports = {
2+
extends: ['../../.eslintrc.js'],
3+
rules: {
4+
'sort-keys-fix/sort-keys-fix': 'off',
5+
},
6+
};

packages/dynamic-codecs/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dist/
2+
e2e/
3+
test-ledger/
4+
target/
5+
CHANGELOG.md

packages/dynamic-codecs/LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
MIT License
2+
3+
Copyright (c) 2024 Codama
4+
5+
Permission is hereby granted, free of charge, to any person obtaining
6+
a copy of this software and associated documentation files (the
7+
"Software"), to deal in the Software without restriction, including
8+
without limitation the rights to use, copy, modify, merge, publish,
9+
distribute, sublicense, and/or sell copies of the Software, and to
10+
permit persons to whom the Software is furnished to do so, subject to
11+
the following conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

packages/dynamic-codecs/README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Codama ➤ Dynamic Codecs
2+
3+
[![npm][npm-image]][npm-url]
4+
[![npm-downloads][npm-downloads-image]][npm-url]
5+
6+
[npm-downloads-image]: https://img.shields.io/npm/dm/@codama/dynamic-codecs.svg?style=flat
7+
[npm-image]: https://img.shields.io/npm/v/@codama/dynamic-codecs.svg?style=flat&label=%40codama%2Fdynamic-codecs
8+
[npm-url]: https://www.npmjs.com/package/@codama/dynamic-codecs
9+
10+
This package provides a set of helpers that provide `Codecs` for Codama nodes that describe data.
11+
12+
// TODO: Move this to `@codama/dynamic-parsers`
13+
This package provides a set of helpers that, given any Codama IDL, dynamically parses any byte array into deserialized accounts, instructions and defined types.
14+
15+
## Installation
16+
17+
```sh
18+
pnpm install @codama/dynamic-codecs
19+
```
20+
21+
> [!NOTE]
22+
> This package is **not** included in the main [`codama`](../library) package.
23+
24+
## Functions
25+
26+
TODO
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"name": "@codama/dynamic-codecs",
3+
"version": "1.0.0",
4+
"description": "Node specifications and helpers for the Codama standard",
5+
"exports": {
6+
"types": "./dist/types/index.d.ts",
7+
"react-native": "./dist/index.react-native.mjs",
8+
"browser": {
9+
"import": "./dist/index.browser.mjs",
10+
"require": "./dist/index.browser.cjs"
11+
},
12+
"node": {
13+
"import": "./dist/index.node.mjs",
14+
"require": "./dist/index.node.cjs"
15+
}
16+
},
17+
"browser": {
18+
"./dist/index.node.cjs": "./dist/index.browser.cjs",
19+
"./dist/index.node.mjs": "./dist/index.browser.mjs"
20+
},
21+
"main": "./dist/index.node.cjs",
22+
"module": "./dist/index.node.mjs",
23+
"react-native": "./dist/index.react-native.mjs",
24+
"types": "./dist/types/index.d.ts",
25+
"type": "commonjs",
26+
"files": [
27+
"./dist/types",
28+
"./dist/index.*"
29+
],
30+
"sideEffects": false,
31+
"keywords": [
32+
"solana",
33+
"framework",
34+
"standard",
35+
"specifications"
36+
],
37+
"scripts": {
38+
"build": "rimraf dist && pnpm build:src && pnpm build:types",
39+
"build:src": "zx ../../node_modules/@codama/internals/scripts/build-src.mjs package",
40+
"build:types": "zx ../../node_modules/@codama/internals/scripts/build-types.mjs",
41+
"dev": "zx ../../node_modules/@codama/internals/scripts/test-unit.mjs node --watch",
42+
"lint": "zx ../../node_modules/@codama/internals/scripts/lint.mjs",
43+
"lint:fix": "zx ../../node_modules/@codama/internals/scripts/lint.mjs --fix",
44+
"test": "pnpm test:types && pnpm test:treeshakability && pnpm test:browser && pnpm test:node && pnpm test:react-native",
45+
"test:browser": "zx ../../node_modules/@codama/internals/scripts/test-unit.mjs browser",
46+
"test:node": "zx ../../node_modules/@codama/internals/scripts/test-unit.mjs node",
47+
"test:react-native": "zx ../../node_modules/@codama/internals/scripts/test-unit.mjs react-native",
48+
"test:treeshakability": "zx ../../node_modules/@codama/internals/scripts/test-treeshakability.mjs",
49+
"test:types": "zx ../../node_modules/@codama/internals/scripts/test-types.mjs"
50+
},
51+
"dependencies": {
52+
"@codama/errors": "workspace:*",
53+
"@codama/nodes": "workspace:*",
54+
"@codama/visitors-core": "workspace:*",
55+
"@solana/addresses": "2.0.0-rc.1",
56+
"@solana/codecs": "2.0.0-rc.1"
57+
},
58+
"license": "MIT",
59+
"repository": {
60+
"type": "git",
61+
"url": "https://github.com/codama-idl/codama"
62+
},
63+
"bugs": {
64+
"url": "http://github.com/codama-idl/codama/issues"
65+
},
66+
"browserslist": [
67+
"supports bigint and not dead",
68+
"maintained node versions"
69+
]
70+
}
Lines changed: 236 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,236 @@
1+
import {
2+
AccountLinkNode,
3+
AccountNode,
4+
assertIsNode,
5+
BytesEncoding,
6+
DefinedTypeLinkNode,
7+
DefinedTypeNode,
8+
InstructionArgumentLinkNode,
9+
InstructionArgumentNode,
10+
InstructionLinkNode,
11+
InstructionNode,
12+
NumberFormat,
13+
REGISTERED_TYPE_NODE_KINDS,
14+
RegisteredTypeNode,
15+
RootNode,
16+
structFieldTypeNodeFromInstructionArgumentNode,
17+
structTypeNodeFromInstructionArgumentNodes,
18+
} from '@codama/nodes';
19+
import {
20+
getConjunctiveNodeSelectorFunction,
21+
interceptVisitor,
22+
LinkableDictionary,
23+
NodeSelector,
24+
pipe,
25+
recordLinkablesVisitor,
26+
visit,
27+
Visitor,
28+
voidVisitor,
29+
} from '@codama/visitors-core';
30+
import { getAddressCodec } from '@solana/addresses';
31+
import {
32+
Codec,
33+
getBase16Codec,
34+
getBase58Codec,
35+
getBase64Codec,
36+
getBooleanCodec,
37+
getBytesCodec,
38+
getF32Codec,
39+
getF64Codec,
40+
getI8Codec,
41+
getI16Codec,
42+
getI32Codec,
43+
getI64Codec,
44+
getI128Codec,
45+
getShortU16Codec,
46+
getU8Codec,
47+
getU16Codec,
48+
getU32Codec,
49+
getU64Codec,
50+
getU128Codec,
51+
getUtf8Codec,
52+
NumberCodec,
53+
} from '@solana/codecs';
54+
55+
type EncodableNodes =
56+
| AccountLinkNode
57+
| AccountNode
58+
| DefinedTypeLinkNode
59+
| DefinedTypeNode
60+
| InstructionArgumentLinkNode
61+
| InstructionArgumentNode
62+
| InstructionLinkNode
63+
| InstructionNode
64+
| RegisteredTypeNode;
65+
66+
const ENCODABLE_NODE_KINDS = [
67+
'accountLinkNode' as const,
68+
'accountNode' as const,
69+
'definedTypeLinkNode' as const,
70+
'definedTypeNode' as const,
71+
'instructionArgumentLinkNode' as const,
72+
'instructionArgumentNode' as const,
73+
'instructionLinkNode' as const,
74+
'instructionNode' as const,
75+
...REGISTERED_TYPE_NODE_KINDS,
76+
];
77+
78+
export function getTypeNodeCodec(node: EncodableNodes): Codec<unknown> {
79+
return visit(node, getTypeNodeCodecVisitor(new LinkableDictionary()));
80+
}
81+
82+
export function getNodeCodec(rootNode: RootNode, selector: NodeSelector | NodeSelector[]): Codec<unknown> {
83+
const linkables = new LinkableDictionary();
84+
const typeNodeVisitor = getTypeNodeCodecVisitor(linkables);
85+
const isSelected = getConjunctiveNodeSelectorFunction(selector);
86+
let codec: Codec<unknown> | undefined;
87+
88+
const visitor = pipe(
89+
voidVisitor(),
90+
v =>
91+
interceptVisitor(v, (node, next) => {
92+
if (!isSelected(node, linkables.stack)) return next(node);
93+
assertIsNode(node, ENCODABLE_NODE_KINDS);
94+
codec = visit(node, typeNodeVisitor);
95+
}),
96+
v => recordLinkablesVisitor(v, linkables),
97+
);
98+
99+
visit(rootNode, visitor);
100+
101+
if (!codec) {
102+
// TODO: Coded error.
103+
throw new Error('Node Selector not found');
104+
}
105+
106+
return codec;
107+
}
108+
109+
export function getTypeNodeCodecVisitor(
110+
linkables: LinkableDictionary,
111+
): Visitor<Codec<unknown>, EncodableNodes['kind']> {
112+
const errorHandler = (): Codec<unknown> => {
113+
throw new Error('Not implemented');
114+
};
115+
return {
116+
visitAccount(node) {
117+
return visit(node.data, this);
118+
},
119+
visitAccountLink(node) {
120+
return visit(linkables.getOrThrow(node), this);
121+
},
122+
visitAmountType(node) {
123+
return visit(node.number, this);
124+
},
125+
visitArrayType: errorHandler,
126+
visitBooleanType(node) {
127+
const size = visit(node.size, this) as NumberCodec;
128+
return getBooleanCodec({ size }) as Codec<unknown>;
129+
},
130+
visitBytesType() {
131+
return getBytesCodec() as Codec<unknown>;
132+
},
133+
visitDateTimeType(node) {
134+
return visit(node.number, this);
135+
},
136+
visitDefinedType(node) {
137+
return visit(node.type, this);
138+
},
139+
visitDefinedTypeLink(node) {
140+
return visit(linkables.getOrThrow(node), this);
141+
},
142+
visitEnumType: errorHandler,
143+
visitEnumEmptyVariantType: errorHandler,
144+
visitEnumStructVariantType: errorHandler,
145+
visitEnumTupleVariantType: errorHandler,
146+
visitFixedSizeType: errorHandler,
147+
visitHiddenPrefixType: errorHandler,
148+
visitHiddenSuffixType: errorHandler,
149+
visitInstruction(node) {
150+
return visit(structTypeNodeFromInstructionArgumentNodes(node.arguments), this);
151+
},
152+
visitInstructionArgument(node) {
153+
return visit(structFieldTypeNodeFromInstructionArgumentNode(node), this);
154+
},
155+
visitInstructionArgumentLink(node) {
156+
return visit(linkables.getOrThrow(node), this);
157+
},
158+
visitInstructionLink(node) {
159+
return visit(linkables.getOrThrow(node), this);
160+
},
161+
visitMapType: errorHandler,
162+
visitNumberType(node) {
163+
return getCodecFromNumberFormat(node.format) as Codec<unknown>;
164+
},
165+
visitOptionType: errorHandler,
166+
visitPostOffsetType: errorHandler,
167+
visitPreOffsetType: errorHandler,
168+
visitPublicKeyType() {
169+
return getAddressCodec() as Codec<unknown>;
170+
},
171+
visitRemainderOptionType: errorHandler,
172+
visitSentinelType: errorHandler,
173+
visitSetType: errorHandler,
174+
visitSizePrefixType: errorHandler,
175+
visitSolAmountType(node) {
176+
return visit(node.number, this);
177+
},
178+
visitStringType(node) {
179+
return getCodecFromBytesEncoding(node.encoding) as Codec<unknown>;
180+
},
181+
visitStructType: errorHandler,
182+
visitStructFieldType: errorHandler,
183+
visitTupleType: errorHandler,
184+
visitZeroableOptionType: errorHandler,
185+
};
186+
}
187+
188+
function getCodecFromBytesEncoding(encoding: BytesEncoding) {
189+
switch (encoding) {
190+
case 'base16':
191+
return getBase16Codec() as Codec<unknown>;
192+
case 'base58':
193+
return getBase58Codec() as Codec<unknown>;
194+
case 'base64':
195+
return getBase64Codec() as Codec<unknown>;
196+
case 'utf8':
197+
return getUtf8Codec() as Codec<unknown>;
198+
default:
199+
// TODO: Coded error.
200+
throw new Error(`Unsupported bytes encoding: ${encoding satisfies never}`);
201+
}
202+
}
203+
204+
function getCodecFromNumberFormat(format: NumberFormat) {
205+
switch (format) {
206+
case 'u8':
207+
return getU8Codec();
208+
case 'u16':
209+
return getU16Codec();
210+
case 'u32':
211+
return getU32Codec();
212+
case 'u64':
213+
return getU64Codec();
214+
case 'u128':
215+
return getU128Codec();
216+
case 'i8':
217+
return getI8Codec();
218+
case 'i16':
219+
return getI16Codec();
220+
case 'i32':
221+
return getI32Codec();
222+
case 'i64':
223+
return getI64Codec();
224+
case 'i128':
225+
return getI128Codec();
226+
case 'f32':
227+
return getF32Codec();
228+
case 'f64':
229+
return getF64Codec();
230+
case 'shortU16':
231+
return getShortU16Codec();
232+
default:
233+
// TODO: Coded error.
234+
throw new Error(`Unsupported number format: ${format satisfies never}`);
235+
}
236+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './codecs';
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
declare const __BROWSER__: boolean;
2+
declare const __ESM__: boolean;
3+
declare const __NODEJS__: boolean;
4+
declare const __REACTNATIVE__: boolean;
5+
declare const __TEST__: boolean;
6+
declare const __VERSION__: string;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { getBase16Encoder, ReadonlyUint8Array } from '@solana/codecs';
2+
3+
export function hex(hexadecimal: string): ReadonlyUint8Array {
4+
return getBase16Encoder().encode(hexadecimal);
5+
}

0 commit comments

Comments
 (0)