@nucypher/taco 0.1.0-rc.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 (137) hide show
  1. package/LICENSE +675 -0
  2. package/README.md +3 -0
  3. package/dist/cjs/conditions/base/contract.d.ts +428 -0
  4. package/dist/cjs/conditions/base/contract.js +76 -0
  5. package/dist/cjs/conditions/base/contract.js.map +1 -0
  6. package/dist/cjs/conditions/base/index.d.ts +21 -0
  7. package/dist/cjs/conditions/base/index.js +42 -0
  8. package/dist/cjs/conditions/base/index.js.map +1 -0
  9. package/dist/cjs/conditions/base/rpc.d.ts +42 -0
  10. package/dist/cjs/conditions/base/rpc.js +23 -0
  11. package/dist/cjs/conditions/base/rpc.js.map +1 -0
  12. package/dist/cjs/conditions/base/shared.d.ts +16 -0
  13. package/dist/cjs/conditions/base/shared.js +17 -0
  14. package/dist/cjs/conditions/base/shared.js.map +1 -0
  15. package/dist/cjs/conditions/base/time.d.ts +40 -0
  16. package/dist/cjs/conditions/base/time.js +16 -0
  17. package/dist/cjs/conditions/base/time.js.map +1 -0
  18. package/dist/cjs/conditions/compound-condition.d.ts +4 -0
  19. package/dist/cjs/conditions/compound-condition.js +23 -0
  20. package/dist/cjs/conditions/compound-condition.js.map +1 -0
  21. package/dist/cjs/conditions/condition-expr.d.ts +23 -0
  22. package/dist/cjs/conditions/condition-expr.js +62 -0
  23. package/dist/cjs/conditions/condition-expr.js.map +1 -0
  24. package/dist/cjs/conditions/condition.d.ts +17 -0
  25. package/dist/cjs/conditions/condition.js +61 -0
  26. package/dist/cjs/conditions/condition.js.map +1 -0
  27. package/dist/cjs/conditions/const.d.ts +4 -0
  28. package/dist/cjs/conditions/const.js +13 -0
  29. package/dist/cjs/conditions/const.js.map +1 -0
  30. package/dist/cjs/conditions/context/context.d.ts +24 -0
  31. package/dist/cjs/conditions/context/context.js +113 -0
  32. package/dist/cjs/conditions/context/context.js.map +1 -0
  33. package/dist/cjs/conditions/context/index.d.ts +1 -0
  34. package/dist/cjs/conditions/context/index.js +6 -0
  35. package/dist/cjs/conditions/context/index.js.map +1 -0
  36. package/dist/cjs/conditions/context/providers.d.ts +16 -0
  37. package/dist/cjs/conditions/context/providers.js +109 -0
  38. package/dist/cjs/conditions/context/providers.js.map +1 -0
  39. package/dist/cjs/conditions/index.d.ts +9 -0
  40. package/dist/cjs/conditions/index.js +44 -0
  41. package/dist/cjs/conditions/index.js.map +1 -0
  42. package/dist/cjs/conditions/predefined/erc721.d.ts +10 -0
  43. package/dist/cjs/conditions/predefined/erc721.js +39 -0
  44. package/dist/cjs/conditions/predefined/erc721.js.map +1 -0
  45. package/dist/cjs/conditions/predefined/index.d.ts +1 -0
  46. package/dist/cjs/conditions/predefined/index.js +7 -0
  47. package/dist/cjs/conditions/predefined/index.js.map +1 -0
  48. package/dist/cjs/conditions/zod.d.ts +3 -0
  49. package/dist/cjs/conditions/zod.js +19 -0
  50. package/dist/cjs/conditions/zod.js.map +1 -0
  51. package/dist/cjs/dkg.d.ts +27 -0
  52. package/dist/cjs/dkg.js +85 -0
  53. package/dist/cjs/dkg.js.map +1 -0
  54. package/dist/cjs/index.d.ts +4 -0
  55. package/dist/cjs/index.js +39 -0
  56. package/dist/cjs/index.js.map +1 -0
  57. package/dist/cjs/porter.d.ts +4 -0
  58. package/dist/cjs/porter.js +22 -0
  59. package/dist/cjs/porter.js.map +1 -0
  60. package/dist/cjs/taco.d.ts +7 -0
  61. package/dist/cjs/taco.js +47 -0
  62. package/dist/cjs/taco.js.map +1 -0
  63. package/dist/cjs/tdec.d.ts +5 -0
  64. package/dist/cjs/tdec.js +61 -0
  65. package/dist/cjs/tdec.js.map +1 -0
  66. package/dist/cjs/web3.d.ts +33 -0
  67. package/dist/cjs/web3.js +3 -0
  68. package/dist/cjs/web3.js.map +1 -0
  69. package/dist/es/conditions/base/contract.d.ts +428 -0
  70. package/dist/es/conditions/base/contract.js +73 -0
  71. package/dist/es/conditions/base/contract.js.map +1 -0
  72. package/dist/es/conditions/base/index.d.ts +21 -0
  73. package/dist/es/conditions/base/index.js +31 -0
  74. package/dist/es/conditions/base/index.js.map +1 -0
  75. package/dist/es/conditions/base/rpc.d.ts +42 -0
  76. package/dist/es/conditions/base/rpc.js +17 -0
  77. package/dist/es/conditions/base/rpc.js.map +1 -0
  78. package/dist/es/conditions/base/shared.d.ts +16 -0
  79. package/dist/es/conditions/base/shared.js +14 -0
  80. package/dist/es/conditions/base/shared.js.map +1 -0
  81. package/dist/es/conditions/base/time.d.ts +40 -0
  82. package/dist/es/conditions/base/time.js +13 -0
  83. package/dist/es/conditions/base/time.js.map +1 -0
  84. package/dist/es/conditions/compound-condition.d.ts +4 -0
  85. package/dist/es/conditions/compound-condition.js +20 -0
  86. package/dist/es/conditions/compound-condition.js.map +1 -0
  87. package/dist/es/conditions/condition-expr.d.ts +23 -0
  88. package/dist/es/conditions/condition-expr.js +58 -0
  89. package/dist/es/conditions/condition-expr.js.map +1 -0
  90. package/dist/es/conditions/condition.d.ts +17 -0
  91. package/dist/es/conditions/condition.js +57 -0
  92. package/dist/es/conditions/condition.js.map +1 -0
  93. package/dist/es/conditions/const.d.ts +4 -0
  94. package/dist/es/conditions/const.js +10 -0
  95. package/dist/es/conditions/const.js.map +1 -0
  96. package/dist/es/conditions/context/context.d.ts +24 -0
  97. package/dist/es/conditions/context/context.js +109 -0
  98. package/dist/es/conditions/context/context.js.map +1 -0
  99. package/dist/es/conditions/context/index.d.ts +1 -0
  100. package/dist/es/conditions/context/index.js +2 -0
  101. package/dist/es/conditions/context/index.js.map +1 -0
  102. package/dist/es/conditions/context/providers.d.ts +16 -0
  103. package/dist/es/conditions/context/providers.js +105 -0
  104. package/dist/es/conditions/context/providers.js.map +1 -0
  105. package/dist/es/conditions/index.d.ts +9 -0
  106. package/dist/es/conditions/index.js +10 -0
  107. package/dist/es/conditions/index.js.map +1 -0
  108. package/dist/es/conditions/predefined/erc721.d.ts +10 -0
  109. package/dist/es/conditions/predefined/erc721.js +34 -0
  110. package/dist/es/conditions/predefined/erc721.js.map +1 -0
  111. package/dist/es/conditions/predefined/index.d.ts +1 -0
  112. package/dist/es/conditions/predefined/index.js +2 -0
  113. package/dist/es/conditions/predefined/index.js.map +1 -0
  114. package/dist/es/conditions/zod.d.ts +3 -0
  115. package/dist/es/conditions/zod.js +17 -0
  116. package/dist/es/conditions/zod.js.map +1 -0
  117. package/dist/es/dkg.d.ts +27 -0
  118. package/dist/es/dkg.js +80 -0
  119. package/dist/es/dkg.js.map +1 -0
  120. package/dist/es/index.d.ts +4 -0
  121. package/dist/es/index.js +5 -0
  122. package/dist/es/index.js.map +1 -0
  123. package/dist/es/porter.d.ts +4 -0
  124. package/dist/es/porter.js +14 -0
  125. package/dist/es/porter.js.map +1 -0
  126. package/dist/es/taco.d.ts +7 -0
  127. package/dist/es/taco.js +40 -0
  128. package/dist/es/taco.js.map +1 -0
  129. package/dist/es/tdec.d.ts +5 -0
  130. package/dist/es/tdec.js +56 -0
  131. package/dist/es/tdec.js.map +1 -0
  132. package/dist/es/web3.d.ts +33 -0
  133. package/dist/es/web3.js +2 -0
  134. package/dist/es/web3.js.map +1 -0
  135. package/dist/tsconfig.cjs.tsbuildinfo +1 -0
  136. package/dist/tsconfig.es.tsbuildinfo +1 -0
  137. package/package.json +58 -0
@@ -0,0 +1,40 @@
1
+ import { z } from 'zod';
2
+ export declare const TimeConditionType = "time";
3
+ export declare const TimeConditionMethod = "blocktime";
4
+ export declare const timeConditionSchema: z.ZodObject<{
5
+ conditionType: z.ZodDefault<z.ZodLiteral<"time">>;
6
+ method: z.ZodDefault<z.ZodLiteral<"blocktime">>;
7
+ chain: z.ZodNever | z.ZodLiteral<z.Primitive> | z.ZodUnion<[z.ZodLiteral<z.Primitive>, z.ZodLiteral<z.Primitive>, ...z.ZodLiteral<z.Primitive>[]]>;
8
+ returnValueTest: z.ZodObject<{
9
+ index: z.ZodOptional<z.ZodNumber>;
10
+ comparator: z.ZodEnum<["==", ">", "<", ">=", "<=", "!="]>;
11
+ value: z.ZodUnknown;
12
+ }, "strip", z.ZodTypeAny, {
13
+ comparator: "==" | ">" | "<" | ">=" | "<=" | "!=";
14
+ index?: number | undefined;
15
+ value?: unknown;
16
+ }, {
17
+ comparator: "==" | ">" | "<" | ">=" | "<=" | "!=";
18
+ index?: number | undefined;
19
+ value?: unknown;
20
+ }>;
21
+ }, "strip", z.ZodTypeAny, {
22
+ conditionType: "time";
23
+ method: "blocktime";
24
+ returnValueTest: {
25
+ comparator: "==" | ">" | "<" | ">=" | "<=" | "!=";
26
+ index?: number | undefined;
27
+ value?: unknown;
28
+ };
29
+ chain?: z.Primitive;
30
+ }, {
31
+ returnValueTest: {
32
+ comparator: "==" | ">" | "<" | ">=" | "<=" | "!=";
33
+ index?: number | undefined;
34
+ value?: unknown;
35
+ };
36
+ conditionType?: "time" | undefined;
37
+ method?: "blocktime" | undefined;
38
+ chain?: z.Primitive;
39
+ }>;
40
+ export type TimeConditionProps = z.infer<typeof timeConditionSchema>;
@@ -0,0 +1,13 @@
1
+ import { z } from 'zod';
2
+ import { rpcConditionSchema } from './rpc';
3
+ // TimeCondition is an RpcCondition with the method set to 'blocktime' and no parameters
4
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
5
+ const { parameters: _, ...restShape } = rpcConditionSchema.shape;
6
+ export const TimeConditionType = 'time';
7
+ export const TimeConditionMethod = 'blocktime';
8
+ export const timeConditionSchema = z.object({
9
+ ...restShape,
10
+ conditionType: z.literal(TimeConditionType).default(TimeConditionType),
11
+ method: z.literal(TimeConditionMethod).default(TimeConditionMethod),
12
+ });
13
+ //# sourceMappingURL=time.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"time.js","sourceRoot":"","sources":["../../../../src/conditions/base/time.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,kBAAkB,EAAE,MAAM,OAAO,CAAC;AAE3C,wFAAwF;AACxF,6DAA6D;AAC7D,MAAM,EAAE,UAAU,EAAE,CAAC,EAAE,GAAG,SAAS,EAAE,GAAG,kBAAkB,CAAC,KAAK,CAAC;AAEjE,MAAM,CAAC,MAAM,iBAAiB,GAAG,MAAM,CAAC;AACxC,MAAM,CAAC,MAAM,mBAAmB,GAAG,WAAW,CAAC;AAE/C,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,CAAC,MAAM,CAAC;IAC1C,GAAG,SAAS;IACZ,aAAa,EAAE,CAAC,CAAC,OAAO,CAAC,iBAAiB,CAAC,CAAC,OAAO,CAAC,iBAAiB,CAAC;IACtE,MAAM,EAAE,CAAC,CAAC,OAAO,CAAC,mBAAmB,CAAC,CAAC,OAAO,CAAC,mBAAmB,CAAC;CACpE,CAAC,CAAC"}
@@ -0,0 +1,4 @@
1
+ import { z } from 'zod';
2
+ export declare const CompoundConditionType = "compound";
3
+ export declare const compoundConditionSchema: z.ZodSchema;
4
+ export type CompoundConditionProps = z.infer<typeof compoundConditionSchema>;
@@ -0,0 +1,20 @@
1
+ import { z } from 'zod';
2
+ import { contractConditionSchema } from './base/contract';
3
+ import { rpcConditionSchema } from './base/rpc';
4
+ import { timeConditionSchema } from './base/time';
5
+ export const CompoundConditionType = 'compound';
6
+ export const compoundConditionSchema = z.object({
7
+ conditionType: z
8
+ .literal(CompoundConditionType)
9
+ .default(CompoundConditionType),
10
+ operator: z.enum(['and', 'or']),
11
+ operands: z
12
+ .array(z.lazy(() => z.union([
13
+ rpcConditionSchema,
14
+ timeConditionSchema,
15
+ contractConditionSchema,
16
+ compoundConditionSchema,
17
+ ])))
18
+ .min(2),
19
+ });
20
+ //# sourceMappingURL=compound-condition.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"compound-condition.js","sourceRoot":"","sources":["../../../src/conditions/compound-condition.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAC;AAExB,OAAO,EAAE,uBAAuB,EAAE,MAAM,iBAAiB,CAAC;AAC1D,OAAO,EAAE,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAChD,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAElD,MAAM,CAAC,MAAM,qBAAqB,GAAG,UAAU,CAAC;AAEhD,MAAM,CAAC,MAAM,uBAAuB,GAAgB,CAAC,CAAC,MAAM,CAAC;IAC3D,aAAa,EAAE,CAAC;SACb,OAAO,CAAC,qBAAqB,CAAC;SAC9B,OAAO,CAAC,qBAAqB,CAAC;IACjC,QAAQ,EAAE,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,IAAI,CAAC,CAAC;IAC/B,QAAQ,EAAE,CAAC;SACR,KAAK,CACJ,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CACV,CAAC,CAAC,KAAK,CAAC;QACN,kBAAkB;QAClB,mBAAmB;QACnB,uBAAuB;QACvB,uBAAuB;KACxB,CAAC,CACH,CACF;SACA,GAAG,CAAC,CAAC,CAAC;CACV,CAAC,CAAC"}
@@ -0,0 +1,23 @@
1
+ import { Conditions as WASMConditions } from '@nucypher/nucypher-core';
2
+ import { ethers } from 'ethers';
3
+ import { Condition } from './condition';
4
+ import { ConditionContext, CustomContextParam } from './context';
5
+ export type ConditionExpressionJSON = {
6
+ version: string;
7
+ condition: Record<string, unknown>;
8
+ };
9
+ export declare class ConditionExpression {
10
+ readonly condition: Condition;
11
+ readonly version: string;
12
+ static version: string;
13
+ constructor(condition: Condition, version?: string);
14
+ toObj(): ConditionExpressionJSON;
15
+ static fromObj(obj: ConditionExpressionJSON): ConditionExpression;
16
+ toJson(): string;
17
+ static fromJSON(json: string): ConditionExpression;
18
+ toWASMConditions(): WASMConditions;
19
+ static fromWASMConditions(conditions: WASMConditions): ConditionExpression;
20
+ buildContext(provider: ethers.providers.Provider, customParameters?: Record<string, CustomContextParam>, signer?: ethers.Signer): ConditionContext;
21
+ contextRequiresSigner(): boolean;
22
+ equals(other: ConditionExpression): boolean;
23
+ }
@@ -0,0 +1,58 @@
1
+ import { Conditions as WASMConditions } from '@nucypher/nucypher-core';
2
+ import { toJSON } from '@nucypher/shared';
3
+ import { SemVer } from 'semver';
4
+ import { Condition } from './condition';
5
+ import { ConditionContext } from './context';
6
+ export class ConditionExpression {
7
+ condition;
8
+ version;
9
+ static version = '1.0.0';
10
+ constructor(condition, version = ConditionExpression.version) {
11
+ this.condition = condition;
12
+ this.version = version;
13
+ }
14
+ toObj() {
15
+ const condition = this.condition.toObj();
16
+ return {
17
+ version: this.version,
18
+ condition,
19
+ };
20
+ }
21
+ static fromObj(obj) {
22
+ const receivedVersion = new SemVer(obj.version);
23
+ const currentVersion = new SemVer(ConditionExpression.version);
24
+ if (receivedVersion.major > currentVersion.major) {
25
+ throw new Error(`Version provided, ${obj.version}, is incompatible with current version, ${ConditionExpression.version}`);
26
+ }
27
+ if (!obj.condition) {
28
+ throw new Error(`Invalid condition: unrecognized condition data ${JSON.stringify(obj.condition)}`);
29
+ }
30
+ const condition = Condition.fromObj(obj.condition);
31
+ return new ConditionExpression(condition, obj.version);
32
+ }
33
+ toJson() {
34
+ return toJSON(this.toObj());
35
+ }
36
+ static fromJSON(json) {
37
+ return ConditionExpression.fromObj(JSON.parse(json));
38
+ }
39
+ toWASMConditions() {
40
+ return new WASMConditions(toJSON(this.toObj()));
41
+ }
42
+ static fromWASMConditions(conditions) {
43
+ return ConditionExpression.fromJSON(conditions.toString());
44
+ }
45
+ buildContext(provider, customParameters = {}, signer) {
46
+ return new ConditionContext(provider, [this.condition], customParameters, signer);
47
+ }
48
+ contextRequiresSigner() {
49
+ return this.condition.requiresSigner();
50
+ }
51
+ equals(other) {
52
+ return [
53
+ this.version === other.version,
54
+ this.condition.equals(other.condition),
55
+ ].every(Boolean);
56
+ }
57
+ }
58
+ //# sourceMappingURL=condition-expr.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"condition-expr.js","sourceRoot":"","sources":["../../../src/conditions/condition-expr.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,IAAI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AG,CAAC,OAAO,CAAC,CAAC;IACzD,CAAC;IAEM,MAAM;QACX,OAAO,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC;IAC9B,CAAC;IAEM,MAAM,CAAC,QAAQ,CAAC,IAAY;QACjC,OAAO,mBAAmB,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC;IACvD,CAAC;IAEM,gBAAgB;QACrB,OAAO,IAAI,cAAc,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;IAClD,CAAC;IAEM,MAAM,CAAC,kBAAkB,CAAC,UAA0B;QACzD,OAAO,mBAAmB,CAAC,QAAQ,CAAC,UAAU,CAAC,QAAQ,EAAE,CAAC,CAAC;IAC7D,CAAC;IAEM,YAAY,CACjB,QAAmC,EACnC,mBAAuD,EAAE,EACzD,MAAsB;QAEtB,OAAO,IAAI,gBAAgB,CACzB,QAAQ,EACR,CAAC,IAAI,CAAC,SAAS,CAAC,EAChB,gBAAgB,EAChB,MAAM,CACP,CAAC;IACJ,CAAC;IAEM,qBAAqB;QAC1B,OAAO,IAAI,CAAC,SAAS,CAAC,cAAc,EAAE,CAAC;IACzC,CAAC;IAEM,MAAM,CAAC,KAA0B;QACtC,OAAO;YACL,IAAI,CAAC,OAAO,KAAK,KAAK,CAAC,OAAO;YAC9B,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,KAAK,CAAC,SAAS,CAAC;SACvC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IACnB,CAAC"}
@@ -0,0 +1,17 @@
1
+ import { z } from 'zod';
2
+ type ConditionSchema = z.ZodSchema;
3
+ export type ConditionProps = z.infer<ConditionSchema>;
4
+ export declare class Condition {
5
+ readonly schema: ConditionSchema;
6
+ readonly value: ConditionProps;
7
+ constructor(schema: ConditionSchema, value: ConditionProps);
8
+ static validate(schema: ConditionSchema, value: ConditionProps): {
9
+ data?: ConditionProps;
10
+ error?: z.ZodError;
11
+ };
12
+ requiresSigner(): boolean;
13
+ toObj(): any;
14
+ static fromObj(obj: ConditionProps): Condition;
15
+ equals(other: Condition): boolean;
16
+ }
17
+ export {};
@@ -0,0 +1,57 @@
1
+ import { objectEquals } from '@nucypher/shared';
2
+ import { CompoundCondition, ContractCondition, ContractConditionType, RpcCondition, RpcConditionType, TimeCondition, TimeConditionType, } from './base';
3
+ import { CompoundConditionType, } from './compound-condition';
4
+ import { USER_ADDRESS_PARAM } from './const';
5
+ class ConditionFactory {
6
+ static conditionFromProps(obj) {
7
+ switch (obj.conditionType) {
8
+ case RpcConditionType:
9
+ return new RpcCondition(obj);
10
+ case TimeConditionType:
11
+ return new TimeCondition(obj);
12
+ case ContractConditionType:
13
+ return new ContractCondition(obj);
14
+ case CompoundConditionType:
15
+ return new CompoundCondition(obj);
16
+ default:
17
+ throw new Error(`Invalid conditionType: ${obj.conditionType}`);
18
+ }
19
+ }
20
+ }
21
+ export class Condition {
22
+ schema;
23
+ value;
24
+ constructor(schema, value) {
25
+ this.schema = schema;
26
+ this.value = value;
27
+ const { data, error } = Condition.validate(schema, value);
28
+ if (error) {
29
+ throw new Error(`Invalid condition: ${JSON.stringify(error.issues)}`);
30
+ }
31
+ this.value = data;
32
+ }
33
+ static validate(schema, value) {
34
+ const result = schema.safeParse(value);
35
+ if (result.success) {
36
+ return { data: result.data };
37
+ }
38
+ return { error: result.error };
39
+ }
40
+ requiresSigner() {
41
+ return JSON.stringify(this.value).includes(USER_ADDRESS_PARAM);
42
+ }
43
+ toObj() {
44
+ const { data, error } = Condition.validate(this.schema, this.value);
45
+ if (error) {
46
+ throw new Error(`Invalid condition: ${JSON.stringify(error.issues)}`);
47
+ }
48
+ return data;
49
+ }
50
+ static fromObj(obj) {
51
+ return ConditionFactory.conditionFromProps(obj);
52
+ }
53
+ equals(other) {
54
+ return objectEquals(this.toObj(), other.toObj());
55
+ }
56
+ }
57
+ //# sourceMappingURL=condition.js.map
@@ -0,0 +1 @@
1
+ {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KAAK,GAAG,IAAI,CAAC;IACpB,CAAC;IAEM,MAAM,CAAC,QAAQ,CACpB,MAAuB,EACvB,KAAqB;QAKrB,MAAM,MAAM,GAAG,MAAM,CAAC,SAAS,CAAC,KAAK,CAAC,CAAC;QACvC,IAAI,MAAM,CAAC,OAAO,EAAE;YAClB,OAAO,EAAE,IAAI,EAAE,MAAM,CAAC,IAAI,EAAE,CAAC;SAC9B;QACD,OAAO,EAAE,KAAK,EAAE,MAAM,CAAC,KAAK,EAAE,CAAC;IACjC,CAAC;IAEM,cAAc;QACnB,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,QAAQ,CAAC,kBAAkB,CAAC,CAAC;IACjE,CAAC;IAEM,KAAK;QACV,MAAM,EAAE,IAAI,EAAE,KAAK,EAAE,GAAG,SAAS,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,EAAE,IAAI,CAAC,KAAK,CAAC,CAAC;QACpE,IAAI,KAAK,EAAE;YACT,MAAM,IAAI,KAAK,CAAC,sBAAsB,IAAI,CAAC,SAAS,CAAC,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC;SACvE;QACD,OAAO,IAAI,CAAC;IACd,CAAC;IAEM,MAAM,CAAC,OAAO,CAAC,GAAmB;QACvC,OAAO,gBAAgB,CAAC,kBAAkB,CAAC,GAAG,CAAC,CAAC;IAClD,CAAC;IAEM,MAAM,CAAC,KAAgB;QAC5B,OAAO,YAAY,CAAC,IAAI,CAAC,KAAK,EAAE,EAAE,KAAK,CAAC,KAAK,EAAE,CAAC,CAAC;IACnD,CAAC;CACF"}
@@ -0,0 +1,4 @@
1
+ import { ChainId } from '@nucypher/shared';
2
+ export declare const USER_ADDRESS_PARAM = ":userAddress";
3
+ export declare const ETH_ADDRESS_REGEXP: RegExp;
4
+ export declare const SUPPORTED_CHAIN_IDS: ChainId[];
@@ -0,0 +1,10 @@
1
+ import { ChainId } from '@nucypher/shared';
2
+ export const USER_ADDRESS_PARAM = ':userAddress';
3
+ export const ETH_ADDRESS_REGEXP = new RegExp('^0x[a-fA-F0-9]{40}$');
4
+ export const SUPPORTED_CHAIN_IDS = [
5
+ ChainId.POLYGON,
6
+ ChainId.MUMBAI,
7
+ ChainId.GOERLI,
8
+ ChainId.MAINNET,
9
+ ];
10
+ //# sourceMappingURL=const.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"const.js","sourceRoot":"","sources":["../../../src/conditions/const.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,kBAAkB,CAAC;AAE3C,MAAM,CAAC,MAAM,kBAAkB,GAAG,cAAc,CAAC;AAEjD,MAAM,CAAC,MAAM,kBAAkB,GAAG,IAAI,MAAM,CAAC,qBAAqB,CAAC,CAAC;AAEpE,MAAM,CAAC,MAAM,mBAAmB,GAAG;IACjC,OAAO,CAAC,OAAO;IACf,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,MAAM;IACd,OAAO,CAAC,OAAO;CAChB,CAAC"}
@@ -0,0 +1,24 @@
1
+ import { Context, Conditions as WASMConditions } from '@nucypher/nucypher-core';
2
+ import { ethers } from 'ethers';
3
+ import { Condition } from '../condition';
4
+ import { TypedSignature } from './providers';
5
+ export type CustomContextParam = string | number | boolean;
6
+ export type ContextParam = CustomContextParam | TypedSignature;
7
+ export declare const RESERVED_CONTEXT_PARAMS: string[];
8
+ export declare const CONTEXT_PARAM_PREFIX = ":";
9
+ export declare class ConditionContext {
10
+ private readonly provider;
11
+ private readonly conditions;
12
+ readonly customParameters: Record<string, CustomContextParam>;
13
+ private readonly signer?;
14
+ private readonly walletAuthProvider?;
15
+ constructor(provider: ethers.providers.Provider, conditions: ReadonlyArray<Condition>, customParameters?: Record<string, CustomContextParam>, signer?: ethers.Signer | undefined);
16
+ private validate;
17
+ toObj: () => Promise<Record<string, ContextParam>>;
18
+ private fillContextParameters;
19
+ private findRequestedParameters;
20
+ toJson(): Promise<string>;
21
+ withCustomParams(params: Record<string, CustomContextParam>): ConditionContext;
22
+ toWASMContext(): Promise<Context>;
23
+ static fromConditions(provider: ethers.providers.Provider, conditions: WASMConditions, signer?: ethers.Signer): ConditionContext;
24
+ }
@@ -0,0 +1,109 @@
1
+ import { Context, Conditions as WASMConditions } from '@nucypher/nucypher-core';
2
+ import { fromJSON, toJSON } from '@nucypher/shared';
3
+ import { ConditionExpression } from '../condition-expr';
4
+ import { USER_ADDRESS_PARAM } from '../const';
5
+ import { WalletAuthenticationProvider } from './providers';
6
+ export const RESERVED_CONTEXT_PARAMS = [USER_ADDRESS_PARAM];
7
+ export const CONTEXT_PARAM_PREFIX = ':';
8
+ export class ConditionContext {
9
+ provider;
10
+ conditions;
11
+ customParameters;
12
+ signer;
13
+ walletAuthProvider;
14
+ constructor(provider, conditions, customParameters = {}, signer) {
15
+ this.provider = provider;
16
+ this.conditions = conditions;
17
+ this.customParameters = customParameters;
18
+ this.signer = signer;
19
+ if (this.signer) {
20
+ this.walletAuthProvider = new WalletAuthenticationProvider(this.provider, this.signer);
21
+ }
22
+ this.validate();
23
+ }
24
+ validate() {
25
+ Object.keys(this.customParameters).forEach((key) => {
26
+ if (RESERVED_CONTEXT_PARAMS.includes(key)) {
27
+ throw new Error(`Cannot use reserved parameter name ${key} as custom parameter`);
28
+ }
29
+ if (!key.startsWith(CONTEXT_PARAM_PREFIX)) {
30
+ throw new Error(`Custom parameter ${key} must start with ${CONTEXT_PARAM_PREFIX}`);
31
+ }
32
+ });
33
+ const conditionRequiresSigner = this.conditions.some((c) => c.requiresSigner());
34
+ if (conditionRequiresSigner && !this.signer) {
35
+ throw new Error(`Signer required to satisfy ${USER_ADDRESS_PARAM} context variable in condition`);
36
+ }
37
+ return this;
38
+ }
39
+ toObj = async () => {
40
+ const requestedParameters = this.findRequestedParameters();
41
+ const parameters = await this.fillContextParameters(requestedParameters);
42
+ // Ok, so at this point we should have all the parameters we need
43
+ // If we don't, we have a problem and we should throw
44
+ const missingParameters = Array.from(requestedParameters).filter((key) => !parameters[key]);
45
+ if (missingParameters.length > 0) {
46
+ throw new Error(`Missing custom context parameter(s): ${missingParameters.join(', ')}`);
47
+ }
48
+ return parameters;
49
+ };
50
+ async fillContextParameters(requestedParameters) {
51
+ // Now, we can safely add all the parameters
52
+ const parameters = {};
53
+ // Fill in predefined context parameters
54
+ if (requestedParameters.has(USER_ADDRESS_PARAM)) {
55
+ if (!this.walletAuthProvider) {
56
+ throw new Error(`Condition contains ${USER_ADDRESS_PARAM} context variable and requires a signer to populate`);
57
+ }
58
+ parameters[USER_ADDRESS_PARAM] =
59
+ await this.walletAuthProvider.getOrCreateWalletSignature();
60
+ // Remove from requested parameters
61
+ requestedParameters.delete(USER_ADDRESS_PARAM);
62
+ }
63
+ // Fill in custom parameters
64
+ for (const key in this.customParameters) {
65
+ parameters[key] = this.customParameters[key];
66
+ }
67
+ return parameters;
68
+ }
69
+ findRequestedParameters() {
70
+ // First, we want to find all the parameters we need to add
71
+ const requestedParameters = new Set();
72
+ // Search conditions for parameters
73
+ const conditions = this.conditions.map((cnd) => cnd.toObj());
74
+ const conditionsToCheck = fromJSON(new WASMConditions(toJSON(conditions)).toString());
75
+ for (const cond of conditionsToCheck) {
76
+ // Check return value test
77
+ const rvt = cond.returnValueTest.value;
78
+ if (typeof rvt === 'string' && rvt.startsWith(CONTEXT_PARAM_PREFIX)) {
79
+ requestedParameters.add(rvt);
80
+ }
81
+ // Check condition parameters
82
+ for (const param of cond.parameters ?? []) {
83
+ if (typeof param === 'string' &&
84
+ param.startsWith(CONTEXT_PARAM_PREFIX)) {
85
+ requestedParameters.add(param);
86
+ }
87
+ }
88
+ }
89
+ return requestedParameters;
90
+ }
91
+ async toJson() {
92
+ const parameters = await this.toObj();
93
+ return toJSON(parameters);
94
+ }
95
+ withCustomParams(params) {
96
+ return new ConditionContext(this.provider, this.conditions, params, this.signer);
97
+ }
98
+ async toWASMContext() {
99
+ const asJson = await this.toJson();
100
+ return new Context(asJson);
101
+ }
102
+ static fromConditions(provider, conditions, signer) {
103
+ const innerConditions = [
104
+ ConditionExpression.fromWASMConditions(conditions).condition,
105
+ ];
106
+ return new ConditionContext(provider, innerConditions, {}, signer);
107
+ }
108
+ }
109
+ //# sourceMappingURL=context.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"context.js","sourceRoot":"","sources":["../../../../src/conditions/context/context.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,UAAU,IAAI,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAChF,OAAO,EAAE,QAAQ,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAIpD,OAAO,EAAE,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AACxD,OAAO,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAE9C,OAAO,EAAkB,4BAA4B,EAAE,MAAM,aAAa,CAAC;AAK3E,MAAM,CAAC,MAAM,uBAAuB,GAAG,CAAC,kBAAkB,CAAC,CAAC;AAC5D,MAAM,CAAC,MAAM,oBAAoB,GAAG,GAAG,CAAC;AAExC,MAAM,OAAO,gBAAgB;IAIR;IACA;IACD;IACC;IANF,kBAAkB,CAAgC;IAEnE,YACmB,QAAmC,EACnC,UAAoC,EACrC,mBAAuD,EAAE,EACxD,MAAsB;QAHtB,aAAQ,GAAR,QAAQ,CAA2B;QACnC,eAAU,GAAV,UAAU,CAA0B;QACrC,qBAAgB,GAAhB,gBAAgB,CAAyC;QACxD,WAAM,GAAN,MAAM,CAAgB;QAEvC,IAAI,IAAI,CAAC,MAAM,EAAE;YACf,IAAI,CAAC,kBAAkB,GAAG,IAAI,4BAA4B,CACxD,IAAI,CAAC,QAAQ,EACb,IAAI,CAAC,MAAM,CACZ,CAAC;SACH;QACD,IAAI,CAAC,QAAQ,EAAE,CAAC;IAClB,CAAC;IAEO,QAAQ;QACd,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,gBAAgB,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YACjD,IAAI,uBAAu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zC,IACE,OAAO,KAAK,KAAK,QAAQ;oBACzB,KAAK,CAAC,UAAU,CAAC,oBAAoB,CAAC,EACtC;oBACA,mBAAmB,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;iBAChC;aACF;SACF;QACD,OAAO,mBAAmB,CAAC;IAC7B,CAAC;IAEM,KAAK,CAAC,MAAM;QACjB,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,KAAK,EAAE,CAAC;QACtC,OAAO,MAAM,CAAC,UAAU,CAAC,CAAC;IAC5B,CAAC;IAEM,gBAAgB,CACrB,MAA0C;QAE1C,OAAO,IAAI,gBAAgB,CACzB,IAAI,CAAC,QAAQ,EACb,IAAI,CAAC,UAAU,EACf,MAAM,EACN,IAAI,CAAC,MAAM,CACZ,CAAC;IACJ,CAAC;IAEM,KAAK,CAAC,aAAa;QACxB,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,MAAM,EAAE,CAAC;QACnC,OAAO,IAAI,OAAO,CAAC,MAAM,CAAC,CAAC;IAC7B,CAAC;IAEM,MAAM,CAAC,cAAc,CAC1B,QAAmC,EACnC,UAA0B,EAC1B,MAAsB;QAEtB,MAAM,eAAe,GAAG;YACtB,mBAAmB,CAAC,kBAAkB,CAAC,UAAU,CAAC,CAAC,SAAS;SAC7D,CAAC;QACF,OAAO,IAAI,gBAAgB,CAAC,QAAQ,EAAE,eAAe,EAAE,EAAE,EAAE,MAAM,CAAC,CAAC;IACrE,CAAC;CACF"}
@@ -0,0 +1 @@
1
+ export { ConditionContext, type CustomContextParam } from './context';
@@ -0,0 +1,2 @@
1
+ export { ConditionContext } from './context';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../src/conditions/context/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAA2B,MAAM,WAAW,CAAC"}
@@ -0,0 +1,16 @@
1
+ import { ethers } from 'ethers';
2
+ import { Eip712TypedData } from '../../web3';
3
+ export interface TypedSignature {
4
+ signature: string;
5
+ typedData: Eip712TypedData;
6
+ address: string;
7
+ }
8
+ export declare class WalletAuthenticationProvider {
9
+ private readonly provider;
10
+ private readonly signer;
11
+ private walletSignature?;
12
+ constructor(provider: ethers.providers.Provider, signer: ethers.Signer);
13
+ getOrCreateWalletSignature(): Promise<TypedSignature>;
14
+ private createWalletSignature;
15
+ private getChainData;
16
+ }
@@ -0,0 +1,105 @@
1
+ import { utils as ethersUtils } from 'ethers/lib/ethers';
2
+ export class WalletAuthenticationProvider {
3
+ provider;
4
+ signer;
5
+ walletSignature;
6
+ constructor(provider, signer) {
7
+ this.provider = provider;
8
+ this.signer = signer;
9
+ }
10
+ async getOrCreateWalletSignature() {
11
+ const address = await this.signer.getAddress();
12
+ const storageKey = `wallet-signature-${address}`;
13
+ // If we have a signature in localStorage, return it
14
+ const isLocalStorage = typeof localStorage !== 'undefined';
15
+ if (isLocalStorage) {
16
+ const maybeSignature = localStorage.getItem(storageKey);
17
+ if (maybeSignature) {
18
+ return JSON.parse(maybeSignature);
19
+ }
20
+ }
21
+ // If not, try returning from memory
22
+ const maybeSignature = this.walletSignature?.[address];
23
+ if (maybeSignature) {
24
+ if (isLocalStorage) {
25
+ localStorage.setItem(storageKey, maybeSignature);
26
+ }
27
+ return JSON.parse(maybeSignature);
28
+ }
29
+ // If at this point we didn't return, we need to create a new signature
30
+ const typedSignature = await this.createWalletSignature();
31
+ // Persist where you can
32
+ if (isLocalStorage) {
33
+ localStorage.setItem(storageKey, JSON.stringify(typedSignature));
34
+ }
35
+ if (!this.walletSignature) {
36
+ this.walletSignature = {};
37
+ }
38
+ this.walletSignature[address] = JSON.stringify(typedSignature);
39
+ return typedSignature;
40
+ }
41
+ async createWalletSignature() {
42
+ // Ensure freshness of the signature
43
+ const { blockNumber, blockHash, chainId } = await this.getChainData();
44
+ const address = await this.signer.getAddress();
45
+ const signatureText = `I'm the owner of address ${address} as of block number ${blockNumber}`;
46
+ const salt = ethersUtils.hexlify(ethersUtils.randomBytes(32));
47
+ const typedData = {
48
+ types: {
49
+ Wallet: [
50
+ { name: 'address', type: 'address' },
51
+ { name: 'signatureText', type: 'string' },
52
+ { name: 'blockNumber', type: 'uint256' },
53
+ { name: 'blockHash', type: 'bytes32' },
54
+ ],
55
+ },
56
+ domain: {
57
+ name: 'cbd',
58
+ version: '1',
59
+ chainId,
60
+ salt,
61
+ },
62
+ message: {
63
+ address,
64
+ signatureText,
65
+ blockNumber,
66
+ blockHash,
67
+ },
68
+ };
69
+ // https://github.com/ethers-io/ethers.js/issues/1431#issuecomment-813950552
70
+ const signature = await this.signer._signTypedData(typedData.domain, typedData.types, typedData.message);
71
+ const formattedTypedData = {
72
+ ...typedData,
73
+ primaryType: 'Wallet',
74
+ types: {
75
+ ...typedData.types,
76
+ EIP712Domain: [
77
+ {
78
+ name: 'name',
79
+ type: 'string',
80
+ },
81
+ {
82
+ name: 'version',
83
+ type: 'string',
84
+ },
85
+ {
86
+ name: 'chainId',
87
+ type: 'uint256',
88
+ },
89
+ {
90
+ name: 'salt',
91
+ type: 'bytes32',
92
+ },
93
+ ],
94
+ },
95
+ };
96
+ return { signature, typedData: formattedTypedData, address };
97
+ }
98
+ async getChainData() {
99
+ const blockNumber = await this.provider.getBlockNumber();
100
+ const blockHash = (await this.provider.getBlock(blockNumber)).hash;
101
+ const chainId = (await this.provider.getNetwork()).chainId;
102
+ return { blockNumber, blockHash, chainId };
103
+ }
104
+ }
105
+ //# sourceMappingURL=providers.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"providers.js","sourceRoot":"","sources":["../../../../src/conditions/context/providers.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,KAAK,IAAI,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAgBzD,MAAM,OAAO,4BAA4B;IAIpB;IACA;IAJX,eAAe,CAA0B;IAEjD,YACmB,QAAmC,EACnC,MAAqB;QADrB,aAAQ,GAAR,QAAQ,CAA2B;QACnC,WAAM,GAAN,MAA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oBACD;wBACE,IAAI,EAAE,MAAM;wBACZ,IAAI,EAAE,SAAS;qBAChB;iBACF;aACF;SACF,CAAC;QACF,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,kBAAkB,EAAE,OAAO,EAAE,CAAC;IAC/D,CAAC;IAEO,KAAK,CAAC,YAAY;QACxB,MAAM,WAAW,GAAG,MAAM,IAAI,CAAC,QAAQ,CAAC,cAAc,EAAE,CAAC;QACzD,MAAM,SAAS,GAAG,CAAC,MAAM,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,WAAW,CAAC,CAAC,CAAC,IAAI,CAAC;QACnE,MAAM,OAAO,GAAG,CAAC,MAAM,IAAI,CAAC,QAAQ,CAAC,UAAU,EAAE,CAAC,CAAC,OAAO,CAAC;QAC3D,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,OAAO,EAAE,CAAC;IAC7C,CAAC;CACF"}
@@ -0,0 +1,9 @@
1
+ import * as base from './base';
2
+ import * as predefined from './predefined';
3
+ export * from './base';
4
+ export * from './predefined';
5
+ export { CompoundConditionProps, CompoundConditionType, } from './compound-condition';
6
+ export { Condition, ConditionProps } from './condition';
7
+ export { ConditionExpression, ConditionExpressionJSON } from './condition-expr';
8
+ export { ConditionContext, CustomContextParam } from './context';
9
+ export { base, predefined };
@@ -0,0 +1,10 @@
1
+ import * as base from './base';
2
+ import * as predefined from './predefined';
3
+ export * from './base';
4
+ export * from './predefined';
5
+ export { CompoundConditionType, } from './compound-condition';
6
+ export { Condition } from './condition';
7
+ export { ConditionExpression } from './condition-expr';
8
+ export { ConditionContext } from './context';
9
+ export { base, predefined };
10
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../../src/conditions/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,IAAI,MAAM,QAAQ,CAAC;AAC/B,OAAO,KAAK,UAAU,MAAM,cAAc,CAAC;AAE3C,cAAc,QAAQ,CAAC;AACvB,cAAc,cAAc,CAAC;AAE7B,OAAO,EAEL,qBAAqB,GACtB,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,SAAS,EAAkB,MAAM,aAAa,CAAC;AACxD,OAAO,EAAE,mBAAmB,EAA2B,MAAM,kBAAkB,CAAC;AAChF,OAAO,EAAE,gBAAgB,EAAsB,MAAM,WAAW,CAAC;AACjE,OAAO,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { ContractCondition, ContractConditionProps } from '../base';
2
+ type ERC721OwnershipFields = 'contractAddress' | 'chain' | 'parameters';
3
+ export declare class ERC721Ownership extends ContractCondition {
4
+ constructor(value: Pick<ContractConditionProps, ERC721OwnershipFields>);
5
+ }
6
+ type ERC721BalanceFields = 'contractAddress' | 'chain';
7
+ export declare class ERC721Balance extends ContractCondition {
8
+ constructor(value: Pick<ContractConditionProps, ERC721BalanceFields>);
9
+ }
10
+ export {};
@@ -0,0 +1,34 @@
1
+ import { ContractCondition, ContractConditionType, } from '../base';
2
+ import { USER_ADDRESS_PARAM } from '../const';
3
+ const ERC721OwnershipDefaults = {
4
+ conditionType: ContractConditionType,
5
+ method: 'ownerOf',
6
+ standardContractType: 'ERC721',
7
+ returnValueTest: {
8
+ index: 0,
9
+ comparator: '==',
10
+ value: USER_ADDRESS_PARAM,
11
+ },
12
+ };
13
+ export class ERC721Ownership extends ContractCondition {
14
+ constructor(value) {
15
+ super({ ...ERC721OwnershipDefaults, ...value });
16
+ }
17
+ }
18
+ const ERC721BalanceDefaults = {
19
+ conditionType: ContractConditionType,
20
+ method: 'balanceOf',
21
+ parameters: [USER_ADDRESS_PARAM],
22
+ standardContractType: 'ERC721',
23
+ returnValueTest: {
24
+ index: 0,
25
+ comparator: '>',
26
+ value: '0',
27
+ },
28
+ };
29
+ export class ERC721Balance extends ContractCondition {
30
+ constructor(value) {
31
+ super({ ...ERC721BalanceDefaults, ...value });
32
+ }
33
+ }
34
+ //# sourceMappingURL=erc721.js.map