antelopeql 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/LICENSE +21 -0
  2. package/antelopeql.mjs +159 -0
  3. package/blockchain/deserialize_action_data.mjs +41 -0
  4. package/blockchain/get_abi.mjs +172 -0
  5. package/blockchain/get_account.mjs +233 -0
  6. package/blockchain/get_accounts_by_authorizers.mjs +79 -0
  7. package/blockchain/get_block.mjs +238 -0
  8. package/blockchain/get_currency_balance.mjs +48 -0
  9. package/blockchain/get_currency_stats.mjs +60 -0
  10. package/blockchain/get_info.mjs +96 -0
  11. package/blockchain/get_producers.mjs +104 -0
  12. package/blockchain/get_ram_price.mjs +57 -0
  13. package/blockchain/get_table_by_scope.mjs +96 -0
  14. package/blockchain_query_field.mjs +44 -0
  15. package/build_graphql_fields_from_abis.mjs +122 -0
  16. package/eosio_abi_to_graphql_ast.mjs +222 -0
  17. package/eosio_types/asset_type.mjs +35 -0
  18. package/eosio_types/block_timestamp_type.mjs +14 -0
  19. package/eosio_types/boolean_type.mjs +10 -0
  20. package/eosio_types/bytes_type.mjs +17 -0
  21. package/eosio_types/eosio_key_type.mjs +13 -0
  22. package/eosio_types/extended_asset_type.mjs +19 -0
  23. package/eosio_types/generate_checksum_type.mjs +20 -0
  24. package/eosio_types/generate_float_type.mjs +16 -0
  25. package/eosio_types/generate_int_type.mjs +28 -0
  26. package/eosio_types/generate_uint_type.mjs +27 -0
  27. package/eosio_types/name_type.mjs +29 -0
  28. package/eosio_types/public_key_type.mjs +26 -0
  29. package/eosio_types/signature_type.mjs +19 -0
  30. package/eosio_types/symbol_code_type.mjs +13 -0
  31. package/eosio_types/symbol_type.mjs +33 -0
  32. package/eosio_types/time_point_sec_type.mjs +12 -0
  33. package/eosio_types/time_point_type.mjs +12 -0
  34. package/eosio_types/varint32_type.mjs +13 -0
  35. package/eosio_types/varuint32_type.mjs +12 -0
  36. package/eosio_types.mjs +59 -0
  37. package/graphql_input_types/actions.mjs +15 -0
  38. package/graphql_input_types/authorization.mjs +30 -0
  39. package/graphql_input_types/configuration.mjs +55 -0
  40. package/graphql_input_types/query_argument_fields.mjs +82 -0
  41. package/graphql_object_types/authorization.mjs +15 -0
  42. package/graphql_object_types/authorizing_account_type.mjs +15 -0
  43. package/graphql_object_types/packed_transaction.mjs +32 -0
  44. package/graphql_object_types/transaction_receipt.mjs +159 -0
  45. package/mutation_resolver.mjs +234 -0
  46. package/package.json +97 -0
  47. package/push_serialized_transaction.mjs +24 -0
  48. package/push_transaction.mjs +75 -0
  49. package/push_transaction_rpc.mjs +37 -0
  50. package/query_resolver.mjs +43 -0
  51. package/readme.md +164 -0
  52. package/serialize_transaction.mjs +23 -0
  53. package/types.mjs +64 -0
@@ -0,0 +1,44 @@
1
+ import { GraphQLError, GraphQLObjectType } from "graphql";
2
+
3
+ import deserialize_action_data from "./blockchain/deserialize_action_data.mjs";
4
+ import get_abi from "./blockchain/get_abi.mjs";
5
+ import get_account from "./blockchain/get_account.mjs";
6
+ import get_accounts_by_authorizers from "./blockchain/get_accounts_by_authorizers.mjs";
7
+ import get_block from "./blockchain/get_block.mjs";
8
+ import get_currency_balance from "./blockchain/get_currency_balance.mjs";
9
+ import get_currency_stats from "./blockchain/get_currency_stats.mjs";
10
+ import get_info from "./blockchain/get_info.mjs";
11
+ import get_producers from "./blockchain/get_producers.mjs";
12
+ import get_ram_price from "./blockchain/get_ram_price.mjs";
13
+ import get_table from "./blockchain/get_table_by_scope.mjs";
14
+
15
+ const blockchain_query_field = {
16
+ description: `Retrieve infomation about the blockchain, cryptocurrency and accounts.`,
17
+ type: new GraphQLObjectType({
18
+ name: "blockchain",
19
+ fields: {
20
+ get_account,
21
+ get_abi,
22
+ get_accounts_by_authorizers,
23
+ get_block,
24
+ get_currency_balance,
25
+ get_currency_stats,
26
+ get_info,
27
+ get_producers,
28
+ deserialize_action_data,
29
+ get_table,
30
+ get_ram_price
31
+ }
32
+ }),
33
+ resolve(_, __, { network: { fetch, rpc_url } }) {
34
+ if (!fetch)
35
+ throw new GraphQLError(
36
+ "Fetch was not supplied to the `antelopeql_context`."
37
+ );
38
+ if (!rpc_url)
39
+ throw new GraphQLError("No RPC url supplied to `antelopeql_context`");
40
+ return {};
41
+ }
42
+ };
43
+
44
+ export default blockchain_query_field;
@@ -0,0 +1,122 @@
1
+ import {
2
+ GraphQLError,
3
+ GraphQLInputObjectType,
4
+ GraphQLObjectType
5
+ } from "graphql";
6
+
7
+ import {
8
+ eosio_abi_to_graphql_ast,
9
+ get_graphql_fields_from_AST
10
+ } from "./eosio_abi_to_graphql_ast.mjs";
11
+
12
+ /** @typedef {import("./types.mjs").ABI} ABI */
13
+
14
+ /**
15
+ * @typedef AccountABI
16
+ * @type {Object}
17
+ * @property {ABI} abi
18
+ * @property {String} account_name
19
+ */
20
+
21
+ /**
22
+ * Builds GraphQL query and mutation fields from a list of ABIs. These GraphQL fields can readily be consumed by a GraphQL Schema, enabling developers the ability to integrate a varienty of EOSIO based blockchains into their GraphQL service.
23
+ * @param {Array<AccountABI>} abi_list Argument.
24
+ * @returns {Object} AntelopeQL fields.
25
+ * @example <caption>`Usage` in a custom GraphQL API.</caption>
26
+ * ```js
27
+ * import actions_type from 'antelopeql/graphql_input_types/actions.js'
28
+ * import serialize_transaction from 'antelopeql/graphql_input_types/actions.js'
29
+ * import push_transaction from 'antelopeql/push_transaction.js'
30
+ *
31
+ * const network = { fetch, rpc_url: 'https://eos.relocke.io', headers: {}, signal }
32
+ * const ABI_list = [{ account_name: 'eosio.token', abi: … }]
33
+ * const { mutation_fields, query_fields, ast_list } =
34
+ * build_graphql_fields_from_abis(ABI_list)
35
+ *
36
+ * // GraphQL query with `eosio.token` queries.
37
+ * const queries = new GraphQLObjectType({
38
+ * name: 'Query',
39
+ * fields: query_fields
40
+ * })
41
+ *
42
+ * const action_fields = actions_type(mutation_fields)
43
+ *
44
+ * // GraphQL mutation with `eosio.token` actions added.
45
+ * const mutations = new GraphQLObjectType({
46
+ * name: 'Mutation',
47
+ * fields: {
48
+ * push_transaction: push_transaction(action_fields, ast_list),
49
+ * serialize_transaction: serialize_transaction(action_fields, ast_list),
50
+ * push_serialized_transaction
51
+ * }
52
+ * })
53
+ *
54
+ * const schema = new GraphQLSchema({
55
+ * query: queries,
56
+ * mutation: mutations
57
+ * })
58
+ *
59
+ * const document = parse(new Source(query)) // GraphQL document.
60
+ *
61
+ * return execute({
62
+ * schema,
63
+ * document,
64
+ * rootValue: '',
65
+ * contextValue: { network },
66
+ * fieldResolver(rootValue, args, ctx, { fieldName }) {
67
+ * return rootValue[fieldName]
68
+ * }
69
+ * })
70
+ * ```
71
+ */
72
+ export default function build_graphql_fields_from_abis(abi_list) {
73
+ const contract_query_fields = {};
74
+ const contract_mutation_fields = {};
75
+ const ast_list = {};
76
+
77
+ for (const { abi, account_name } of abi_list) {
78
+ const name = account_name.replace(/\./gmu, "_");
79
+ const AST = eosio_abi_to_graphql_ast(abi);
80
+
81
+ ast_list[name] = AST; // For use in serializing data in mutation resolver.
82
+ const { query_fields, mutation_fields } = get_graphql_fields_from_AST(
83
+ AST,
84
+ abi,
85
+ name
86
+ );
87
+
88
+ contract_query_fields[name] = {
89
+ name,
90
+ type: new GraphQLObjectType({
91
+ name: name + "_query",
92
+ fields: query_fields
93
+ }),
94
+ resolve(__, _, { network: { rpc_url, fetch } = {} }, { fieldName }) {
95
+ if (!fetch)
96
+ throw new GraphQLError(
97
+ "No fetch argument found on the context of the GraphQL.execute."
98
+ );
99
+
100
+ if (!rpc_url)
101
+ throw new GraphQLError(
102
+ "No rpc_url argument found on the context of the GraphQL.execute."
103
+ );
104
+
105
+ return { code: fieldName.replace(/_/gmu, ".") };
106
+ }
107
+ };
108
+
109
+ contract_mutation_fields[name] = {
110
+ type: new GraphQLInputObjectType({
111
+ name,
112
+ fields: mutation_fields
113
+ })
114
+ };
115
+ }
116
+
117
+ return {
118
+ query_fields: contract_query_fields,
119
+ mutation_fields: contract_mutation_fields,
120
+ ast_list
121
+ };
122
+ }
@@ -0,0 +1,222 @@
1
+ import {
2
+ GraphQLInputObjectType,
3
+ GraphQLList,
4
+ GraphQLNonNull,
5
+ GraphQLObjectType
6
+ } from "graphql";
7
+
8
+ import eosio_types from "./eosio_types.mjs";
9
+ import authorization_type from "./graphql_input_types/authorization.mjs";
10
+ import query_argument_fields from "./graphql_input_types/query_argument_fields.mjs";
11
+ import resolve from "./query_resolver.mjs";
12
+
13
+ /**
14
+ * Recursively resolves all the base fields and collects them into a single array.
15
+ * @param {String} base Base struct field name.
16
+ * @param {Array<Object>} structs List of ABI structs.
17
+ * @returns {Array<Object>} List of base fields.
18
+ */
19
+ function handleBaseFields(base, structs) {
20
+ const base_struct = structs.find(({ name }) => name == base);
21
+ const { fields, base: nested_base } = base_struct;
22
+ return [
23
+ ...(nested_base ? handleBaseFields(nested_base, structs) : []),
24
+ ...fields
25
+ ];
26
+ }
27
+
28
+ /**
29
+ * Performs some transformations on the ABI structs to make it malleable to the GraphQL spec.
30
+ * ABI structs are converted onto GraphQL Types.
31
+ * @param {Object} structs ABI structs
32
+ * @returns {Object} Struct AST that will be consumed by [eosio_abi_to_graphql_ast](./eosio_abi_to_graphql_ast.mjs).
33
+ */
34
+ function handleStructs(structs) {
35
+ let graphql_ast_structs = {};
36
+
37
+ for (const struct of structs) {
38
+ const { name, base, fields } = struct;
39
+ const fields_with_base_fields = base
40
+ ? [...handleBaseFields(base, structs), ...fields]
41
+ : fields;
42
+ let i = 0;
43
+ let ast_fields = new Array(fields_with_base_fields.length);
44
+ for (const field of fields_with_base_fields) {
45
+ const optional = !!field.type.match(/[$?]/gmu);
46
+ const binary_ex = !!field.type.match(/\$/gmu);
47
+ const variant = !!field.type.match(/@/gmu);
48
+ const list = !!field.type.match(/\[\]/gmu);
49
+ let type = field.type.replace(/[[\]?$@]/gmu, "");
50
+ const object = !eosio_types[type];
51
+ ast_fields[i] = {
52
+ name: field.name,
53
+ type,
54
+ $info: { object, optional, list, binary_ex, variant }
55
+ };
56
+ i++;
57
+ }
58
+ graphql_ast_structs[name] = ast_fields;
59
+ }
60
+ return graphql_ast_structs;
61
+ }
62
+
63
+ /**
64
+ * Generate an Abstract syntax tree (AST) for an EOSIO application Binary interface (ABI).
65
+ * @param {ABI} abi EOSIO smart contract Application Binary interface (ABI).
66
+ * @returns {Object} a GraphQL AST for a given smart contract.
67
+ */
68
+ export function eosio_abi_to_graphql_ast(abi) {
69
+ const { types, variants } = abi;
70
+
71
+ let structs = [
72
+ ...variants.map(({ name, types }) => ({
73
+ name,
74
+ base: "",
75
+ fields: types.map((item) => ({ name: item, type: item + "$@" })) // @ indiacted a variant type and binary extention.
76
+ })),
77
+ ...abi.structs
78
+ ];
79
+
80
+ if (types.length)
81
+ for (const { type: real_type, new_type_name } of types)
82
+ structs = structs.map(({ fields, ...struct }) => ({
83
+ ...struct,
84
+ fields: fields.map(({ name, type }) =>
85
+ type.match(new RegExp(`^${new_type_name}[?$]?([])?$`, "gmu"))
86
+ ? { name, type: type.replace(new_type_name, real_type) }
87
+ : { name, type }
88
+ )
89
+ }));
90
+
91
+ const structs_ast = handleStructs(structs);
92
+
93
+ return Object.freeze(structs_ast);
94
+ }
95
+
96
+ /**
97
+ * Wraps a GraphQL type in a GraphQLNonNullType and GraphQLListType.
98
+ * @param {Object} type GraphQL type to wrap.
99
+ * @param {Object} Arg Argument
100
+ * @param {Boolean} Arg.optional Wraps GraphQL type optional type.
101
+ * @param {Boolean} Arg.list Wraps GraphQL type in list type.
102
+ * @returns {Object} wrapped GraphQL type.
103
+ */
104
+ function Wrap(type, { optional, list }) {
105
+ let gql_type = type;
106
+ if (list) gql_type = new GraphQLList(gql_type);
107
+ if (!optional) gql_type = new GraphQLNonNull(gql_type);
108
+ return gql_type;
109
+ }
110
+
111
+ /**
112
+ * Generates GraphQL query and mutation fields from an ABI AST.
113
+ * @param {Object} AST Abstract syntax tree generated by `eosio_abi_to_graphql_ast` function.
114
+ * @param {Object} ABI EOSIO application binary interface (ABI).
115
+ * @param {String} [account_name] Blockchain account name.
116
+ * @returns {Object} GraphQL query and mutation fields.
117
+ */
118
+ export function get_graphql_fields_from_AST(AST, ABI, account_name = "") {
119
+ const { tables, actions } = ABI;
120
+ const gql_account_name = account_name.replace(/\./gmu, "_") + "_";
121
+
122
+ let query_fields = {};
123
+ const queryTypes = {};
124
+ const GQL_TYPES = {};
125
+
126
+ for (const table of tables) {
127
+ let { name: table_name, type: table_type } = table;
128
+ table_name = table_name.replace(/\./gmu, "_");
129
+ const table_fields = AST[table_type];
130
+
131
+ const buildQGL = (fields, acc = {}) => {
132
+ for (const field of fields) {
133
+ const { name, type, $info } = field;
134
+
135
+ // Do this because of variant type from table.
136
+ const resolve = (data, args, context, { fieldName }) => {
137
+ if ($info.variant) return type == data[0] ? data[1] : null;
138
+ return data[fieldName];
139
+ };
140
+
141
+ if ($info.object) {
142
+ if (!GQL_TYPES[type])
143
+ GQL_TYPES[type] = new GraphQLObjectType({
144
+ name: gql_account_name + type,
145
+ fields: buildQGL(AST[type])
146
+ });
147
+
148
+ acc = {
149
+ ...acc,
150
+ [name]: { type: Wrap(GQL_TYPES[type], $info), resolve }
151
+ };
152
+ } else
153
+ acc = {
154
+ ...acc,
155
+ [name]: { type: Wrap(eosio_types[type], $info), resolve }
156
+ };
157
+ }
158
+ return acc;
159
+ };
160
+
161
+ if (!queryTypes[table_type])
162
+ queryTypes[table_type] = {
163
+ type: new GraphQLList(
164
+ new GraphQLObjectType({
165
+ name: gql_account_name + table_type + "_query",
166
+ fields: buildQGL(table_fields)
167
+ })
168
+ ),
169
+ args: {
170
+ arg: {
171
+ name: "argument_type",
172
+ type: query_argument_fields
173
+ }
174
+ },
175
+ resolve
176
+ };
177
+
178
+ query_fields[table_name] = queryTypes[table_type];
179
+ }
180
+
181
+ const GQL_MTYPES = {};
182
+ let mutation_fields = {};
183
+ const mutationTypes = {};
184
+ for (const action of actions) {
185
+ let { name: action_name, type: action_type } = action;
186
+ action_name = action_name.replace(/\./gmu, "_");
187
+ const action_fields = AST[action_type];
188
+
189
+ const buildQGL = (fields, acc = {}) => {
190
+ for (const field of fields) {
191
+ const { name, type, $info } = field;
192
+ if ($info.object) {
193
+ if (!GQL_MTYPES[type])
194
+ GQL_MTYPES[type] = new GraphQLInputObjectType({
195
+ name: gql_account_name + "input_" + type,
196
+ fields: buildQGL(AST[type])
197
+ });
198
+ acc = { ...acc, [name]: { type: Wrap(GQL_MTYPES[type], $info) } };
199
+ } else
200
+ acc = { ...acc, [name]: { type: Wrap(eosio_types[type], $info) } };
201
+ }
202
+ return acc;
203
+ };
204
+
205
+ if (!mutationTypes[action_type])
206
+ mutationTypes[action_type] = {
207
+ type: new GraphQLInputObjectType({
208
+ name: gql_account_name + action_type,
209
+ fields: {
210
+ ...buildQGL(action_fields),
211
+ authorization: {
212
+ type: new GraphQLList(new GraphQLNonNull(authorization_type))
213
+ }
214
+ }
215
+ })
216
+ };
217
+
218
+ mutation_fields[action_name] = mutationTypes[action_type];
219
+ }
220
+
221
+ return { query_fields, mutation_fields };
222
+ }
@@ -0,0 +1,35 @@
1
+ import { GraphQLScalarType } from "graphql";
2
+
3
+ const asset_type = new GraphQLScalarType({
4
+ description: `
5
+ \`Asset type\`
6
+
7
+ ---
8
+
9
+ An \`asset\` type describes a blockchain asset and includes a quantity and a symbol section.
10
+
11
+
12
+ - The quantity must include decimal precision (with a maximum precision of 18)
13
+ - Symbol must be an uppercase string of 7 or less characters from [A-Z]
14
+
15
+ ***example*** - \`1.0010 EOS\`
16
+
17
+ - Quantity is 1.0010
18
+ - Symbol is EOS
19
+
20
+ `,
21
+ name: "asset",
22
+ parseValue(asset_string) {
23
+ if (asset_string == "") return "";
24
+
25
+ if (!asset_string.match(/^\d+(\.\d+)?\s[A-Z]{1,7}$/gmu))
26
+ throw new TypeError("Invalid asset type supplied.");
27
+
28
+ if (asset_string.replace(/[A-Z.\s]/gmu, "").length > 21)
29
+ throw new RangeError("Invalid asset size, maximum (2 ^ 62) - 1");
30
+
31
+ return asset_string;
32
+ }
33
+ });
34
+
35
+ export default asset_type;
@@ -0,0 +1,14 @@
1
+ import { GraphQLScalarType } from "graphql";
2
+
3
+ const block_timestamp_type = new GraphQLScalarType({
4
+ description: "`Block timestamp type`",
5
+ name: "block_timestamp_type",
6
+ parseValue(block_time_stamp) {
7
+ if (block_time_stamp == "") return "";
8
+ if (Number.isNaN(Date.parse(block_time_stamp)))
9
+ throw new TypeError("Invalid block timestamp " + block_time_stamp);
10
+ return block_time_stamp;
11
+ }
12
+ });
13
+
14
+ export default block_timestamp_type;
@@ -0,0 +1,10 @@
1
+ import { GraphQLScalarType } from "graphql";
2
+
3
+ const boolean_type = new GraphQLScalarType({
4
+ description: "`Boolean type` true=1 or false=0",
5
+ name: "bool",
6
+ parseValue: (boolean) => boolean,
7
+ serialize: (boolean) => (boolean ? true : false)
8
+ });
9
+
10
+ export default boolean_type;
@@ -0,0 +1,17 @@
1
+ import { GraphQLScalarType } from "graphql";
2
+
3
+ const bytes_type = new GraphQLScalarType({
4
+ description: "Hexedecimal text string type.",
5
+ name: "bytes",
6
+ parseValue(bytes) {
7
+ if (bytes == "") return bytes;
8
+ if (!bytes.match(/^[A-Fa-f0-9]+$/gmu))
9
+ throw new TypeError("Invald hexadecimal string: " + bytes);
10
+ if (bytes.length % 2 != 0)
11
+ throw new TypeError("Invalid Hexadecimal string length");
12
+
13
+ return bytes;
14
+ }
15
+ });
16
+
17
+ export default bytes_type;
@@ -0,0 +1,13 @@
1
+ import { GraphQLInt, GraphQLObjectType } from "graphql";
2
+
3
+ import public_key_type from "./public_key_type.mjs";
4
+
5
+ const EOSIO_key_type = new GraphQLObjectType({
6
+ name: "key_type",
7
+ fields: () => ({
8
+ key: { type: public_key_type },
9
+ weight: { type: GraphQLInt }
10
+ })
11
+ });
12
+
13
+ export default EOSIO_key_type;
@@ -0,0 +1,19 @@
1
+ import { GraphQLScalarType } from "graphql";
2
+
3
+ const extended_asset_type = new GraphQLScalarType({
4
+ description: `\`Extended_asset\`
5
+
6
+ Extended asset which stores the information of the owner of the asset.
7
+
8
+ ### extended asset
9
+ - Start with asset type followed by “@” and the account name.
10
+
11
+ example:
12
+
13
+ 1.0000 EOS@eosio.token
14
+ `,
15
+ name: "extended_asset",
16
+ parseValue: (extended_asset) => extended_asset
17
+ });
18
+
19
+ export default extended_asset_type;
@@ -0,0 +1,20 @@
1
+ import { GraphQLScalarType } from "graphql";
2
+
3
+ /**
4
+ * Generates a GraphQL scalar type for checksum of `size`.
5
+ * @name generate_int_type
6
+ * @param {number} size Size of the checksum.
7
+ * @returns {GraphQLScalarType} GraphQL scalar type.
8
+ */
9
+ function generate_checksum(size) {
10
+ return new GraphQLScalarType({
11
+ description: `\`Checksum${8 * size} type\`
12
+
13
+ Represented as a hexadecimal string of ${2 * size} characters.
14
+ `,
15
+ name: `checksum${size * 8}`,
16
+ parseValue: (checksum) => checksum
17
+ });
18
+ }
19
+
20
+ export default generate_checksum;
@@ -0,0 +1,16 @@
1
+ import { GraphQLScalarType } from "graphql";
2
+
3
+ /**
4
+ * Generates a GraphQL scalar Float type of `size`.
5
+ * @param {number} size Size of the float can be 32, 64 or 128.
6
+ * @returns {GraphQLScalarType} GraphQL scalar float type.
7
+ */
8
+ function generate_float_type(size) {
9
+ return new GraphQLScalarType({
10
+ description: `\`Float${size} type\``,
11
+ name: `float${size}`,
12
+ parseValue: (float) => float
13
+ });
14
+ }
15
+
16
+ export default generate_float_type;
@@ -0,0 +1,28 @@
1
+ import { GraphQLScalarType } from "graphql";
2
+
3
+ /**
4
+ * Generates a GraphQL scalar signed integer type of size `bytes`.
5
+ * @name generate_int_type
6
+ * @param {number} bits Size of the integer.
7
+ * @returns {GraphQLScalarType} GraphQL scalar integer type.
8
+ */
9
+ function generate_int_type(bits) {
10
+ bits = BigInt(bits);
11
+ const max = 2n ** (bits - 1n) - 1n;
12
+ const min = 2n ** (bits - 1n) * -1n;
13
+ return new GraphQLScalarType({
14
+ description: `\`Integer t${bits} type\`
15
+
16
+ Signed integer range is between ${min} - ${max}`,
17
+ name: `int${bits.toString()}`,
18
+ parseValue(int) {
19
+ if (int == "") return "";
20
+ int = BigInt(int);
21
+ if (int > max || int < min)
22
+ throw new RangeError(`Integer range is outside uint${bits}.`);
23
+ return int.toString();
24
+ }
25
+ });
26
+ }
27
+
28
+ export default generate_int_type;
@@ -0,0 +1,27 @@
1
+ import { GraphQLScalarType } from "graphql";
2
+ /**
3
+ * Generates a GraphQL scalar unsigend integer type of size `bits`.
4
+ * @param {Number} bits Size of the unint.
5
+ * @returns {GraphQLScalarType} GraphQL scalar integer type.
6
+ */
7
+ function generate_uint_type(bits) {
8
+ const size = 2n ** BigInt(bits);
9
+ return new GraphQLScalarType({
10
+ description: `\`Unsigned integer${bits.toString()} type\`
11
+
12
+ Unsigned integer range is between 0 - ${size.toString()}`,
13
+ name: `uint${bits}`,
14
+ parseValue(uint) {
15
+ if (uint === "") return "";
16
+
17
+ uint = BigInt(uint);
18
+ if (uint >= size || uint < 0n)
19
+ throw new RangeError(
20
+ `Integer range is outside uint${bits.toString()}.`
21
+ );
22
+ return uint.toString();
23
+ }
24
+ });
25
+ }
26
+
27
+ export default generate_uint_type;
@@ -0,0 +1,29 @@
1
+ import { GraphQLScalarType } from "graphql";
2
+
3
+ const name_type = new GraphQLScalarType({
4
+ description: `\`Name type\`
5
+
6
+ Names are unique identifiers on the blockchain.
7
+
8
+ ---
9
+
10
+ ### name rules
11
+
12
+ - Combination of lowercase characters
13
+ - Can include numbers 1 - 5 and period “.” character
14
+ - Must NOT end with a period “.”
15
+ - Must NOT be longer than 12 characters
16
+
17
+ `,
18
+ name: "name",
19
+ parseValue(_name) {
20
+ if (_name == "") return _name;
21
+
22
+ if (!_name.match(/^[1-5a-z]{1}[1-5a-z.]{0,10}[1-5a-z]{1}$/gmu))
23
+ throw new TypeError(`Invalid name “${_name}”.`);
24
+
25
+ return _name;
26
+ }
27
+ });
28
+
29
+ export default name_type;
@@ -0,0 +1,26 @@
1
+ import legacy_to_public_key from "eosio-ecc/legacy_to_public_key.mjs";
2
+ import validate_public_key from "eosio-ecc/validate_public_key.mjs";
3
+ import { GraphQLScalarType } from "graphql";
4
+
5
+ const public_key_type = new GraphQLScalarType({
6
+ description: `\`Public key type\`
7
+ ---
8
+
9
+ Public keys should begin with PUB_K1 (or EOS for legacy keys) and include base58 characters only.
10
+ `,
11
+ name: "public_key",
12
+ async serialize(legacy_key) {
13
+ if (!legacy_key.startsWith("EOS")) return legacy_key;
14
+
15
+ return legacy_to_public_key(legacy_key);
16
+ },
17
+ async parseValue(public_key) {
18
+ if (public_key == "") return "";
19
+
20
+ const { valid, message } = await validate_public_key(public_key);
21
+ if (!valid) throw new RangeError(message);
22
+ return public_key;
23
+ }
24
+ });
25
+
26
+ export default public_key_type;
@@ -0,0 +1,19 @@
1
+ import { GraphQLScalarType } from "graphql";
2
+
3
+ const signature_type = new GraphQLScalarType({
4
+ description: `\`Signature type\`
5
+
6
+ EOS K1 signature supported.`,
7
+ name: "signature",
8
+ parseValue(signature) {
9
+ if (signature == "") return "";
10
+ if (typeof signature !== "string")
11
+ throw new TypeError("Expected signature to be string");
12
+
13
+ if (signature.slice(0, 7) != "SIG_K1_")
14
+ throw new TypeError("Signature prefix is should be SIG_K1_.");
15
+ return signature;
16
+ }
17
+ });
18
+
19
+ export default signature_type;
@@ -0,0 +1,13 @@
1
+ import { GraphQLScalarType } from "graphql";
2
+
3
+ const symbol_code_type = new GraphQLScalarType({
4
+ description: `\`Symbol code type\``,
5
+ name: "symbol_code",
6
+ parseValue: (symbol_code) => {
7
+ if (!symbol_code.match(/[A-Z]{1,7}/gmu))
8
+ throw new Error("Invalid symbol code.");
9
+ return symbol_code;
10
+ }
11
+ });
12
+
13
+ export default symbol_code_type;