cashscript 0.10.0 → 0.10.2
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 +1 -1
- package/dist/LibauthTemplate.js +5 -5
- package/dist/Transaction.js +16 -7
- package/dist/TransactionBuilder.js +1 -1
- package/dist/interfaces.d.ts +1 -0
- package/dist/interfaces.js +1 -0
- package/dist/network/BitcoinRpcNetworkProvider.d.ts +6 -30
- package/dist/network/BitcoinRpcNetworkProvider.js +6 -6
- package/dist/network/MockNetworkProvider.js +1 -1
- package/dist/utils.js +1 -0
- package/package.json +5 -7
package/README.md
CHANGED
|
@@ -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/LibauthTemplate.js
CHANGED
|
@@ -38,7 +38,7 @@ export const buildTemplate = async ({ transaction, transactionHex = undefined, /
|
|
|
38
38
|
},
|
|
39
39
|
};
|
|
40
40
|
// add extra unlocking and locking script for P2PKH inputs spent alongside our contract
|
|
41
|
-
// this is needed for correct cross-
|
|
41
|
+
// this is needed for correct cross-references in the template
|
|
42
42
|
template.scripts[unlockScriptName] = {
|
|
43
43
|
name: unlockScriptName,
|
|
44
44
|
script: `<${signatureString}>\n<${placeholderKeyName}.public_key>`,
|
|
@@ -175,7 +175,7 @@ const generateTemplateScenarioTransaction = (contract, libauthTransaction, csTra
|
|
|
175
175
|
outpointIndex: input.outpointIndex,
|
|
176
176
|
outpointTransactionHash: binToHex(input.outpointTransactionHash),
|
|
177
177
|
sequenceNumber: input.sequenceNumber,
|
|
178
|
-
unlockingBytecode: generateTemplateScenarioBytecode(csInput, `p2pkh_placeholder_unlock_${index}`, index === slotIndex),
|
|
178
|
+
unlockingBytecode: generateTemplateScenarioBytecode(csInput, `p2pkh_placeholder_unlock_${index}`, `placeholder_key_${index}`, index === slotIndex),
|
|
179
179
|
};
|
|
180
180
|
});
|
|
181
181
|
const locktime = libauthTransaction.locktime;
|
|
@@ -201,21 +201,21 @@ const generateTemplateScenarioSourceOutputs = (csTransaction) => {
|
|
|
201
201
|
const slotIndex = csTransaction.inputs.findIndex((input) => !isUtxoP2PKH(input));
|
|
202
202
|
return csTransaction.inputs.map((input, index) => {
|
|
203
203
|
return {
|
|
204
|
-
lockingBytecode: generateTemplateScenarioBytecode(input, `p2pkh_placeholder_lock_${index}`, index === slotIndex),
|
|
204
|
+
lockingBytecode: generateTemplateScenarioBytecode(input, `p2pkh_placeholder_lock_${index}`, `placeholder_key_${index}`, index === slotIndex),
|
|
205
205
|
valueSatoshis: Number(input.satoshis),
|
|
206
206
|
token: serialiseTokenDetails(input.token),
|
|
207
207
|
};
|
|
208
208
|
});
|
|
209
209
|
};
|
|
210
210
|
// Used for generating the locking / unlocking bytecode for source outputs and inputs
|
|
211
|
-
const generateTemplateScenarioBytecode = (input, p2pkhScriptName, insertSlot) => {
|
|
211
|
+
const generateTemplateScenarioBytecode = (input, p2pkhScriptName, placeholderKeyName, insertSlot) => {
|
|
212
212
|
if (isUtxoP2PKH(input)) {
|
|
213
213
|
return {
|
|
214
214
|
script: p2pkhScriptName,
|
|
215
215
|
overrides: {
|
|
216
216
|
keys: {
|
|
217
217
|
privateKeys: {
|
|
218
|
-
|
|
218
|
+
[placeholderKeyName]: binToHex(input.template.privateKey),
|
|
219
219
|
},
|
|
220
220
|
},
|
|
221
221
|
},
|
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) {
|
|
@@ -191,7 +190,7 @@ export class Transaction {
|
|
|
191
190
|
// Compare nfts in- and outputs, check if inputs have nfts corresponding to outputs
|
|
192
191
|
// Keep list of nfts in inputs without matching output
|
|
193
192
|
// First check immutable nfts, then mutable & minting nfts together
|
|
194
|
-
// This is so an
|
|
193
|
+
// This is so an immutable input gets matched first and is removed from the list of unused nfts
|
|
195
194
|
let unusedNfts = listNftsInputs;
|
|
196
195
|
for (const nftInput of listNftsInputs) {
|
|
197
196
|
if (nftInput.capability === 'none') {
|
|
@@ -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)))
|
|
@@ -17,7 +17,7 @@ export class TransactionBuilder {
|
|
|
17
17
|
addInputs(utxos, unlocker, options) {
|
|
18
18
|
if ((!unlocker && utxos.some((utxo) => !isUnlockableUtxo(utxo)))
|
|
19
19
|
|| (unlocker && utxos.some((utxo) => isUnlockableUtxo(utxo)))) {
|
|
20
|
-
throw new Error('Either all UTXOs must have an individual unlocker
|
|
20
|
+
throw new Error('Either all UTXOs must have an individual unlocker specified, or no UTXOs must have an individual unlocker specified and a shared unlocker must be provided');
|
|
21
21
|
}
|
|
22
22
|
if (!unlocker) {
|
|
23
23
|
this.inputs = this.inputs.concat(utxos);
|
package/dist/interfaces.d.ts
CHANGED
package/dist/interfaces.js
CHANGED
|
@@ -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;
|
|
@@ -10,7 +10,7 @@ export default class MockNetworkProvider {
|
|
|
10
10
|
constructor() {
|
|
11
11
|
this.utxoMap = {};
|
|
12
12
|
this.transactionMap = {};
|
|
13
|
-
this.network = Network.
|
|
13
|
+
this.network = Network.MOCKNET;
|
|
14
14
|
for (let i = 0; i < 3; i += 1) {
|
|
15
15
|
this.addUtxo(aliceAddress, randomUtxo());
|
|
16
16
|
this.addUtxo(bobAddress, randomUtxo());
|
package/dist/utils.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cashscript",
|
|
3
|
-
"version": "0.10.
|
|
3
|
+
"version": "0.10.2",
|
|
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.2",
|
|
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": "a7c4f652462ddfccd54fc929f06cb16022769df9"
|
|
65
63
|
}
|