cashscript 0.10.2 → 0.10.3

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.
package/README.md CHANGED
@@ -14,7 +14,7 @@ See the [GitHub repository](https://github.com/CashScript/cashscript) and the [C
14
14
  CashScript is a high-level language that allows you to write Bitcoin Cash smart contracts in a straightforward and familiar way. Its syntax is inspired by Ethereum's Solidity language, but its functionality is different since the underlying systems have very different fundamentals. See the [language documentation](https://cashscript.org/docs/language/) for a full reference of the language.
15
15
 
16
16
  ## The CashScript SDK
17
- The main way to interact with CashScript contracts and integrate them into applications is using the CashScript SDK. This SDK allows you to import `.json` artifact files that were compiled using the `cashc` compiler and convert them to `Contract` objects. These objects are used to create new contract instances. These instances are used to interact with the contracts using the functions that were implemented in the `.cash` file. For more information on the CashScript SDK, refer to the [SDK documentation](https://cashscript.org/docs/sdk/).
17
+ The main way to interact with CashScript contracts and integrate them into applications is using the CashScript SDK. This SDK allows you to import `.json` (or `.ts`) artifact files that were compiled using the `cashc` compiler and convert them to `Contract` objects. These objects are used to create new contract instances. These instances are used to interact with the contracts using the functions that were implemented in the `.cash` file. For more information on the CashScript SDK, refer to the [SDK documentation](https://cashscript.org/docs/sdk/).
18
18
 
19
19
  ### Installation
20
20
  ```bash
@@ -3,8 +3,17 @@ import { Transaction } from './Transaction.js';
3
3
  import { ConstructorArgument, FunctionArgument } from './Argument.js';
4
4
  import { Unlocker, ContractOptions, Utxo, AddressType } from './interfaces.js';
5
5
  import NetworkProvider from './network/NetworkProvider.js';
6
- export declare class Contract {
7
- artifact: Artifact;
6
+ import { ParamsToTuple, AbiToFunctionMap } from './types/type-inference.js';
7
+ export declare class Contract<TArtifact extends Artifact = Artifact, TResolved extends {
8
+ constructorInputs: ConstructorArgument[];
9
+ functions: Record<string, any>;
10
+ unlock: Record<string, any>;
11
+ } = {
12
+ constructorInputs: ParamsToTuple<TArtifact['constructorInputs']>;
13
+ functions: AbiToFunctionMap<TArtifact['abi'], Transaction>;
14
+ unlock: AbiToFunctionMap<TArtifact['abi'], Unlocker>;
15
+ }> {
16
+ artifact: TArtifact;
8
17
  private options?;
9
18
  name: string;
10
19
  address: string;
@@ -12,13 +21,13 @@ export declare class Contract {
12
21
  bytecode: string;
13
22
  bytesize: number;
14
23
  opcount: number;
15
- functions: Record<string, ContractFunction>;
16
- unlock: Record<string, ContractUnlocker>;
24
+ functions: TResolved['functions'];
25
+ unlock: TResolved['unlock'];
17
26
  redeemScript: Script;
18
27
  provider: NetworkProvider;
19
28
  addressType: AddressType;
20
29
  encodedConstructorArgs: Uint8Array[];
21
- constructor(artifact: Artifact, constructorArgs: ConstructorArgument[], options?: ContractOptions | undefined);
30
+ constructor(artifact: TArtifact, constructorArgs: TResolved['constructorInputs'], options?: ContractOptions | undefined);
22
31
  getBalance(): Promise<bigint>;
23
32
  getUtxos(): Promise<Utxo[]>;
24
33
  private createFunction;
package/dist/Contract.js CHANGED
@@ -1,7 +1,7 @@
1
1
  import { binToHex } from '@bitauth/libauth';
2
2
  import { asmToScript, calculateBytesize, countOpcodes, generateRedeemScript, hash256, scriptToBytecode, } from '@cashscript/utils';
3
3
  import { Transaction } from './Transaction.js';
4
- import { encodeFunctionArgument, encodeConstructorArguments, encodeFunctionArguments } from './Argument.js';
4
+ import { encodeFunctionArgument, encodeConstructorArguments, encodeFunctionArguments, } from './Argument.js';
5
5
  import { addressToLockScript, createInputScript, createSighashPreimage, scriptToAddress, } from './utils.js';
6
6
  import SignatureTemplate from './SignatureTemplate.js';
7
7
  import { ElectrumNetworkProvider } from './network/index.js';
@@ -16,7 +16,7 @@ export class Contract {
16
16
  throw new Error('Invalid or incomplete artifact provided');
17
17
  }
18
18
  if (artifact.constructorInputs.length !== constructorArgs.length) {
19
- throw new Error(`Incorrect number of arguments passed to ${artifact.contractName} constructor. Expected ${artifact.constructorInputs.length} arguments (${artifact.constructorInputs.map(input => input.type)}) but got ${constructorArgs.length}`);
19
+ throw new Error(`Incorrect number of arguments passed to ${artifact.contractName} constructor. Expected ${artifact.constructorInputs.length} arguments (${artifact.constructorInputs.map((input) => input.type)}) but got ${constructorArgs.length}`);
20
20
  }
21
21
  // Encode arguments (this also performs type checking)
22
22
  this.encodedConstructorArgs = encodeConstructorArguments(artifact, constructorArgs);
@@ -26,10 +26,12 @@ export class Contract {
26
26
  this.functions = {};
27
27
  if (artifact.abi.length === 1) {
28
28
  const f = artifact.abi[0];
29
+ // @ts-ignore TODO: see if we can use generics to make TypeScript happy
29
30
  this.functions[f.name] = this.createFunction(f);
30
31
  }
31
32
  else {
32
33
  artifact.abi.forEach((f, i) => {
34
+ // @ts-ignore TODO: see if we can use generics to make TypeScript happy
33
35
  this.functions[f.name] = this.createFunction(f, i);
34
36
  });
35
37
  }
@@ -38,10 +40,12 @@ export class Contract {
38
40
  this.unlock = {};
39
41
  if (artifact.abi.length === 1) {
40
42
  const f = artifact.abi[0];
43
+ // @ts-ignore TODO: see if we can use generics to make TypeScript happy
41
44
  this.unlock[f.name] = this.createUnlocker(f);
42
45
  }
43
46
  else {
44
47
  artifact.abi.forEach((f, i) => {
48
+ // @ts-ignore TODO: see if we can use generics to make TypeScript happy
45
49
  this.unlock[f.name] = this.createUnlocker(f, i);
46
50
  });
47
51
  }
@@ -62,7 +66,7 @@ export class Contract {
62
66
  createFunction(abiFunction, selector) {
63
67
  return (...args) => {
64
68
  if (abiFunction.inputs.length !== args.length) {
65
- throw new Error(`Incorrect number of arguments passed to function ${abiFunction.name}. Expected ${abiFunction.inputs.length} arguments (${abiFunction.inputs.map(input => input.type)}) but got ${args.length}`);
69
+ throw new Error(`Incorrect number of arguments passed to function ${abiFunction.name}. Expected ${abiFunction.inputs.length} arguments (${abiFunction.inputs.map((input) => input.type)}) but got ${args.length}`);
66
70
  }
67
71
  // Encode passed args (this also performs type checking)
68
72
  const encodedArgs = encodeFunctionArguments(abiFunction, args);
@@ -73,7 +77,7 @@ export class Contract {
73
77
  createUnlocker(abiFunction, selector) {
74
78
  return (...args) => {
75
79
  if (abiFunction.inputs.length !== args.length) {
76
- throw new Error(`Incorrect number of arguments passed to function ${abiFunction.name}. Expected ${abiFunction.inputs.length} arguments (${abiFunction.inputs.map(input => input.type)}) but got ${args.length}`);
80
+ throw new Error(`Incorrect number of arguments passed to function ${abiFunction.name}. Expected ${abiFunction.inputs.length} arguments (${abiFunction.inputs.map((input) => input.type)}) but got ${args.length}`);
77
81
  }
78
82
  const bytecode = scriptToBytecode(this.redeemScript);
79
83
  const encodedArgs = args
@@ -0,0 +1,32 @@
1
+ import type SignatureTemplate from '../SignatureTemplate.js';
2
+ type TypeMap = {
3
+ [k: `bytes${number}`]: Uint8Array | string;
4
+ } & {
5
+ byte: Uint8Array | string;
6
+ bytes: Uint8Array | string;
7
+ bool: boolean;
8
+ int: bigint;
9
+ string: string;
10
+ pubkey: Uint8Array | string;
11
+ sig: SignatureTemplate | Uint8Array | string;
12
+ datasig: Uint8Array | string;
13
+ };
14
+ type ProcessParam<Param> = Param extends {
15
+ type: infer Type;
16
+ } ? Type extends keyof TypeMap ? TypeMap[Type] : any : any;
17
+ export type ParamsToTuple<Params> = Params extends readonly [infer Head, ...infer Tail] ? [ProcessParam<Head>, ...ParamsToTuple<Tail>] : Params extends readonly [] ? [] : any[];
18
+ type ProcessFunction<Function, ReturnType> = Function extends {
19
+ name: string;
20
+ inputs: readonly any[];
21
+ } ? {
22
+ [functionName in Function['name']]: (...functionParameters: ParamsToTuple<Function['inputs']>) => ReturnType;
23
+ } : {};
24
+ type InternalAbiToFunctionMap<Abi, ReturnType> = unknown extends Abi ? GenericFunctionMap<ReturnType> : Abi extends readonly [infer Head, ...infer Tail] ? ProcessFunction<Head, ReturnType> & InternalAbiToFunctionMap<Tail, ReturnType> : Abi extends readonly [] ? {} : GenericFunctionMap<ReturnType>;
25
+ type GenericFunctionMap<ReturnType> = {
26
+ [functionName: string]: (...functionParameters: any[]) => ReturnType;
27
+ };
28
+ type Prettify<T> = {
29
+ [K in keyof T]: T[K];
30
+ } & {};
31
+ export type AbiToFunctionMap<T, ReturnType> = Prettify<InternalAbiToFunctionMap<T, ReturnType>>;
32
+ export {};
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=type-inference.js.map
package/dist/utils.d.ts CHANGED
@@ -34,4 +34,4 @@ export declare const randomNFT: (defaults?: Partial<TokenDetails>) => TokenDetai
34
34
  export declare function findLastIndex<T>(array: Array<T>, predicate: (value: T, index: number, obj: T[]) => boolean): number;
35
35
  export declare const snakeCase: (str: string) => string;
36
36
  export declare const extendedStringify: (obj: any, spaces?: number) => string;
37
- export declare const zip: <T, U>(a: T[], b: U[]) => [T, U][];
37
+ export declare const zip: <T, U>(a: readonly T[], b: readonly U[]) => [T, U][];
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "cashscript",
3
- "version": "0.10.2",
3
+ "version": "0.10.3",
4
4
  "description": "Easily write and interact with Bitcoin Cash contracts",
5
5
  "keywords": [
6
6
  "bitcoin cash",
@@ -44,7 +44,7 @@
44
44
  },
45
45
  "dependencies": {
46
46
  "@bitauth/libauth": "^3.0.0",
47
- "@cashscript/utils": "^0.10.2",
47
+ "@cashscript/utils": "^0.10.3",
48
48
  "@mr-zwets/bchn-api-wrapper": "^1.0.1",
49
49
  "delay": "^5.0.0",
50
50
  "electrum-cash": "^2.0.10",
@@ -59,5 +59,5 @@
59
59
  "jest": "^29.4.1",
60
60
  "typescript": "^5.5.4"
61
61
  },
62
- "gitHead": "a7c4f652462ddfccd54fc929f06cb16022769df9"
62
+ "gitHead": "1aa7c6b26ee8add3ff8cf42227141d9bea8aba66"
63
63
  }