cashscript 0.11.0 → 0.11.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/dist/TransactionBuilder.d.ts +3 -0
- package/dist/TransactionBuilder.js +17 -10
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/interfaces.d.ts +4 -0
- package/dist/interfaces.js +3 -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 +37 -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,18 @@ 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 encodedTransaction = this.build();
|
|
147
|
+
const transaction = decodeTransactionUnsafe(hexToBin(encodedTransaction));
|
|
148
|
+
const libauthSourceOutputs = generateLibauthSourceOutputs(this.inputs);
|
|
149
|
+
const sourceOutputs = libauthSourceOutputs.map((sourceOutput, index) => {
|
|
150
|
+
return {
|
|
151
|
+
...sourceOutput,
|
|
152
|
+
...transaction.inputs[index],
|
|
153
|
+
...getWcContractInfo(this.inputs[index]),
|
|
154
|
+
};
|
|
155
|
+
});
|
|
156
|
+
return { ...options, transaction, sourceOutputs };
|
|
157
|
+
}
|
|
151
158
|
}
|
|
152
159
|
//# 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/interfaces.d.ts
CHANGED
|
@@ -40,9 +40,13 @@ export interface P2PKHUnlocker extends Unlocker {
|
|
|
40
40
|
template: SignatureTemplate;
|
|
41
41
|
}
|
|
42
42
|
export type StandardUnlocker = ContractUnlocker | P2PKHUnlocker;
|
|
43
|
+
export type PlaceholderP2PKHUnlocker = Unlocker & {
|
|
44
|
+
placeholder: true;
|
|
45
|
+
};
|
|
43
46
|
export declare function isContractUnlocker(unlocker: Unlocker): unlocker is ContractUnlocker;
|
|
44
47
|
export declare function isP2PKHUnlocker(unlocker: Unlocker): unlocker is P2PKHUnlocker;
|
|
45
48
|
export declare function isStandardUnlocker(unlocker: Unlocker): unlocker is StandardUnlocker;
|
|
49
|
+
export declare function isPlaceholderUnlocker(unlocker: Unlocker): unlocker is PlaceholderP2PKHUnlocker;
|
|
46
50
|
export interface UtxoP2PKH extends Utxo {
|
|
47
51
|
template: SignatureTemplate;
|
|
48
52
|
}
|
package/dist/interfaces.js
CHANGED
|
@@ -13,6 +13,9 @@ export function isP2PKHUnlocker(unlocker) {
|
|
|
13
13
|
export function isStandardUnlocker(unlocker) {
|
|
14
14
|
return isContractUnlocker(unlocker) || isP2PKHUnlocker(unlocker);
|
|
15
15
|
}
|
|
16
|
+
export function isPlaceholderUnlocker(unlocker) {
|
|
17
|
+
return 'placeholder' in unlocker;
|
|
18
|
+
}
|
|
16
19
|
export function isUtxoP2PKH(utxo) {
|
|
17
20
|
return 'template' in utxo;
|
|
18
21
|
}
|
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 LibauthOutput, type PlaceholderP2PKHUnlocker, type UnlockableUtxo } 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;
|
|
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: UnlockableUtxo): WcContractInfo | {};
|
|
23
|
+
export declare const placeholderSignature: () => Uint8Array;
|
|
24
|
+
export declare const placeholderPublicKey: () => Uint8Array;
|
|
25
|
+
export declare const placeholderP2PKHUnlocker: (userAddress: string) => PlaceholderP2PKHUnlocker;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { isContractUnlocker } from './interfaces.js';
|
|
2
|
+
import { scriptToBytecode } from '@cashscript/utils';
|
|
3
|
+
import { cashAddressToLockingBytecode } from '@bitauth/libauth';
|
|
4
|
+
export function getWcContractInfo(input) {
|
|
5
|
+
// If the input does not have a contract unlocker, return an empty object
|
|
6
|
+
if (!(isContractUnlocker(input.unlocker)))
|
|
7
|
+
return {};
|
|
8
|
+
const contract = input.unlocker.contract;
|
|
9
|
+
const abiFunctionName = input.unlocker.abiFunction?.name;
|
|
10
|
+
const abiFunction = contract.artifact.abi.find(abi => abi.name === abiFunctionName);
|
|
11
|
+
if (!abiFunction) {
|
|
12
|
+
throw new Error(`ABI function ${abiFunctionName} not found in contract artifact`);
|
|
13
|
+
}
|
|
14
|
+
const wcContractObj = {
|
|
15
|
+
contract: {
|
|
16
|
+
abiFunction: abiFunction,
|
|
17
|
+
redeemScript: scriptToBytecode(contract.redeemScript),
|
|
18
|
+
artifact: contract.artifact,
|
|
19
|
+
},
|
|
20
|
+
};
|
|
21
|
+
return wcContractObj;
|
|
22
|
+
}
|
|
23
|
+
export const placeholderSignature = () => Uint8Array.from(Array(65));
|
|
24
|
+
export const placeholderPublicKey = () => Uint8Array.from(Array(33));
|
|
25
|
+
export const placeholderP2PKHUnlocker = (userAddress) => {
|
|
26
|
+
const decodeAddressResult = cashAddressToLockingBytecode(userAddress);
|
|
27
|
+
if (typeof decodeAddressResult === 'string') {
|
|
28
|
+
throw new Error(`Invalid address: ${decodeAddressResult}`);
|
|
29
|
+
}
|
|
30
|
+
const lockingBytecode = decodeAddressResult.bytecode;
|
|
31
|
+
return {
|
|
32
|
+
generateLockingBytecode: () => lockingBytecode,
|
|
33
|
+
generateUnlockingBytecode: () => Uint8Array.from(Array(0)),
|
|
34
|
+
placeholder: true,
|
|
35
|
+
};
|
|
36
|
+
};
|
|
37
|
+
//# 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.2",
|
|
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.2",
|
|
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": "81e7dc182ef71909af721d62751ef403bcfcc41e"
|
|
67
67
|
}
|