@bennyblader/ddk-ts 0.1.5
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/LICENSE +21 -0
- package/README.md +150 -0
- package/index.js +524 -0
- package/package.json +95 -0
- package/src/conversions.rs +268 -0
- package/src/lib.rs +285 -0
- package/src/types.rs +111 -0
package/package.json
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@bennyblader/ddk-ts",
|
|
3
|
+
"version": "0.1.5",
|
|
4
|
+
"description": "dlcdevkit typescript bindings",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"repository": "https://github.com/bennyblader/ddk-ffi",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"keywords": [
|
|
9
|
+
"napi-rs",
|
|
10
|
+
"rust",
|
|
11
|
+
"rust-dlc",
|
|
12
|
+
"ddk",
|
|
13
|
+
"dlcdevkit"
|
|
14
|
+
],
|
|
15
|
+
"files": [
|
|
16
|
+
"src"
|
|
17
|
+
],
|
|
18
|
+
"napi": {
|
|
19
|
+
"binaryName": "ddk-ts",
|
|
20
|
+
"targets": [
|
|
21
|
+
"aarch64-apple-darwin",
|
|
22
|
+
"x86_64-unknown-linux-gnu"
|
|
23
|
+
]
|
|
24
|
+
},
|
|
25
|
+
"engines": {
|
|
26
|
+
"node": ">= 6.14.2 < 7 || >= 8.11.2 < 9 || >= 9.11.0 < 10 || >= 10.0.0"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"registry": "https://registry.npmjs.org/",
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"scripts": {
|
|
33
|
+
"artifacts": "napi artifacts",
|
|
34
|
+
"bench": "node --import @oxc-node/core/register benchmark/bench.ts",
|
|
35
|
+
"build": "napi build --platform --release --js index.js --dts index.d.ts",
|
|
36
|
+
"build:debug": "napi build --platform --js src/index.js --dts src/index.d.ts",
|
|
37
|
+
"build:all": "napi build --release --js src/index.js --dts src/index.d.ts --target aarch64-apple-darwin --target x86_64-unknown-linux-gnu",
|
|
38
|
+
"prepublish": "napi prepublish -t npm",
|
|
39
|
+
"prepublishOnly": "napi prepublish -t npm",
|
|
40
|
+
"build:darwin-x64": "napi build --release --js index.js --dts index.d.ts --target x86_64-apple-darwin",
|
|
41
|
+
"build:darwin-arm64": "napi build --release --js index.js --dts index.d.ts --target aarch64-apple-darwin",
|
|
42
|
+
"build:linux-x64": "napi build --release --js index.js --dts index.d.ts --target x86_64-unknown-linux-gnu",
|
|
43
|
+
"build:linux-x64-musl": "napi build --release --js index.js --dts index.d.ts --target x86_64-unknown-linux-musl",
|
|
44
|
+
"build:linux-arm64": "napi build --release --js src/index.js --dts src/index.d.ts --target aarch64-unknown-linux-gnu",
|
|
45
|
+
"build:linux-arm64-musl": "napi build --release --js src/index.js --dts src/index.d.ts --target aarch64-unknown-linux-musl",
|
|
46
|
+
"format": "run-p format:prettier format:rs format:toml",
|
|
47
|
+
"format:prettier": "prettier . -w",
|
|
48
|
+
"format:toml": "taplo format",
|
|
49
|
+
"format:rs": "cargo fmt",
|
|
50
|
+
"test": "ava",
|
|
51
|
+
"test:all": "yarn verify && cargo test && yarn test",
|
|
52
|
+
"verify": "node scripts/verify-parity.js && node scripts/verify-types.js",
|
|
53
|
+
"verify:parity": "node scripts/verify-parity.js",
|
|
54
|
+
"verify:types": "node scripts/verify-types.js",
|
|
55
|
+
"version": "napi version",
|
|
56
|
+
"prepare": "husky"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"@emnapi/core": "^1.4.5",
|
|
60
|
+
"@emnapi/runtime": "^1.4.5",
|
|
61
|
+
"@napi-rs/cli": "^3.0.3",
|
|
62
|
+
"@taplo/cli": "^0.7.0",
|
|
63
|
+
"@tybys/wasm-util": "^0.10.0",
|
|
64
|
+
"ava": "^6.4.1",
|
|
65
|
+
"chalk": "^5.4.1",
|
|
66
|
+
"husky": "^9.1.7",
|
|
67
|
+
"lint-staged": "^16.1.2",
|
|
68
|
+
"npm-run-all2": "^8.0.4",
|
|
69
|
+
"prettier": "^3.6.2",
|
|
70
|
+
"tinybench": "^4.0.1",
|
|
71
|
+
"typescript": "^5.8.3"
|
|
72
|
+
},
|
|
73
|
+
"ava": {
|
|
74
|
+
"timeout": "2m",
|
|
75
|
+
"workerThreads": false,
|
|
76
|
+
"files": [
|
|
77
|
+
"__test__/**/*.spec.mjs"
|
|
78
|
+
]
|
|
79
|
+
},
|
|
80
|
+
"prettier": {
|
|
81
|
+
"printWidth": 120,
|
|
82
|
+
"semi": false,
|
|
83
|
+
"trailingComma": "all",
|
|
84
|
+
"singleQuote": true,
|
|
85
|
+
"arrowParens": "always"
|
|
86
|
+
},
|
|
87
|
+
"workspaces": [
|
|
88
|
+
"example"
|
|
89
|
+
],
|
|
90
|
+
"packageManager": "yarn@4.9.2",
|
|
91
|
+
"optionalDependencies": {
|
|
92
|
+
"@bennyblader/ddk-ts-darwin-arm64": "0.1.5",
|
|
93
|
+
"@bennyblader/ddk-ts-linux-x64-gnu": "0.1.5"
|
|
94
|
+
}
|
|
95
|
+
}
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
use crate::types::*;
|
|
2
|
+
use napi::bindgen_prelude::*;
|
|
3
|
+
|
|
4
|
+
// Helper function to convert BigInt to u64 safely
|
|
5
|
+
pub fn bigint_to_u64(bi: &BigInt) -> Result<u64> {
|
|
6
|
+
let (sign_bit, value, _lossless) = bi.get_u64();
|
|
7
|
+
if sign_bit {
|
|
8
|
+
return Err(Error::from_reason("BigInt value is negative"));
|
|
9
|
+
}
|
|
10
|
+
Ok(value)
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
// Helper function to convert Vec<u8> to Buffer
|
|
14
|
+
pub fn vec_to_buffer(vec: Vec<u8>) -> Buffer {
|
|
15
|
+
Buffer::from(vec)
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Helper function to convert Buffer to Vec<u8>
|
|
19
|
+
pub fn buffer_to_vec(buffer: &Buffer) -> Vec<u8> {
|
|
20
|
+
buffer.to_vec()
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Convert ddk_ffi Transaction to NAPI Transaction
|
|
24
|
+
impl From<ddk_ffi::Transaction> for Transaction {
|
|
25
|
+
fn from(tx: ddk_ffi::Transaction) -> Self {
|
|
26
|
+
Transaction {
|
|
27
|
+
version: tx.version,
|
|
28
|
+
lock_time: tx.lock_time,
|
|
29
|
+
inputs: tx.inputs.into_iter().map(Into::into).collect(),
|
|
30
|
+
outputs: tx.outputs.into_iter().map(Into::into).collect(),
|
|
31
|
+
raw_bytes: Buffer::from(tx.raw_bytes),
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// Convert NAPI Transaction to ddk_ffi Transaction
|
|
37
|
+
impl TryFrom<Transaction> for ddk_ffi::Transaction {
|
|
38
|
+
type Error = napi::Error;
|
|
39
|
+
|
|
40
|
+
fn try_from(tx: Transaction) -> Result<Self> {
|
|
41
|
+
let outputs: Result<Vec<_>> = tx.outputs.into_iter().map(TryInto::try_into).collect();
|
|
42
|
+
Ok(ddk_ffi::Transaction {
|
|
43
|
+
version: tx.version,
|
|
44
|
+
lock_time: tx.lock_time,
|
|
45
|
+
inputs: tx.inputs.into_iter().map(Into::into).collect(),
|
|
46
|
+
outputs: outputs?,
|
|
47
|
+
raw_bytes: tx.raw_bytes.to_vec(),
|
|
48
|
+
})
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// Convert NAPI TxInput to ddk_ffi TxInput
|
|
53
|
+
impl From<TxInput> for ddk_ffi::TxInput {
|
|
54
|
+
fn from(input: TxInput) -> Self {
|
|
55
|
+
ddk_ffi::TxInput {
|
|
56
|
+
txid: input.txid,
|
|
57
|
+
vout: input.vout,
|
|
58
|
+
script_sig: input.script_sig.to_vec(),
|
|
59
|
+
sequence: input.sequence,
|
|
60
|
+
witness: input.witness.into_iter().map(|w| w.to_vec()).collect(),
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Convert ddk_ffi TxInput to NAPI TxInput
|
|
66
|
+
impl From<ddk_ffi::TxInput> for TxInput {
|
|
67
|
+
fn from(input: ddk_ffi::TxInput) -> Self {
|
|
68
|
+
TxInput {
|
|
69
|
+
txid: input.txid,
|
|
70
|
+
vout: input.vout,
|
|
71
|
+
script_sig: Buffer::from(input.script_sig),
|
|
72
|
+
sequence: input.sequence,
|
|
73
|
+
witness: input.witness.into_iter().map(Buffer::from).collect(),
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
// Convert NAPI TxOutput to ddk_ffi TxOutput
|
|
79
|
+
impl TryFrom<TxOutput> for ddk_ffi::TxOutput {
|
|
80
|
+
type Error = napi::Error;
|
|
81
|
+
|
|
82
|
+
fn try_from(output: TxOutput) -> Result<Self> {
|
|
83
|
+
Ok(ddk_ffi::TxOutput {
|
|
84
|
+
value: bigint_to_u64(&output.value)?,
|
|
85
|
+
script_pubkey: output.script_pubkey.to_vec(),
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Convert ddk_ffi TxOutput to NAPI TxOutput
|
|
91
|
+
impl From<ddk_ffi::TxOutput> for TxOutput {
|
|
92
|
+
fn from(output: ddk_ffi::TxOutput) -> Self {
|
|
93
|
+
TxOutput {
|
|
94
|
+
value: BigInt::from(output.value),
|
|
95
|
+
script_pubkey: Buffer::from(output.script_pubkey),
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// Convert NAPI TxInputInfo to ddk_ffi TxInputInfo
|
|
101
|
+
impl TryFrom<TxInputInfo> for ddk_ffi::TxInputInfo {
|
|
102
|
+
type Error = napi::Error;
|
|
103
|
+
|
|
104
|
+
fn try_from(info: TxInputInfo) -> Result<Self> {
|
|
105
|
+
Ok(ddk_ffi::TxInputInfo {
|
|
106
|
+
txid: info.txid,
|
|
107
|
+
vout: info.vout,
|
|
108
|
+
script_sig: info.script_sig.to_vec(),
|
|
109
|
+
max_witness_length: info.max_witness_length,
|
|
110
|
+
serial_id: bigint_to_u64(&info.serial_id)?,
|
|
111
|
+
})
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
// Convert ddk_ffi TxInputInfo to NAPI TxInputInfo
|
|
116
|
+
impl From<ddk_ffi::TxInputInfo> for TxInputInfo {
|
|
117
|
+
fn from(info: ddk_ffi::TxInputInfo) -> Self {
|
|
118
|
+
TxInputInfo {
|
|
119
|
+
txid: info.txid,
|
|
120
|
+
vout: info.vout,
|
|
121
|
+
script_sig: Buffer::from(info.script_sig),
|
|
122
|
+
max_witness_length: info.max_witness_length,
|
|
123
|
+
serial_id: BigInt::from(info.serial_id),
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Convert NAPI DlcOutcome to ddk_ffi DlcOutcome
|
|
129
|
+
impl TryFrom<DlcOutcome> for ddk_ffi::DlcOutcome {
|
|
130
|
+
type Error = napi::Error;
|
|
131
|
+
|
|
132
|
+
fn try_from(outcome: DlcOutcome) -> Result<Self> {
|
|
133
|
+
Ok(ddk_ffi::DlcOutcome {
|
|
134
|
+
local_payout: bigint_to_u64(&outcome.local_payout)?,
|
|
135
|
+
remote_payout: bigint_to_u64(&outcome.remote_payout)?,
|
|
136
|
+
})
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Convert ddk_ffi DlcOutcome to NAPI DlcOutcome
|
|
141
|
+
impl From<ddk_ffi::DlcOutcome> for DlcOutcome {
|
|
142
|
+
fn from(outcome: ddk_ffi::DlcOutcome) -> Self {
|
|
143
|
+
DlcOutcome {
|
|
144
|
+
local_payout: BigInt::from(outcome.local_payout),
|
|
145
|
+
remote_payout: BigInt::from(outcome.remote_payout),
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
// Convert NAPI DlcInputInfo to ddk_ffi DlcInputInfo
|
|
151
|
+
impl TryFrom<DlcInputInfo> for ddk_ffi::DlcInputInfo {
|
|
152
|
+
type Error = napi::Error;
|
|
153
|
+
|
|
154
|
+
fn try_from(info: DlcInputInfo) -> Result<Self> {
|
|
155
|
+
Ok(ddk_ffi::DlcInputInfo {
|
|
156
|
+
fund_tx: info.fund_tx.try_into()?,
|
|
157
|
+
fund_vout: info.fund_vout,
|
|
158
|
+
local_fund_pubkey: info.local_fund_pubkey.to_vec(),
|
|
159
|
+
remote_fund_pubkey: info.remote_fund_pubkey.to_vec(),
|
|
160
|
+
fund_amount: bigint_to_u64(&info.fund_amount)?,
|
|
161
|
+
max_witness_len: info.max_witness_len,
|
|
162
|
+
input_serial_id: bigint_to_u64(&info.input_serial_id)?,
|
|
163
|
+
contract_id: info.contract_id.to_vec(),
|
|
164
|
+
})
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Convert ddk_ffi DlcInputInfo to NAPI DlcInputInfo
|
|
169
|
+
impl From<ddk_ffi::DlcInputInfo> for DlcInputInfo {
|
|
170
|
+
fn from(info: ddk_ffi::DlcInputInfo) -> Self {
|
|
171
|
+
DlcInputInfo {
|
|
172
|
+
fund_tx: info.fund_tx.into(),
|
|
173
|
+
fund_vout: info.fund_vout,
|
|
174
|
+
local_fund_pubkey: Buffer::from(info.local_fund_pubkey),
|
|
175
|
+
remote_fund_pubkey: Buffer::from(info.remote_fund_pubkey),
|
|
176
|
+
fund_amount: BigInt::from(info.fund_amount),
|
|
177
|
+
max_witness_len: info.max_witness_len,
|
|
178
|
+
input_serial_id: BigInt::from(info.input_serial_id),
|
|
179
|
+
contract_id: Buffer::from(info.contract_id),
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
// Convert NAPI PartyParams to ddk_ffi PartyParams
|
|
185
|
+
impl TryFrom<PartyParams> for ddk_ffi::PartyParams {
|
|
186
|
+
type Error = napi::Error;
|
|
187
|
+
|
|
188
|
+
fn try_from(params: PartyParams) -> Result<Self> {
|
|
189
|
+
let inputs: Result<Vec<_>> = params.inputs.into_iter().map(TryInto::try_into).collect();
|
|
190
|
+
let dlc_inputs: Result<Vec<_>> = params
|
|
191
|
+
.dlc_inputs
|
|
192
|
+
.into_iter()
|
|
193
|
+
.map(TryInto::try_into)
|
|
194
|
+
.collect();
|
|
195
|
+
|
|
196
|
+
Ok(ddk_ffi::PartyParams {
|
|
197
|
+
fund_pubkey: params.fund_pubkey.to_vec(),
|
|
198
|
+
change_script_pubkey: params.change_script_pubkey.to_vec(),
|
|
199
|
+
change_serial_id: bigint_to_u64(¶ms.change_serial_id)?,
|
|
200
|
+
payout_script_pubkey: params.payout_script_pubkey.to_vec(),
|
|
201
|
+
payout_serial_id: bigint_to_u64(¶ms.payout_serial_id)?,
|
|
202
|
+
inputs: inputs?,
|
|
203
|
+
input_amount: bigint_to_u64(¶ms.input_amount)?,
|
|
204
|
+
collateral: bigint_to_u64(¶ms.collateral)?,
|
|
205
|
+
dlc_inputs: dlc_inputs?,
|
|
206
|
+
})
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Convert ddk_ffi PartyParams to NAPI PartyParams
|
|
211
|
+
impl From<ddk_ffi::PartyParams> for PartyParams {
|
|
212
|
+
fn from(params: ddk_ffi::PartyParams) -> Self {
|
|
213
|
+
PartyParams {
|
|
214
|
+
fund_pubkey: Buffer::from(params.fund_pubkey),
|
|
215
|
+
change_script_pubkey: Buffer::from(params.change_script_pubkey),
|
|
216
|
+
change_serial_id: BigInt::from(params.change_serial_id),
|
|
217
|
+
payout_script_pubkey: Buffer::from(params.payout_script_pubkey),
|
|
218
|
+
payout_serial_id: BigInt::from(params.payout_serial_id),
|
|
219
|
+
inputs: params.inputs.into_iter().map(Into::into).collect(),
|
|
220
|
+
input_amount: BigInt::from(params.input_amount),
|
|
221
|
+
collateral: BigInt::from(params.collateral),
|
|
222
|
+
dlc_inputs: params.dlc_inputs.into_iter().map(Into::into).collect(),
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
// Convert ddk_ffi DlcTransactions to NAPI DlcTransactions
|
|
228
|
+
impl From<ddk_ffi::DlcTransactions> for DlcTransactions {
|
|
229
|
+
fn from(txs: ddk_ffi::DlcTransactions) -> Self {
|
|
230
|
+
DlcTransactions {
|
|
231
|
+
fund: txs.fund.into(),
|
|
232
|
+
cets: txs.cets.into_iter().map(Into::into).collect(),
|
|
233
|
+
refund: txs.refund.into(),
|
|
234
|
+
funding_script_pubkey: Buffer::from(txs.funding_script_pubkey),
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
// Convert ddk_ffi ChangeOutputAndFees to NAPI ChangeOutputAndFees
|
|
240
|
+
impl From<ddk_ffi::ChangeOutputAndFees> for ChangeOutputAndFees {
|
|
241
|
+
fn from(fees: ddk_ffi::ChangeOutputAndFees) -> Self {
|
|
242
|
+
ChangeOutputAndFees {
|
|
243
|
+
change_output: fees.change_output.into(),
|
|
244
|
+
fund_fee: BigInt::from(fees.fund_fee),
|
|
245
|
+
cet_fee: BigInt::from(fees.cet_fee),
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// Convert NAPI OracleInfo to ddk_ffi OracleInfo
|
|
251
|
+
impl From<OracleInfo> for ddk_ffi::OracleInfo {
|
|
252
|
+
fn from(info: OracleInfo) -> Self {
|
|
253
|
+
ddk_ffi::OracleInfo {
|
|
254
|
+
public_key: info.public_key.to_vec(),
|
|
255
|
+
nonces: info.nonces.into_iter().map(|n| n.to_vec()).collect(),
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
// Convert ddk_ffi AdaptorSignature to NAPI AdaptorSignature
|
|
261
|
+
impl From<ddk_ffi::AdaptorSignature> for AdaptorSignature {
|
|
262
|
+
fn from(sig: ddk_ffi::AdaptorSignature) -> Self {
|
|
263
|
+
AdaptorSignature {
|
|
264
|
+
signature: Buffer::from(sig.signature),
|
|
265
|
+
proof: Buffer::from(sig.proof),
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
package/src/lib.rs
ADDED
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
#![deny(clippy::all)]
|
|
2
|
+
|
|
3
|
+
pub mod conversions;
|
|
4
|
+
mod types;
|
|
5
|
+
|
|
6
|
+
use conversions::*;
|
|
7
|
+
use napi::bindgen_prelude::*;
|
|
8
|
+
use napi_derive::napi;
|
|
9
|
+
use types::*;
|
|
10
|
+
|
|
11
|
+
// Import ddk_ffi crate
|
|
12
|
+
extern crate ddk_ffi;
|
|
13
|
+
|
|
14
|
+
#[napi]
|
|
15
|
+
pub fn version() -> String {
|
|
16
|
+
ddk_ffi::version()
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
#[napi]
|
|
20
|
+
pub fn create_fund_tx_locking_script(
|
|
21
|
+
local_fund_pubkey: Buffer,
|
|
22
|
+
remote_fund_pubkey: Buffer,
|
|
23
|
+
) -> Result<Buffer> {
|
|
24
|
+
let local_pubkey = buffer_to_vec(&local_fund_pubkey);
|
|
25
|
+
let remote_pubkey = buffer_to_vec(&remote_fund_pubkey);
|
|
26
|
+
|
|
27
|
+
let result = ddk_ffi::create_fund_tx_locking_script(local_pubkey, remote_pubkey)
|
|
28
|
+
.map_err(|e| Error::from_reason(format!("{:?}", e)))?;
|
|
29
|
+
|
|
30
|
+
Ok(vec_to_buffer(result))
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
#[napi]
|
|
34
|
+
pub fn create_dlc_transactions(
|
|
35
|
+
outcomes: Vec<DlcOutcome>,
|
|
36
|
+
local_params: PartyParams,
|
|
37
|
+
remote_params: PartyParams,
|
|
38
|
+
refund_locktime: u32,
|
|
39
|
+
fee_rate: BigInt,
|
|
40
|
+
fund_lock_time: u32,
|
|
41
|
+
cet_lock_time: u32,
|
|
42
|
+
fund_output_serial_id: BigInt,
|
|
43
|
+
) -> Result<DlcTransactions> {
|
|
44
|
+
let ffi_outcomes: Result<Vec<ddk_ffi::DlcOutcome>> =
|
|
45
|
+
outcomes.into_iter().map(TryInto::try_into).collect();
|
|
46
|
+
|
|
47
|
+
let ffi_local_params = local_params.try_into()?;
|
|
48
|
+
let ffi_remote_params = remote_params.try_into()?;
|
|
49
|
+
|
|
50
|
+
let result = ddk_ffi::create_dlc_transactions(
|
|
51
|
+
ffi_outcomes?,
|
|
52
|
+
ffi_local_params,
|
|
53
|
+
ffi_remote_params,
|
|
54
|
+
refund_locktime,
|
|
55
|
+
bigint_to_u64(&fee_rate)?,
|
|
56
|
+
fund_lock_time,
|
|
57
|
+
cet_lock_time,
|
|
58
|
+
bigint_to_u64(&fund_output_serial_id)?,
|
|
59
|
+
)
|
|
60
|
+
.map_err(|e| Error::from_reason(format!("{:?}", e)))?;
|
|
61
|
+
|
|
62
|
+
Ok(result.into())
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
#[napi]
|
|
66
|
+
pub fn create_spliced_dlc_transactions(
|
|
67
|
+
outcomes: Vec<DlcOutcome>,
|
|
68
|
+
local_params: PartyParams,
|
|
69
|
+
remote_params: PartyParams,
|
|
70
|
+
refund_locktime: u32,
|
|
71
|
+
fee_rate: BigInt,
|
|
72
|
+
fund_lock_time: u32,
|
|
73
|
+
cet_lock_time: u32,
|
|
74
|
+
fund_output_serial_id: BigInt,
|
|
75
|
+
) -> Result<DlcTransactions> {
|
|
76
|
+
let ffi_outcomes: Result<Vec<ddk_ffi::DlcOutcome>> =
|
|
77
|
+
outcomes.into_iter().map(TryInto::try_into).collect();
|
|
78
|
+
|
|
79
|
+
let ffi_local_params = local_params.try_into()?;
|
|
80
|
+
let ffi_remote_params = remote_params.try_into()?;
|
|
81
|
+
|
|
82
|
+
let result = ddk_ffi::create_spliced_dlc_transactions(
|
|
83
|
+
ffi_outcomes?,
|
|
84
|
+
ffi_local_params,
|
|
85
|
+
ffi_remote_params,
|
|
86
|
+
refund_locktime,
|
|
87
|
+
bigint_to_u64(&fee_rate)?,
|
|
88
|
+
fund_lock_time,
|
|
89
|
+
cet_lock_time,
|
|
90
|
+
bigint_to_u64(&fund_output_serial_id)?,
|
|
91
|
+
)
|
|
92
|
+
.map_err(|e| Error::from_reason(format!("{:?}", e)))?;
|
|
93
|
+
|
|
94
|
+
Ok(result.into())
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
#[napi]
|
|
98
|
+
pub fn create_cet(
|
|
99
|
+
local_output: TxOutput,
|
|
100
|
+
local_payout_serial_id: BigInt,
|
|
101
|
+
remote_output: TxOutput,
|
|
102
|
+
remote_payout_serial_id: BigInt,
|
|
103
|
+
fund_tx_id: String,
|
|
104
|
+
fund_vout: u32,
|
|
105
|
+
lock_time: u32,
|
|
106
|
+
) -> Result<Transaction> {
|
|
107
|
+
let result = ddk_ffi::create_cet(
|
|
108
|
+
local_output.try_into()?,
|
|
109
|
+
bigint_to_u64(&local_payout_serial_id)?,
|
|
110
|
+
remote_output.try_into()?,
|
|
111
|
+
bigint_to_u64(&remote_payout_serial_id)?,
|
|
112
|
+
fund_tx_id,
|
|
113
|
+
fund_vout,
|
|
114
|
+
lock_time,
|
|
115
|
+
)
|
|
116
|
+
.map_err(|e| Error::from_reason(format!("{:?}", e)))?;
|
|
117
|
+
|
|
118
|
+
Ok(result.into())
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
#[napi]
|
|
122
|
+
pub fn create_cets(
|
|
123
|
+
fund_tx_id: String,
|
|
124
|
+
fund_vout: u32,
|
|
125
|
+
local_final_script_pubkey: Buffer,
|
|
126
|
+
remote_final_script_pubkey: Buffer,
|
|
127
|
+
outcomes: Vec<DlcOutcome>,
|
|
128
|
+
lock_time: u32,
|
|
129
|
+
local_serial_id: BigInt,
|
|
130
|
+
remote_serial_id: BigInt,
|
|
131
|
+
) -> Result<Vec<Transaction>> {
|
|
132
|
+
let ffi_outcomes: Result<Vec<ddk_ffi::DlcOutcome>> =
|
|
133
|
+
outcomes.into_iter().map(TryInto::try_into).collect();
|
|
134
|
+
|
|
135
|
+
let result = ddk_ffi::create_cets(
|
|
136
|
+
fund_tx_id,
|
|
137
|
+
fund_vout,
|
|
138
|
+
buffer_to_vec(&local_final_script_pubkey),
|
|
139
|
+
buffer_to_vec(&remote_final_script_pubkey),
|
|
140
|
+
ffi_outcomes?,
|
|
141
|
+
lock_time,
|
|
142
|
+
bigint_to_u64(&local_serial_id)?,
|
|
143
|
+
bigint_to_u64(&remote_serial_id)?,
|
|
144
|
+
)
|
|
145
|
+
.map_err(|e| Error::from_reason(format!("{:?}", e)))?;
|
|
146
|
+
|
|
147
|
+
Ok(result.into_iter().map(Into::into).collect())
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
#[napi]
|
|
151
|
+
pub fn create_refund_transaction(
|
|
152
|
+
local_final_script_pubkey: Buffer,
|
|
153
|
+
remote_final_script_pubkey: Buffer,
|
|
154
|
+
local_amount: BigInt,
|
|
155
|
+
remote_amount: BigInt,
|
|
156
|
+
lock_time: u32,
|
|
157
|
+
fund_tx_id: String,
|
|
158
|
+
fund_vout: u32,
|
|
159
|
+
) -> Result<Transaction> {
|
|
160
|
+
let result = ddk_ffi::create_refund_transaction(
|
|
161
|
+
buffer_to_vec(&local_final_script_pubkey),
|
|
162
|
+
buffer_to_vec(&remote_final_script_pubkey),
|
|
163
|
+
bigint_to_u64(&local_amount)?,
|
|
164
|
+
bigint_to_u64(&remote_amount)?,
|
|
165
|
+
lock_time,
|
|
166
|
+
fund_tx_id,
|
|
167
|
+
fund_vout,
|
|
168
|
+
)
|
|
169
|
+
.map_err(|e| Error::from_reason(format!("{:?}", e)))?;
|
|
170
|
+
|
|
171
|
+
Ok(result.into())
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
#[napi]
|
|
175
|
+
pub fn is_dust_output(output: TxOutput) -> Result<bool> {
|
|
176
|
+
eprintln!("is_dust_output called with value: {:?}", output.value);
|
|
177
|
+
let ffi_output = output.try_into()?;
|
|
178
|
+
Ok(ddk_ffi::is_dust_output(ffi_output))
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
#[napi]
|
|
182
|
+
pub fn get_change_output_and_fees(
|
|
183
|
+
params: PartyParams,
|
|
184
|
+
fee_rate: BigInt,
|
|
185
|
+
) -> Result<ChangeOutputAndFees> {
|
|
186
|
+
let result = ddk_ffi::get_change_output_and_fees(params.try_into()?, bigint_to_u64(&fee_rate)?)
|
|
187
|
+
.map_err(|e| Error::from_reason(format!("{:?}", e)))?;
|
|
188
|
+
|
|
189
|
+
Ok(result.into())
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
#[napi]
|
|
193
|
+
pub fn get_total_input_vsize(inputs: Vec<TxInputInfo>) -> Result<u32> {
|
|
194
|
+
let ffi_inputs: Result<Vec<ddk_ffi::TxInputInfo>> =
|
|
195
|
+
inputs.into_iter().map(TryInto::try_into).collect();
|
|
196
|
+
|
|
197
|
+
Ok(ddk_ffi::get_total_input_vsize(ffi_inputs?))
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
#[napi]
|
|
201
|
+
pub fn verify_fund_tx_signature(
|
|
202
|
+
fund_tx: Transaction,
|
|
203
|
+
signature: Buffer,
|
|
204
|
+
pubkey: Buffer,
|
|
205
|
+
txid: String,
|
|
206
|
+
vout: u32,
|
|
207
|
+
input_amount: BigInt,
|
|
208
|
+
) -> Result<bool> {
|
|
209
|
+
let result = ddk_ffi::verify_fund_tx_signature(
|
|
210
|
+
fund_tx.try_into()?,
|
|
211
|
+
buffer_to_vec(&signature),
|
|
212
|
+
buffer_to_vec(&pubkey),
|
|
213
|
+
txid,
|
|
214
|
+
vout,
|
|
215
|
+
bigint_to_u64(&input_amount)?,
|
|
216
|
+
)
|
|
217
|
+
.map_err(|e| Error::from_reason(format!("{:?}", e)))?;
|
|
218
|
+
|
|
219
|
+
Ok(result)
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
#[napi]
|
|
223
|
+
pub fn get_raw_funding_transaction_input_signature(
|
|
224
|
+
funding_transaction: Transaction,
|
|
225
|
+
privkey: Buffer,
|
|
226
|
+
prev_tx_id: String,
|
|
227
|
+
prev_tx_vout: u32,
|
|
228
|
+
value: BigInt,
|
|
229
|
+
) -> Result<Buffer> {
|
|
230
|
+
let result = ddk_ffi::get_raw_funding_transaction_input_signature(
|
|
231
|
+
funding_transaction.try_into()?,
|
|
232
|
+
buffer_to_vec(&privkey),
|
|
233
|
+
prev_tx_id,
|
|
234
|
+
prev_tx_vout,
|
|
235
|
+
bigint_to_u64(&value)?,
|
|
236
|
+
)
|
|
237
|
+
.map_err(|e| Error::from_reason(format!("{:?}", e)))?;
|
|
238
|
+
|
|
239
|
+
Ok(vec_to_buffer(result))
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
#[napi]
|
|
243
|
+
pub fn sign_fund_transaction_input(
|
|
244
|
+
fund_transaction: Transaction,
|
|
245
|
+
privkey: Buffer,
|
|
246
|
+
prev_tx_id: String,
|
|
247
|
+
prev_tx_vout: u32,
|
|
248
|
+
value: BigInt,
|
|
249
|
+
) -> Result<Transaction> {
|
|
250
|
+
let result = ddk_ffi::sign_fund_transaction_input(
|
|
251
|
+
fund_transaction.try_into()?,
|
|
252
|
+
buffer_to_vec(&privkey),
|
|
253
|
+
prev_tx_id,
|
|
254
|
+
prev_tx_vout,
|
|
255
|
+
bigint_to_u64(&value)?,
|
|
256
|
+
)
|
|
257
|
+
.map_err(|e| Error::from_reason(format!("{:?}", e)))?;
|
|
258
|
+
|
|
259
|
+
Ok(result.into())
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
#[napi]
|
|
263
|
+
pub fn create_cet_adaptor_signature_from_oracle_info(
|
|
264
|
+
cet: Transaction,
|
|
265
|
+
oracle_info: OracleInfo,
|
|
266
|
+
funding_sk: Buffer,
|
|
267
|
+
funding_script_pubkey: Buffer,
|
|
268
|
+
total_collateral: BigInt,
|
|
269
|
+
msgs: Vec<Buffer>,
|
|
270
|
+
) -> Result<AdaptorSignature> {
|
|
271
|
+
let ffi_oracle_info = oracle_info.into();
|
|
272
|
+
let ffi_msgs: Vec<Vec<u8>> = msgs.iter().map(buffer_to_vec).collect();
|
|
273
|
+
|
|
274
|
+
let result = ddk_ffi::create_cet_adaptor_signature_from_oracle_info(
|
|
275
|
+
cet.try_into()?,
|
|
276
|
+
ffi_oracle_info,
|
|
277
|
+
buffer_to_vec(&funding_sk),
|
|
278
|
+
buffer_to_vec(&funding_script_pubkey),
|
|
279
|
+
bigint_to_u64(&total_collateral)?,
|
|
280
|
+
ffi_msgs,
|
|
281
|
+
)
|
|
282
|
+
.map_err(|e| Error::from_reason(format!("{:?}", e)))?;
|
|
283
|
+
|
|
284
|
+
Ok(result.into())
|
|
285
|
+
}
|