Skip to content

Commit 7e3a7cb

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

File tree

16 files changed

+616
-0
lines changed

16 files changed

+616
-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.3",
56+
"@solana/codecs": "2.0.0-rc.3"
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: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
import {
2+
AccountLinkNode,
3+
AccountNode,
4+
BytesEncoding,
5+
DefinedTypeLinkNode,
6+
DefinedTypeNode,
7+
InstructionArgumentLinkNode,
8+
InstructionArgumentNode,
9+
InstructionLinkNode,
10+
InstructionNode,
11+
NumberFormat,
12+
RegisteredTypeNode,
13+
structFieldTypeNodeFromInstructionArgumentNode,
14+
structTypeNodeFromInstructionArgumentNodes,
15+
} from '@codama/nodes';
16+
import {
17+
getLastNodeFromPath,
18+
getRecordLinkablesVisitor,
19+
LinkableDictionary,
20+
NodePath,
21+
NodeStack,
22+
pipe,
23+
recordNodeStackVisitor,
24+
visit,
25+
Visitor,
26+
} from '@codama/visitors-core';
27+
import { getAddressCodec } from '@solana/addresses';
28+
import {
29+
Codec,
30+
getBase16Codec,
31+
getBase58Codec,
32+
getBase64Codec,
33+
getBooleanCodec,
34+
getBytesCodec,
35+
getF32Codec,
36+
getF64Codec,
37+
getI8Codec,
38+
getI16Codec,
39+
getI32Codec,
40+
getI64Codec,
41+
getI128Codec,
42+
getShortU16Codec,
43+
getU8Codec,
44+
getU16Codec,
45+
getU32Codec,
46+
getU64Codec,
47+
getU128Codec,
48+
getUtf8Codec,
49+
NumberCodec,
50+
} from '@solana/codecs';
51+
52+
export type EncodableNodes =
53+
| AccountLinkNode
54+
| AccountNode
55+
| DefinedTypeLinkNode
56+
| DefinedTypeNode
57+
| InstructionArgumentLinkNode
58+
| InstructionArgumentNode
59+
| InstructionLinkNode
60+
| InstructionNode
61+
| RegisteredTypeNode;
62+
63+
export function getNodeCodec(path: NodePath<EncodableNodes>): Codec<unknown> {
64+
const linkables = new LinkableDictionary();
65+
visit(path[0], getRecordLinkablesVisitor(linkables));
66+
return visit(getLastNodeFromPath(path), getNodeCodecVisitor(linkables));
67+
}
68+
69+
export function getNodeCodecVisitor(
70+
linkables: LinkableDictionary,
71+
options: { stack?: NodeStack } = {},
72+
): Visitor<Codec<unknown>, EncodableNodes['kind']> {
73+
const stack = options.stack ?? new NodeStack();
74+
75+
const errorHandler = (): Codec<unknown> => {
76+
throw new Error('Not implemented');
77+
};
78+
79+
const baseVisitor: Visitor<Codec<unknown>, EncodableNodes['kind']> = {
80+
visitAccount(node) {
81+
return visit(node.data, this);
82+
},
83+
visitAccountLink(node) {
84+
const path = linkables.getPathOrThrow(stack.getPath(node.kind));
85+
stack.pushPath(path);
86+
const result = visit(getLastNodeFromPath(path), this);
87+
stack.popPath();
88+
return result;
89+
},
90+
visitAmountType(node) {
91+
return visit(node.number, this);
92+
},
93+
visitArrayType: errorHandler,
94+
visitBooleanType(node) {
95+
const size = visit(node.size, this) as NumberCodec;
96+
return getBooleanCodec({ size }) as Codec<unknown>;
97+
},
98+
visitBytesType() {
99+
return getBytesCodec() as Codec<unknown>;
100+
},
101+
visitDateTimeType(node) {
102+
return visit(node.number, this);
103+
},
104+
visitDefinedType(node) {
105+
return visit(node.type, this);
106+
},
107+
visitDefinedTypeLink(node) {
108+
const path = linkables.getPathOrThrow(stack.getPath(node.kind));
109+
stack.pushPath(path);
110+
const result = visit(getLastNodeFromPath(path), this);
111+
stack.popPath();
112+
return result;
113+
},
114+
visitEnumType: errorHandler,
115+
visitEnumEmptyVariantType: errorHandler,
116+
visitEnumStructVariantType: errorHandler,
117+
visitEnumTupleVariantType: errorHandler,
118+
visitFixedSizeType: errorHandler,
119+
visitHiddenPrefixType: errorHandler,
120+
visitHiddenSuffixType: errorHandler,
121+
visitInstruction(node) {
122+
return visit(structTypeNodeFromInstructionArgumentNodes(node.arguments), this);
123+
},
124+
visitInstructionArgument(node) {
125+
return visit(structFieldTypeNodeFromInstructionArgumentNode(node), this);
126+
},
127+
visitInstructionArgumentLink(node) {
128+
const path = linkables.getPathOrThrow(stack.getPath(node.kind));
129+
stack.pushPath(path);
130+
const result = visit(getLastNodeFromPath(path), this);
131+
stack.popPath();
132+
return result;
133+
},
134+
visitInstructionLink(node) {
135+
const path = linkables.getPathOrThrow(stack.getPath(node.kind));
136+
stack.pushPath(path);
137+
const result = visit(getLastNodeFromPath(path), this);
138+
stack.popPath();
139+
return result;
140+
},
141+
visitMapType: errorHandler,
142+
visitNumberType(node) {
143+
return getCodecFromNumberFormat(node.format) as Codec<unknown>;
144+
},
145+
visitOptionType: errorHandler,
146+
visitPostOffsetType: errorHandler,
147+
visitPreOffsetType: errorHandler,
148+
visitPublicKeyType() {
149+
return getAddressCodec() as Codec<unknown>;
150+
},
151+
visitRemainderOptionType: errorHandler,
152+
visitSentinelType: errorHandler,
153+
visitSetType: errorHandler,
154+
visitSizePrefixType: errorHandler,
155+
visitSolAmountType(node) {
156+
return visit(node.number, this);
157+
},
158+
visitStringType(node) {
159+
return getCodecFromBytesEncoding(node.encoding) as Codec<unknown>;
160+
},
161+
visitStructType: errorHandler,
162+
visitStructFieldType: errorHandler,
163+
visitTupleType: errorHandler,
164+
visitZeroableOptionType: errorHandler,
165+
};
166+
167+
return pipe(baseVisitor, v => recordNodeStackVisitor(v, stack));
168+
}
169+
170+
function getCodecFromBytesEncoding(encoding: BytesEncoding) {
171+
switch (encoding) {
172+
case 'base16':
173+
return getBase16Codec() as Codec<unknown>;
174+
case 'base58':
175+
return getBase58Codec() as Codec<unknown>;
176+
case 'base64':
177+
return getBase64Codec() as Codec<unknown>;
178+
case 'utf8':
179+
return getUtf8Codec() as Codec<unknown>;
180+
default:
181+
// TODO: Coded error.
182+
throw new Error(`Unsupported bytes encoding: ${encoding satisfies never}`);
183+
}
184+
}
185+
186+
function getCodecFromNumberFormat(format: NumberFormat) {
187+
switch (format) {
188+
case 'u8':
189+
return getU8Codec();
190+
case 'u16':
191+
return getU16Codec();
192+
case 'u32':
193+
return getU32Codec();
194+
case 'u64':
195+
return getU64Codec();
196+
case 'u128':
197+
return getU128Codec();
198+
case 'i8':
199+
return getI8Codec();
200+
case 'i16':
201+
return getI16Codec();
202+
case 'i32':
203+
return getI32Codec();
204+
case 'i64':
205+
return getI64Codec();
206+
case 'i128':
207+
return getI128Codec();
208+
case 'f32':
209+
return getF32Codec();
210+
case 'f64':
211+
return getF64Codec();
212+
case 'shortU16':
213+
return getShortU16Codec();
214+
default:
215+
// TODO: Coded error.
216+
throw new Error(`Unsupported number format: ${format satisfies never}`);
217+
}
218+
}
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)