cashscript 0.11.0 → 0.11.1
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/dist/TransactionBuilder.d.ts +3 -0
- package/dist/TransactionBuilder.js +21 -10
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/utils.d.ts +2 -1
- package/dist/utils.js +11 -0
- package/dist/walletconnect-utils.d.ts +25 -0
- package/dist/walletconnect-utils.js +35 -0
- package/package.json +3 -3
|
@@ -2,6 +2,8 @@ import { Transaction as LibauthTransaction, WalletTemplate } from '@bitauth/liba
|
|
|
2
2
|
import { Unlocker, Output, TransactionDetails, UnlockableUtxo, Utxo, InputOptions } from './interfaces.js';
|
|
3
3
|
import { NetworkProvider } from './network/index.js';
|
|
4
4
|
import { DebugResults } from './debugging.js';
|
|
5
|
+
import { WcTransactionOptions } from './walletconnect-utils.js';
|
|
6
|
+
import { WcTransactionObject } from './walletconnect-utils.js';
|
|
5
7
|
export interface TransactionBuilderOptions {
|
|
6
8
|
provider: NetworkProvider;
|
|
7
9
|
}
|
|
@@ -29,4 +31,5 @@ export declare class TransactionBuilder {
|
|
|
29
31
|
send(): Promise<TransactionDetails>;
|
|
30
32
|
send(raw: true): Promise<string>;
|
|
31
33
|
private getTxDetails;
|
|
34
|
+
generateWcTransactionObject(options?: WcTransactionOptions): WcTransactionObject;
|
|
32
35
|
}
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import { binToHex, decodeTransaction, encodeTransaction, hexToBin, } from '@bitauth/libauth';
|
|
1
|
+
import { binToHex, decodeTransaction, decodeTransactionUnsafe, encodeTransaction, hexToBin, } from '@bitauth/libauth';
|
|
2
2
|
import delay from 'delay';
|
|
3
3
|
import { isUnlockableUtxo, isStandardUnlockableUtxo, } from './interfaces.js';
|
|
4
|
-
import { cashScriptOutputToLibauthOutput, createOpReturnOutput, validateInput, validateOutput, } from './utils.js';
|
|
4
|
+
import { cashScriptOutputToLibauthOutput, createOpReturnOutput, generateLibauthSourceOutputs, validateInput, validateOutput, } from './utils.js';
|
|
5
5
|
import { FailedTransactionError } from './Errors.js';
|
|
6
6
|
import { getBitauthUri } from './LibauthTemplate.js';
|
|
7
7
|
import { debugLibauthTemplate, getLibauthTemplates } from './advanced/LibauthTemplate.js';
|
|
8
|
+
import { getWcContractInfo } from './walletconnect-utils.js';
|
|
8
9
|
import semver from 'semver';
|
|
9
10
|
const DEFAULT_SEQUENCE = 0xfffffffe;
|
|
10
11
|
export class TransactionBuilder {
|
|
@@ -77,14 +78,7 @@ export class TransactionBuilder {
|
|
|
77
78
|
version: 2,
|
|
78
79
|
};
|
|
79
80
|
// Generate source outputs from inputs (for signing with SIGHASH_UTXOS)
|
|
80
|
-
const sourceOutputs = this.inputs
|
|
81
|
-
const sourceOutput = {
|
|
82
|
-
amount: input.satoshis,
|
|
83
|
-
to: input.unlocker.generateLockingBytecode(),
|
|
84
|
-
token: input.token,
|
|
85
|
-
};
|
|
86
|
-
return cashScriptOutputToLibauthOutput(sourceOutput);
|
|
87
|
-
});
|
|
81
|
+
const sourceOutputs = generateLibauthSourceOutputs(this.inputs);
|
|
88
82
|
const inputScripts = this.inputs.map((input, inputIndex) => (input.unlocker.generateUnlockingBytecode({ transaction, sourceOutputs, inputIndex })));
|
|
89
83
|
inputScripts.forEach((script, i) => {
|
|
90
84
|
transaction.inputs[i].unlockingBytecode = script;
|
|
@@ -148,5 +142,22 @@ export class TransactionBuilder {
|
|
|
148
142
|
// Should not happen
|
|
149
143
|
throw new Error('Could not retrieve transaction details for over 10 minutes');
|
|
150
144
|
}
|
|
145
|
+
generateWcTransactionObject(options) {
|
|
146
|
+
const inputs = this.inputs;
|
|
147
|
+
if (!inputs.every(input => isStandardUnlockableUtxo(input))) {
|
|
148
|
+
throw new Error('All inputs must be StandardUnlockableUtxos to generate the wcSourceOutputs');
|
|
149
|
+
}
|
|
150
|
+
const encodedTransaction = this.build();
|
|
151
|
+
const transaction = decodeTransactionUnsafe(hexToBin(encodedTransaction));
|
|
152
|
+
const libauthSourceOutputs = generateLibauthSourceOutputs(inputs);
|
|
153
|
+
const sourceOutputs = libauthSourceOutputs.map((sourceOutput, index) => {
|
|
154
|
+
return {
|
|
155
|
+
...sourceOutput,
|
|
156
|
+
...transaction.inputs[index],
|
|
157
|
+
...getWcContractInfo(inputs[index]),
|
|
158
|
+
};
|
|
159
|
+
});
|
|
160
|
+
return { ...options, transaction, sourceOutputs };
|
|
161
|
+
}
|
|
151
162
|
}
|
|
152
163
|
//# sourceMappingURL=TransactionBuilder.js.map
|
package/dist/index.d.ts
CHANGED
|
@@ -9,3 +9,4 @@ export * from './interfaces.js';
|
|
|
9
9
|
export * from './Errors.js';
|
|
10
10
|
export { type NetworkProvider, BitcoinRpcNetworkProvider, ElectrumNetworkProvider, FullStackNetworkProvider, MockNetworkProvider, } from './network/index.js';
|
|
11
11
|
export { randomUtxo, randomToken, randomNFT } from './utils.js';
|
|
12
|
+
export * from './walletconnect-utils.js';
|
package/dist/index.js
CHANGED
|
@@ -8,4 +8,5 @@ export * from './interfaces.js';
|
|
|
8
8
|
export * from './Errors.js';
|
|
9
9
|
export { BitcoinRpcNetworkProvider, ElectrumNetworkProvider, FullStackNetworkProvider, MockNetworkProvider, } from './network/index.js';
|
|
10
10
|
export { randomUtxo, randomToken, randomNFT } from './utils.js';
|
|
11
|
+
export * from './walletconnect-utils.js';
|
|
11
12
|
//# sourceMappingURL=index.js.map
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { Transaction } from '@bitauth/libauth';
|
|
2
2
|
import { Script } from '@cashscript/utils';
|
|
3
|
-
import { Utxo, Output, LibauthOutput, TokenDetails, AddressType } from './interfaces.js';
|
|
3
|
+
import { Utxo, Output, LibauthOutput, TokenDetails, AddressType, UnlockableUtxo } from './interfaces.js';
|
|
4
4
|
export declare function validateInput(utxo: Utxo): void;
|
|
5
5
|
export declare function validateOutput(output: Output): void;
|
|
6
6
|
export declare function calculateDust(output: Output): number;
|
|
@@ -8,6 +8,7 @@ export declare function getOutputSize(output: Output): number;
|
|
|
8
8
|
export declare function encodeOutput(output: Output): Uint8Array;
|
|
9
9
|
export declare function cashScriptOutputToLibauthOutput(output: Output): LibauthOutput;
|
|
10
10
|
export declare function libauthOutputToCashScriptOutput(output: LibauthOutput): Output;
|
|
11
|
+
export declare function generateLibauthSourceOutputs(inputs: UnlockableUtxo[]): LibauthOutput[];
|
|
11
12
|
export declare function getInputSize(inputScript: Uint8Array): number;
|
|
12
13
|
export declare function getTxSizeWithoutInputs(outputs: Output[]): number;
|
|
13
14
|
export declare function createInputScript(redeemScript: Script, encodedArgs: Uint8Array[], selector?: number): Uint8Array;
|
package/dist/utils.js
CHANGED
|
@@ -74,6 +74,17 @@ export function libauthOutputToCashScriptOutput(output) {
|
|
|
74
74
|
},
|
|
75
75
|
};
|
|
76
76
|
}
|
|
77
|
+
export function generateLibauthSourceOutputs(inputs) {
|
|
78
|
+
const sourceOutputs = inputs.map((input) => {
|
|
79
|
+
const sourceOutput = {
|
|
80
|
+
amount: input.satoshis,
|
|
81
|
+
to: input.unlocker.generateLockingBytecode(),
|
|
82
|
+
token: input.token,
|
|
83
|
+
};
|
|
84
|
+
return cashScriptOutputToLibauthOutput(sourceOutput);
|
|
85
|
+
});
|
|
86
|
+
return sourceOutputs;
|
|
87
|
+
}
|
|
77
88
|
function isTokenAddress(address) {
|
|
78
89
|
const result = decodeCashAddress(address);
|
|
79
90
|
if (typeof result === 'string')
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { StandardUnlockableUtxo, LibauthOutput, Unlocker } from './interfaces.js';
|
|
2
|
+
import { type AbiFunction, type Artifact } from '@cashscript/utils';
|
|
3
|
+
import { type Input, type TransactionCommon } from '@bitauth/libauth';
|
|
4
|
+
export interface WcTransactionOptions {
|
|
5
|
+
broadcast?: boolean;
|
|
6
|
+
userPrompt?: string;
|
|
7
|
+
}
|
|
8
|
+
export interface WcTransactionObject {
|
|
9
|
+
transaction: TransactionCommon | string;
|
|
10
|
+
sourceOutputs: WcSourceOutput[];
|
|
11
|
+
broadcast?: boolean;
|
|
12
|
+
userPrompt?: string;
|
|
13
|
+
}
|
|
14
|
+
export type WcSourceOutput = Input & LibauthOutput & WcContractInfo;
|
|
15
|
+
export interface WcContractInfo {
|
|
16
|
+
contract?: {
|
|
17
|
+
abiFunction: AbiFunction;
|
|
18
|
+
redeemScript: Uint8Array;
|
|
19
|
+
artifact: Partial<Artifact>;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
export declare function getWcContractInfo(input: StandardUnlockableUtxo): WcContractInfo | {};
|
|
23
|
+
export declare const placeholderSignature: () => Uint8Array;
|
|
24
|
+
export declare const placeholderPublicKey: () => Uint8Array;
|
|
25
|
+
export declare const placeholderP2PKHUnlocker: (userAddress: string) => Unlocker;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { scriptToBytecode } from '@cashscript/utils';
|
|
2
|
+
import { cashAddressToLockingBytecode } from '@bitauth/libauth';
|
|
3
|
+
export function getWcContractInfo(input) {
|
|
4
|
+
// If the input does not have a contract unlocker, return an empty object
|
|
5
|
+
if (!('contract' in input.unlocker))
|
|
6
|
+
return {};
|
|
7
|
+
const contract = input.unlocker.contract;
|
|
8
|
+
const abiFunctionName = input.unlocker.abiFunction?.name;
|
|
9
|
+
const abiFunction = contract.artifact.abi.find(abi => abi.name === abiFunctionName);
|
|
10
|
+
if (!abiFunction) {
|
|
11
|
+
throw new Error(`ABI function ${abiFunctionName} not found in contract artifact`);
|
|
12
|
+
}
|
|
13
|
+
const wcContractObj = {
|
|
14
|
+
contract: {
|
|
15
|
+
abiFunction: abiFunction,
|
|
16
|
+
redeemScript: scriptToBytecode(contract.redeemScript),
|
|
17
|
+
artifact: contract.artifact,
|
|
18
|
+
},
|
|
19
|
+
};
|
|
20
|
+
return wcContractObj;
|
|
21
|
+
}
|
|
22
|
+
export const placeholderSignature = () => Uint8Array.from(Array(65));
|
|
23
|
+
export const placeholderPublicKey = () => Uint8Array.from(Array(33));
|
|
24
|
+
export const placeholderP2PKHUnlocker = (userAddress) => {
|
|
25
|
+
const decodeAddressResult = cashAddressToLockingBytecode(userAddress);
|
|
26
|
+
if (typeof decodeAddressResult === 'string') {
|
|
27
|
+
throw new Error(`Invalid address: ${decodeAddressResult}`);
|
|
28
|
+
}
|
|
29
|
+
const lockingBytecode = decodeAddressResult.bytecode;
|
|
30
|
+
return {
|
|
31
|
+
generateLockingBytecode: () => lockingBytecode,
|
|
32
|
+
generateUnlockingBytecode: () => Uint8Array.from(Array(0)),
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
//# sourceMappingURL=walletconnect-utils.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "cashscript",
|
|
3
|
-
"version": "0.11.
|
|
3
|
+
"version": "0.11.1",
|
|
4
4
|
"description": "Easily write and interact with Bitcoin Cash contracts",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"bitcoin cash",
|
|
@@ -46,7 +46,7 @@
|
|
|
46
46
|
},
|
|
47
47
|
"dependencies": {
|
|
48
48
|
"@bitauth/libauth": "^3.1.0-next.2",
|
|
49
|
-
"@cashscript/utils": "^0.11.
|
|
49
|
+
"@cashscript/utils": "^0.11.1",
|
|
50
50
|
"@electrum-cash/network": "^4.1.1",
|
|
51
51
|
"@mr-zwets/bchn-api-wrapper": "^1.0.1",
|
|
52
52
|
"delay": "^6.0.0",
|
|
@@ -63,5 +63,5 @@
|
|
|
63
63
|
"jest": "^29.7.0",
|
|
64
64
|
"typescript": "^5.7.3"
|
|
65
65
|
},
|
|
66
|
-
"gitHead": "
|
|
66
|
+
"gitHead": "ecd880401ed92fa7740ccd23d405f10b2fb61a29"
|
|
67
67
|
}
|