cashscript 0.10.1 → 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 +2 -2
- package/dist/Contract.d.ts +14 -5
- package/dist/Contract.js +8 -4
- package/dist/Transaction.js +15 -6
- package/dist/network/BitcoinRpcNetworkProvider.d.ts +6 -30
- package/dist/network/BitcoinRpcNetworkProvider.js +6 -6
- package/dist/types/type-inference.d.ts +32 -0
- package/dist/types/type-inference.js +2 -0
- package/dist/utils.d.ts +1 -1
- package/package.json +5 -7
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
|
|
@@ -31,7 +31,7 @@ Using the CashScript SDK, you can import contract artifact files, create new ins
|
|
|
31
31
|
```ts
|
|
32
32
|
...
|
|
33
33
|
// Import the P2PKH artifact
|
|
34
|
-
import P2PKH from './p2pkh-artifact.json'
|
|
34
|
+
import P2PKH from './p2pkh-artifact.json' with { type: 'json' };
|
|
35
35
|
|
|
36
36
|
// Instantiate a network provider for CashScript's network operations
|
|
37
37
|
const provider = new ElectrumNetworkProvider('mainnet');
|
package/dist/Contract.d.ts
CHANGED
|
@@ -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
|
-
|
|
7
|
-
|
|
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:
|
|
16
|
-
unlock:
|
|
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:
|
|
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
|
package/dist/Transaction.js
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
|
-
import bip68 from 'bip68';
|
|
2
1
|
import { hexToBin, decodeTransaction, } from '@bitauth/libauth';
|
|
3
2
|
import delay from 'delay';
|
|
4
|
-
import { placeholder, scriptToBytecode, } from '@cashscript/utils';
|
|
3
|
+
import { encodeBip68, placeholder, scriptToBytecode, } from '@cashscript/utils';
|
|
5
4
|
import deepEqual from 'fast-deep-equal';
|
|
6
|
-
import { isUtxoP2PKH, } from './interfaces.js';
|
|
5
|
+
import { isUtxoP2PKH, SignatureAlgorithm, } from './interfaces.js';
|
|
7
6
|
import { createInputScript, getInputSize, createOpReturnOutput, getTxSizeWithoutInputs, getPreimageSize, validateOutput, utxoComparator, calculateDust, getOutputSize, utxoTokenComparator, } from './utils.js';
|
|
8
7
|
import SignatureTemplate from './SignatureTemplate.js';
|
|
9
8
|
import { P2PKH_INPUT_SIZE } from './constants.js';
|
|
@@ -57,7 +56,7 @@ export class Transaction {
|
|
|
57
56
|
return this;
|
|
58
57
|
}
|
|
59
58
|
withAge(age) {
|
|
60
|
-
this.sequence =
|
|
59
|
+
this.sequence = encodeBip68({ blocks: age });
|
|
61
60
|
return this;
|
|
62
61
|
}
|
|
63
62
|
withTime(time) {
|
|
@@ -248,8 +247,18 @@ export class Transaction {
|
|
|
248
247
|
}
|
|
249
248
|
}
|
|
250
249
|
}
|
|
251
|
-
// Replace all SignatureTemplate with
|
|
252
|
-
const placeholderArgs = this.encodedFunctionArgs.map((arg) =>
|
|
250
|
+
// Replace all SignatureTemplate with placeholder Uint8Arrays
|
|
251
|
+
const placeholderArgs = this.encodedFunctionArgs.map((arg) => {
|
|
252
|
+
if (!(arg instanceof SignatureTemplate))
|
|
253
|
+
return arg;
|
|
254
|
+
// Schnorr signatures are *always* 65 bytes: 64 for signature + 1 byte for hashtype.
|
|
255
|
+
if (arg.getSignatureAlgorithm() === SignatureAlgorithm.SCHNORR)
|
|
256
|
+
return placeholder(65);
|
|
257
|
+
// ECDSA signatures are at least 71 bytes: 64 bytes for signature + 1 byte for hashtype + 6 bytes for encoding
|
|
258
|
+
// overhead. But it may have up to 2 extra bytes for padding, so we overestimate by 2 bytes.
|
|
259
|
+
// (see https://transactionfee.info/charts/bitcoin-script-ecdsa-length/)
|
|
260
|
+
return placeholder(73);
|
|
261
|
+
});
|
|
253
262
|
// Create a placeholder preimage of the correct size
|
|
254
263
|
const placeholderPreimage = this.abiFunction.covenant
|
|
255
264
|
? placeholder(getPreimageSize(scriptToBytecode(this.contract.redeemScript)))
|
|
@@ -1,40 +1,16 @@
|
|
|
1
1
|
import { Utxo, Network } from '../interfaces.js';
|
|
2
2
|
import NetworkProvider from './NetworkProvider.js';
|
|
3
|
+
import { BchnRpcClient } from '@mr-zwets/bchn-api-wrapper';
|
|
3
4
|
export default class BitcoinRpcNetworkProvider implements NetworkProvider {
|
|
4
5
|
network: Network;
|
|
5
6
|
private rpcClient;
|
|
6
|
-
constructor(network: Network, url: string, opts
|
|
7
|
+
constructor(network: Network, url: string, opts: {
|
|
8
|
+
rpcUser: string;
|
|
9
|
+
rpcPassword: string;
|
|
10
|
+
});
|
|
7
11
|
getUtxos(address: string): Promise<Utxo[]>;
|
|
8
12
|
getBlockHeight(): Promise<number>;
|
|
9
13
|
getRawTransaction(txid: string): Promise<string>;
|
|
10
14
|
sendRawTransaction(txHex: string): Promise<string>;
|
|
11
|
-
getClient():
|
|
15
|
+
getClient(): BchnRpcClient;
|
|
12
16
|
}
|
|
13
|
-
interface ListUnspentItem {
|
|
14
|
-
txid: string;
|
|
15
|
-
vout: number;
|
|
16
|
-
address: string;
|
|
17
|
-
label: string;
|
|
18
|
-
scriptPubKey: string;
|
|
19
|
-
amount: number;
|
|
20
|
-
confirmations: number;
|
|
21
|
-
redeemScript: string;
|
|
22
|
-
spendable: boolean;
|
|
23
|
-
solvable: boolean;
|
|
24
|
-
safe: boolean;
|
|
25
|
-
}
|
|
26
|
-
interface IRpcClientRetry {
|
|
27
|
-
constructor(url: string, opts?: object): void;
|
|
28
|
-
listUnspent(minConf?: number, maxConf?: number, addresses?: string[], includeUnsafe?: boolean, queryOptions?: object): Promise<ListUnspentItem[]>;
|
|
29
|
-
getBlockCount(): Promise<number>;
|
|
30
|
-
getRawTransaction(txid: string, verbose?: boolean, blockHash?: string): Promise<string>;
|
|
31
|
-
sendRawTransaction(hexString: string, allowHighFees?: boolean): Promise<string>;
|
|
32
|
-
generate(nBlocks: number, maxTries?: number): Promise<string[]>;
|
|
33
|
-
generateToAddress(nBlocks: number, address: string, maxTries?: number): Promise<string[]>;
|
|
34
|
-
getNewAddress(label?: string): Promise<string>;
|
|
35
|
-
dumpPrivKey(address: string): Promise<string>;
|
|
36
|
-
getBalance(dummy?: string, minConf?: number, includeWatchOnly?: boolean): Promise<number>;
|
|
37
|
-
getBlock(blockHash: string, verbosity?: number): Promise<string>;
|
|
38
|
-
importAddress(address: string, label?: string, rescan?: boolean, p2sh?: boolean): Promise<void>;
|
|
39
|
-
}
|
|
40
|
-
export {};
|
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { BchnRpcClient, } from '@mr-zwets/bchn-api-wrapper';
|
|
2
2
|
export default class BitcoinRpcNetworkProvider {
|
|
3
3
|
constructor(network, url, opts) {
|
|
4
4
|
this.network = network;
|
|
5
|
-
this.rpcClient = new
|
|
5
|
+
this.rpcClient = new BchnRpcClient({ url, ...opts });
|
|
6
6
|
}
|
|
7
7
|
async getUtxos(address) {
|
|
8
|
-
const result = await this.rpcClient.
|
|
8
|
+
const result = await this.rpcClient.request('listunspent', 0, 9999999, [address]);
|
|
9
9
|
const utxos = result.map((utxo) => ({
|
|
10
10
|
txid: utxo.txid,
|
|
11
11
|
vout: utxo.vout,
|
|
@@ -14,13 +14,13 @@ export default class BitcoinRpcNetworkProvider {
|
|
|
14
14
|
return utxos;
|
|
15
15
|
}
|
|
16
16
|
async getBlockHeight() {
|
|
17
|
-
return this.rpcClient.
|
|
17
|
+
return this.rpcClient.request('getblockcount');
|
|
18
18
|
}
|
|
19
19
|
async getRawTransaction(txid) {
|
|
20
|
-
return this.rpcClient.
|
|
20
|
+
return this.rpcClient.request('getrawtransaction', txid, 0);
|
|
21
21
|
}
|
|
22
22
|
async sendRawTransaction(txHex) {
|
|
23
|
-
return this.rpcClient.
|
|
23
|
+
return this.rpcClient.request('sendrawtransaction', txHex);
|
|
24
24
|
}
|
|
25
25
|
getClient() {
|
|
26
26
|
return this.rpcClient;
|
|
@@ -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 {};
|
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.
|
|
3
|
+
"version": "0.10.3",
|
|
4
4
|
"description": "Easily write and interact with Bitcoin Cash contracts",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"bitcoin cash",
|
|
@@ -44,9 +44,8 @@
|
|
|
44
44
|
},
|
|
45
45
|
"dependencies": {
|
|
46
46
|
"@bitauth/libauth": "^3.0.0",
|
|
47
|
-
"@cashscript/utils": "^0.10.
|
|
48
|
-
"
|
|
49
|
-
"bitcoin-rpc-promise-retry": "^1.3.0",
|
|
47
|
+
"@cashscript/utils": "^0.10.3",
|
|
48
|
+
"@mr-zwets/bchn-api-wrapper": "^1.0.1",
|
|
50
49
|
"delay": "^5.0.0",
|
|
51
50
|
"electrum-cash": "^2.0.10",
|
|
52
51
|
"fast-deep-equal": "^3.1.3",
|
|
@@ -54,12 +53,11 @@
|
|
|
54
53
|
},
|
|
55
54
|
"devDependencies": {
|
|
56
55
|
"@jest/globals": "^29.4.1",
|
|
57
|
-
"@psf/bch-js": "^
|
|
56
|
+
"@psf/bch-js": "^6.8.0",
|
|
58
57
|
"@types/pako": "^2.0.3",
|
|
59
|
-
"bip39": "^3.0.4",
|
|
60
58
|
"eslint": "^8.54.0",
|
|
61
59
|
"jest": "^29.4.1",
|
|
62
60
|
"typescript": "^5.5.4"
|
|
63
61
|
},
|
|
64
|
-
"gitHead": "
|
|
62
|
+
"gitHead": "1aa7c6b26ee8add3ff8cf42227141d9bea8aba66"
|
|
65
63
|
}
|