@inversealtruism/csd-tx 0.1.0
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/dist/index.cjs +133 -0
- package/dist/index.d.cts +74 -0
- package/dist/index.d.ts +74 -0
- package/dist/index.js +102 -0
- package/package.json +42 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 InverseAltruism
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/index.ts
|
|
21
|
+
var index_exports = {};
|
|
22
|
+
__export(index_exports, {
|
|
23
|
+
buildAttest: () => buildAttest,
|
|
24
|
+
buildPropose: () => buildPropose,
|
|
25
|
+
buildSend: () => buildSend,
|
|
26
|
+
selectInputs: () => selectInputs,
|
|
27
|
+
signTx: () => signTx,
|
|
28
|
+
txSize: () => txSize,
|
|
29
|
+
txToNodeJson: () => txToNodeJson
|
|
30
|
+
});
|
|
31
|
+
module.exports = __toCommonJS(index_exports);
|
|
32
|
+
var import_csd_codec = require("@inversealtruism/csd-codec");
|
|
33
|
+
var import_csd_crypto = require("@inversealtruism/csd-crypto");
|
|
34
|
+
function selectInputs(utxos, need) {
|
|
35
|
+
const seen = /* @__PURE__ */ new Set();
|
|
36
|
+
const confirmed = utxos.filter((x) => {
|
|
37
|
+
if (Number(x.confirmations ?? 0) < 1) return false;
|
|
38
|
+
const v = Number(x.value);
|
|
39
|
+
if (!Number.isFinite(v) || v <= 0 || !Number.isSafeInteger(v)) return false;
|
|
40
|
+
const key = `${String(x.txid).toLowerCase()}:${Number(x.vout)}`;
|
|
41
|
+
if (seen.has(key)) return false;
|
|
42
|
+
seen.add(key);
|
|
43
|
+
return true;
|
|
44
|
+
});
|
|
45
|
+
const take = (pool) => {
|
|
46
|
+
const inputs = [];
|
|
47
|
+
let total = 0;
|
|
48
|
+
for (const x of [...pool].sort((a, b) => Number(b.value) - Number(a.value))) {
|
|
49
|
+
const v = Number(x.value);
|
|
50
|
+
total += v;
|
|
51
|
+
if (!Number.isSafeInteger(total)) return null;
|
|
52
|
+
inputs.push({ txid: x.txid, vout: Number(x.vout), value: v });
|
|
53
|
+
if (inputs.length > import_csd_codec.MAX_TX_INPUTS) return null;
|
|
54
|
+
if (total >= need) return { inputs, total };
|
|
55
|
+
}
|
|
56
|
+
return null;
|
|
57
|
+
};
|
|
58
|
+
return take(confirmed.filter((x) => !x.coinbase)) ?? take(confirmed);
|
|
59
|
+
}
|
|
60
|
+
function txSize(tx) {
|
|
61
|
+
return (0, import_csd_codec.serialize)(tx).length;
|
|
62
|
+
}
|
|
63
|
+
var bytesArr = (hex) => Array.from(hex.startsWith("0x") ? hexToU8(hex.slice(2)) : hexToU8(hex));
|
|
64
|
+
function hexToU8(h) {
|
|
65
|
+
const o = new Uint8Array(h.length / 2);
|
|
66
|
+
for (let i = 0; i < o.length; i++) o[i] = parseInt(h.slice(i * 2, i * 2 + 2), 16);
|
|
67
|
+
return o;
|
|
68
|
+
}
|
|
69
|
+
function appToJson(app) {
|
|
70
|
+
if (app.type === "None") return "None";
|
|
71
|
+
if (app.type === "Propose") return { Propose: { domain: app.domain, payload_hash: bytesArr(app.payloadHash), uri: app.uri, expires_epoch: Number(app.expiresEpoch) } };
|
|
72
|
+
return { Attest: { proposal_id: bytesArr(app.proposalId), score: app.score, confidence: app.confidence } };
|
|
73
|
+
}
|
|
74
|
+
function txToNodeJson(tx) {
|
|
75
|
+
return {
|
|
76
|
+
version: tx.version,
|
|
77
|
+
locktime: tx.locktime,
|
|
78
|
+
app: appToJson(tx.app),
|
|
79
|
+
inputs: tx.inputs.map((i) => ({ prevout: { txid: bytesArr(i.prevTxid), vout: i.vout }, script_sig: bytesArr(i.scriptSig) })),
|
|
80
|
+
outputs: tx.outputs.map((o) => ({ value: Number(o.value), script_pubkey: bytesArr(o.scriptPubkey) }))
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
function signTx(tx, priv) {
|
|
84
|
+
const sh = (0, import_csd_codec.sighash)(tx);
|
|
85
|
+
const { sig64, pub33 } = (0, import_csd_crypto.signDigest)(sh, priv);
|
|
86
|
+
const ss = (0, import_csd_crypto.buildScriptSig)(sig64, pub33);
|
|
87
|
+
const signed = { ...tx, inputs: tx.inputs.map((i) => ({ ...i, scriptSig: ss })) };
|
|
88
|
+
return { tx: signed, txid: (0, import_csd_codec.txid)(signed), sighash: sh, nodeJson: txToNodeJson(signed) };
|
|
89
|
+
}
|
|
90
|
+
function selectAndAssemble(utxos, outs, fee, app, priv) {
|
|
91
|
+
if (!Number.isSafeInteger(fee) || fee < 0) return { ok: false, error: "fee out of range" };
|
|
92
|
+
let sumOut = 0;
|
|
93
|
+
for (const o of outs) {
|
|
94
|
+
if (!(0, import_csd_crypto.isValidAddr)(String(o.scriptPubkey))) return { ok: false, error: "each recipient must be a 0x\u2026 20-byte address" };
|
|
95
|
+
const v = Number(o.value);
|
|
96
|
+
if (!(v >= 0) || !Number.isSafeInteger(v)) return { ok: false, error: "each amount must be a non-negative safe integer" };
|
|
97
|
+
sumOut += v;
|
|
98
|
+
if (!Number.isSafeInteger(sumOut)) return { ok: false, error: "total outputs exceed safe-integer range" };
|
|
99
|
+
}
|
|
100
|
+
const addr = (0, import_csd_crypto.addrFromPriv)(priv);
|
|
101
|
+
const need = sumOut + fee;
|
|
102
|
+
const sel = selectInputs(utxos, need);
|
|
103
|
+
if (!sel) return { ok: false, error: "insufficient confirmed balance for outputs + fee" };
|
|
104
|
+
const change = sel.total - need;
|
|
105
|
+
const outputs = [...outs];
|
|
106
|
+
if (change > 0) outputs.push({ value: change, scriptPubkey: addr });
|
|
107
|
+
const tx = { version: 1, locktime: 0, app, inputs: sel.inputs.map((i) => ({ prevTxid: i.txid, vout: i.vout, scriptSig: "0x" })), outputs };
|
|
108
|
+
return { ok: true, ...signTx(tx, priv), change, inTotal: sel.total, fee };
|
|
109
|
+
}
|
|
110
|
+
function buildSend(p) {
|
|
111
|
+
if (!p.outputs?.length) return { ok: false, error: "at least one output required" };
|
|
112
|
+
for (const o of p.outputs) if (!(Number(o.value) > 0)) return { ok: false, error: "each send amount must be positive" };
|
|
113
|
+
const outs = p.outputs.map((o) => ({ value: Number(o.value), scriptPubkey: String(o.to) }));
|
|
114
|
+
return selectAndAssemble(p.utxos, outs, p.fee, { type: "None" }, p.priv);
|
|
115
|
+
}
|
|
116
|
+
function buildPropose(p) {
|
|
117
|
+
if (p.fee < import_csd_codec.MIN_FEE_PROPOSE) return { ok: false, error: `propose fee must be \u2265 ${import_csd_codec.MIN_FEE_PROPOSE} (0.25 CSD)` };
|
|
118
|
+
return selectAndAssemble(p.utxos, [], p.fee, { type: "Propose", domain: p.domain, payloadHash: p.payloadHash, uri: p.uri, expiresEpoch: p.expiresEpoch }, p.priv);
|
|
119
|
+
}
|
|
120
|
+
function buildAttest(p) {
|
|
121
|
+
if (p.fee < import_csd_codec.MIN_FEE_ATTEST) return { ok: false, error: `attest fee must be \u2265 ${import_csd_codec.MIN_FEE_ATTEST} (0.05 CSD)` };
|
|
122
|
+
return selectAndAssemble(p.utxos, [], p.fee, { type: "Attest", proposalId: p.proposalId, score: p.score >>> 0, confidence: p.confidence >>> 0 }, p.priv);
|
|
123
|
+
}
|
|
124
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
125
|
+
0 && (module.exports = {
|
|
126
|
+
buildAttest,
|
|
127
|
+
buildPropose,
|
|
128
|
+
buildSend,
|
|
129
|
+
selectInputs,
|
|
130
|
+
signTx,
|
|
131
|
+
txSize,
|
|
132
|
+
txToNodeJson
|
|
133
|
+
});
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Tx } from '@inversealtruism/csd-codec';
|
|
2
|
+
export { App, Tx, TxInput, TxOutput } from '@inversealtruism/csd-codec';
|
|
3
|
+
|
|
4
|
+
interface Utxo {
|
|
5
|
+
txid: string;
|
|
6
|
+
vout: number;
|
|
7
|
+
value: number;
|
|
8
|
+
confirmations?: number;
|
|
9
|
+
coinbase?: boolean;
|
|
10
|
+
}
|
|
11
|
+
interface Selection {
|
|
12
|
+
inputs: {
|
|
13
|
+
txid: string;
|
|
14
|
+
vout: number;
|
|
15
|
+
value: number;
|
|
16
|
+
}[];
|
|
17
|
+
total: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Greedy largest-first coin selection. Prefers mature non-coinbase coins; HARDENED against a
|
|
21
|
+
* hostile/buggy RPC: missing confirmations → 0 (unspendable), dedupe by (case-normalized)
|
|
22
|
+
* outpoint, drop non-positive/unsafe values, refuse to exceed the consensus input cap, and
|
|
23
|
+
* safe-integer-guard the running sum so no mis-signed value can slip in.
|
|
24
|
+
*/
|
|
25
|
+
declare function selectInputs(utxos: Utxo[], need: number): Selection | null;
|
|
26
|
+
/** Exact serialized byte size of a tx (deterministic — no scripts/witness). */
|
|
27
|
+
declare function txSize(tx: Tx): number;
|
|
28
|
+
declare function txToNodeJson(tx: Tx): any;
|
|
29
|
+
interface Signed {
|
|
30
|
+
tx: Tx;
|
|
31
|
+
txid: string;
|
|
32
|
+
sighash: string;
|
|
33
|
+
nodeJson: any;
|
|
34
|
+
}
|
|
35
|
+
/** Sign an already-constructed tx. One sighash signs all inputs (CSD blanks every input). */
|
|
36
|
+
declare function signTx(tx: Tx, priv: string): Signed;
|
|
37
|
+
interface BuildResult extends Partial<Signed> {
|
|
38
|
+
ok: boolean;
|
|
39
|
+
error?: string;
|
|
40
|
+
change?: number;
|
|
41
|
+
inTotal?: number;
|
|
42
|
+
fee?: number;
|
|
43
|
+
}
|
|
44
|
+
/** Build + sign a 1→many transfer (None app). Change → sender. */
|
|
45
|
+
declare function buildSend(p: {
|
|
46
|
+
outputs: {
|
|
47
|
+
to: string;
|
|
48
|
+
value: number;
|
|
49
|
+
}[];
|
|
50
|
+
fee: number;
|
|
51
|
+
utxos: Utxo[];
|
|
52
|
+
priv: string;
|
|
53
|
+
}): BuildResult;
|
|
54
|
+
/** Build + sign a Propose (fee paid via input−change; no value output beyond change). */
|
|
55
|
+
declare function buildPropose(p: {
|
|
56
|
+
domain: string;
|
|
57
|
+
payloadHash: string;
|
|
58
|
+
uri: string;
|
|
59
|
+
expiresEpoch: number;
|
|
60
|
+
fee: number;
|
|
61
|
+
utxos: Utxo[];
|
|
62
|
+
priv: string;
|
|
63
|
+
}): BuildResult;
|
|
64
|
+
/** Build + sign an Attest (fee = weight). */
|
|
65
|
+
declare function buildAttest(p: {
|
|
66
|
+
proposalId: string;
|
|
67
|
+
score: number;
|
|
68
|
+
confidence: number;
|
|
69
|
+
fee: number;
|
|
70
|
+
utxos: Utxo[];
|
|
71
|
+
priv: string;
|
|
72
|
+
}): BuildResult;
|
|
73
|
+
|
|
74
|
+
export { type BuildResult, type Selection, type Signed, type Utxo, buildAttest, buildPropose, buildSend, selectInputs, signTx, txSize, txToNodeJson };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { Tx } from '@inversealtruism/csd-codec';
|
|
2
|
+
export { App, Tx, TxInput, TxOutput } from '@inversealtruism/csd-codec';
|
|
3
|
+
|
|
4
|
+
interface Utxo {
|
|
5
|
+
txid: string;
|
|
6
|
+
vout: number;
|
|
7
|
+
value: number;
|
|
8
|
+
confirmations?: number;
|
|
9
|
+
coinbase?: boolean;
|
|
10
|
+
}
|
|
11
|
+
interface Selection {
|
|
12
|
+
inputs: {
|
|
13
|
+
txid: string;
|
|
14
|
+
vout: number;
|
|
15
|
+
value: number;
|
|
16
|
+
}[];
|
|
17
|
+
total: number;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Greedy largest-first coin selection. Prefers mature non-coinbase coins; HARDENED against a
|
|
21
|
+
* hostile/buggy RPC: missing confirmations → 0 (unspendable), dedupe by (case-normalized)
|
|
22
|
+
* outpoint, drop non-positive/unsafe values, refuse to exceed the consensus input cap, and
|
|
23
|
+
* safe-integer-guard the running sum so no mis-signed value can slip in.
|
|
24
|
+
*/
|
|
25
|
+
declare function selectInputs(utxos: Utxo[], need: number): Selection | null;
|
|
26
|
+
/** Exact serialized byte size of a tx (deterministic — no scripts/witness). */
|
|
27
|
+
declare function txSize(tx: Tx): number;
|
|
28
|
+
declare function txToNodeJson(tx: Tx): any;
|
|
29
|
+
interface Signed {
|
|
30
|
+
tx: Tx;
|
|
31
|
+
txid: string;
|
|
32
|
+
sighash: string;
|
|
33
|
+
nodeJson: any;
|
|
34
|
+
}
|
|
35
|
+
/** Sign an already-constructed tx. One sighash signs all inputs (CSD blanks every input). */
|
|
36
|
+
declare function signTx(tx: Tx, priv: string): Signed;
|
|
37
|
+
interface BuildResult extends Partial<Signed> {
|
|
38
|
+
ok: boolean;
|
|
39
|
+
error?: string;
|
|
40
|
+
change?: number;
|
|
41
|
+
inTotal?: number;
|
|
42
|
+
fee?: number;
|
|
43
|
+
}
|
|
44
|
+
/** Build + sign a 1→many transfer (None app). Change → sender. */
|
|
45
|
+
declare function buildSend(p: {
|
|
46
|
+
outputs: {
|
|
47
|
+
to: string;
|
|
48
|
+
value: number;
|
|
49
|
+
}[];
|
|
50
|
+
fee: number;
|
|
51
|
+
utxos: Utxo[];
|
|
52
|
+
priv: string;
|
|
53
|
+
}): BuildResult;
|
|
54
|
+
/** Build + sign a Propose (fee paid via input−change; no value output beyond change). */
|
|
55
|
+
declare function buildPropose(p: {
|
|
56
|
+
domain: string;
|
|
57
|
+
payloadHash: string;
|
|
58
|
+
uri: string;
|
|
59
|
+
expiresEpoch: number;
|
|
60
|
+
fee: number;
|
|
61
|
+
utxos: Utxo[];
|
|
62
|
+
priv: string;
|
|
63
|
+
}): BuildResult;
|
|
64
|
+
/** Build + sign an Attest (fee = weight). */
|
|
65
|
+
declare function buildAttest(p: {
|
|
66
|
+
proposalId: string;
|
|
67
|
+
score: number;
|
|
68
|
+
confidence: number;
|
|
69
|
+
fee: number;
|
|
70
|
+
utxos: Utxo[];
|
|
71
|
+
priv: string;
|
|
72
|
+
}): BuildResult;
|
|
73
|
+
|
|
74
|
+
export { type BuildResult, type Selection, type Signed, type Utxo, buildAttest, buildPropose, buildSend, selectInputs, signTx, txSize, txToNodeJson };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
// src/index.ts
|
|
2
|
+
import { serialize, txid, sighash, MAX_TX_INPUTS, MIN_FEE_PROPOSE, MIN_FEE_ATTEST } from "@inversealtruism/csd-codec";
|
|
3
|
+
import { addrFromPriv, signDigest, buildScriptSig, isValidAddr } from "@inversealtruism/csd-crypto";
|
|
4
|
+
function selectInputs(utxos, need) {
|
|
5
|
+
const seen = /* @__PURE__ */ new Set();
|
|
6
|
+
const confirmed = utxos.filter((x) => {
|
|
7
|
+
if (Number(x.confirmations ?? 0) < 1) return false;
|
|
8
|
+
const v = Number(x.value);
|
|
9
|
+
if (!Number.isFinite(v) || v <= 0 || !Number.isSafeInteger(v)) return false;
|
|
10
|
+
const key = `${String(x.txid).toLowerCase()}:${Number(x.vout)}`;
|
|
11
|
+
if (seen.has(key)) return false;
|
|
12
|
+
seen.add(key);
|
|
13
|
+
return true;
|
|
14
|
+
});
|
|
15
|
+
const take = (pool) => {
|
|
16
|
+
const inputs = [];
|
|
17
|
+
let total = 0;
|
|
18
|
+
for (const x of [...pool].sort((a, b) => Number(b.value) - Number(a.value))) {
|
|
19
|
+
const v = Number(x.value);
|
|
20
|
+
total += v;
|
|
21
|
+
if (!Number.isSafeInteger(total)) return null;
|
|
22
|
+
inputs.push({ txid: x.txid, vout: Number(x.vout), value: v });
|
|
23
|
+
if (inputs.length > MAX_TX_INPUTS) return null;
|
|
24
|
+
if (total >= need) return { inputs, total };
|
|
25
|
+
}
|
|
26
|
+
return null;
|
|
27
|
+
};
|
|
28
|
+
return take(confirmed.filter((x) => !x.coinbase)) ?? take(confirmed);
|
|
29
|
+
}
|
|
30
|
+
function txSize(tx) {
|
|
31
|
+
return serialize(tx).length;
|
|
32
|
+
}
|
|
33
|
+
var bytesArr = (hex) => Array.from(hex.startsWith("0x") ? hexToU8(hex.slice(2)) : hexToU8(hex));
|
|
34
|
+
function hexToU8(h) {
|
|
35
|
+
const o = new Uint8Array(h.length / 2);
|
|
36
|
+
for (let i = 0; i < o.length; i++) o[i] = parseInt(h.slice(i * 2, i * 2 + 2), 16);
|
|
37
|
+
return o;
|
|
38
|
+
}
|
|
39
|
+
function appToJson(app) {
|
|
40
|
+
if (app.type === "None") return "None";
|
|
41
|
+
if (app.type === "Propose") return { Propose: { domain: app.domain, payload_hash: bytesArr(app.payloadHash), uri: app.uri, expires_epoch: Number(app.expiresEpoch) } };
|
|
42
|
+
return { Attest: { proposal_id: bytesArr(app.proposalId), score: app.score, confidence: app.confidence } };
|
|
43
|
+
}
|
|
44
|
+
function txToNodeJson(tx) {
|
|
45
|
+
return {
|
|
46
|
+
version: tx.version,
|
|
47
|
+
locktime: tx.locktime,
|
|
48
|
+
app: appToJson(tx.app),
|
|
49
|
+
inputs: tx.inputs.map((i) => ({ prevout: { txid: bytesArr(i.prevTxid), vout: i.vout }, script_sig: bytesArr(i.scriptSig) })),
|
|
50
|
+
outputs: tx.outputs.map((o) => ({ value: Number(o.value), script_pubkey: bytesArr(o.scriptPubkey) }))
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function signTx(tx, priv) {
|
|
54
|
+
const sh = sighash(tx);
|
|
55
|
+
const { sig64, pub33 } = signDigest(sh, priv);
|
|
56
|
+
const ss = buildScriptSig(sig64, pub33);
|
|
57
|
+
const signed = { ...tx, inputs: tx.inputs.map((i) => ({ ...i, scriptSig: ss })) };
|
|
58
|
+
return { tx: signed, txid: txid(signed), sighash: sh, nodeJson: txToNodeJson(signed) };
|
|
59
|
+
}
|
|
60
|
+
function selectAndAssemble(utxos, outs, fee, app, priv) {
|
|
61
|
+
if (!Number.isSafeInteger(fee) || fee < 0) return { ok: false, error: "fee out of range" };
|
|
62
|
+
let sumOut = 0;
|
|
63
|
+
for (const o of outs) {
|
|
64
|
+
if (!isValidAddr(String(o.scriptPubkey))) return { ok: false, error: "each recipient must be a 0x\u2026 20-byte address" };
|
|
65
|
+
const v = Number(o.value);
|
|
66
|
+
if (!(v >= 0) || !Number.isSafeInteger(v)) return { ok: false, error: "each amount must be a non-negative safe integer" };
|
|
67
|
+
sumOut += v;
|
|
68
|
+
if (!Number.isSafeInteger(sumOut)) return { ok: false, error: "total outputs exceed safe-integer range" };
|
|
69
|
+
}
|
|
70
|
+
const addr = addrFromPriv(priv);
|
|
71
|
+
const need = sumOut + fee;
|
|
72
|
+
const sel = selectInputs(utxos, need);
|
|
73
|
+
if (!sel) return { ok: false, error: "insufficient confirmed balance for outputs + fee" };
|
|
74
|
+
const change = sel.total - need;
|
|
75
|
+
const outputs = [...outs];
|
|
76
|
+
if (change > 0) outputs.push({ value: change, scriptPubkey: addr });
|
|
77
|
+
const tx = { version: 1, locktime: 0, app, inputs: sel.inputs.map((i) => ({ prevTxid: i.txid, vout: i.vout, scriptSig: "0x" })), outputs };
|
|
78
|
+
return { ok: true, ...signTx(tx, priv), change, inTotal: sel.total, fee };
|
|
79
|
+
}
|
|
80
|
+
function buildSend(p) {
|
|
81
|
+
if (!p.outputs?.length) return { ok: false, error: "at least one output required" };
|
|
82
|
+
for (const o of p.outputs) if (!(Number(o.value) > 0)) return { ok: false, error: "each send amount must be positive" };
|
|
83
|
+
const outs = p.outputs.map((o) => ({ value: Number(o.value), scriptPubkey: String(o.to) }));
|
|
84
|
+
return selectAndAssemble(p.utxos, outs, p.fee, { type: "None" }, p.priv);
|
|
85
|
+
}
|
|
86
|
+
function buildPropose(p) {
|
|
87
|
+
if (p.fee < MIN_FEE_PROPOSE) return { ok: false, error: `propose fee must be \u2265 ${MIN_FEE_PROPOSE} (0.25 CSD)` };
|
|
88
|
+
return selectAndAssemble(p.utxos, [], p.fee, { type: "Propose", domain: p.domain, payloadHash: p.payloadHash, uri: p.uri, expiresEpoch: p.expiresEpoch }, p.priv);
|
|
89
|
+
}
|
|
90
|
+
function buildAttest(p) {
|
|
91
|
+
if (p.fee < MIN_FEE_ATTEST) return { ok: false, error: `attest fee must be \u2265 ${MIN_FEE_ATTEST} (0.05 CSD)` };
|
|
92
|
+
return selectAndAssemble(p.utxos, [], p.fee, { type: "Attest", proposalId: p.proposalId, score: p.score >>> 0, confidence: p.confidence >>> 0 }, p.priv);
|
|
93
|
+
}
|
|
94
|
+
export {
|
|
95
|
+
buildAttest,
|
|
96
|
+
buildPropose,
|
|
97
|
+
buildSend,
|
|
98
|
+
selectInputs,
|
|
99
|
+
signTx,
|
|
100
|
+
txSize,
|
|
101
|
+
txToNodeJson
|
|
102
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@inversealtruism/csd-tx",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Compute Substrate transaction builder — coin selection, fee sizing, buildSend/Propose/Attest, signTx, node-submit JSON. p2pkh-only, SafeInteger-guarded.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/index.cjs",
|
|
7
|
+
"module": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"import": "./dist/index.js",
|
|
13
|
+
"require": "./dist/index.cjs"
|
|
14
|
+
}
|
|
15
|
+
},
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"LICENSE"
|
|
19
|
+
],
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@inversealtruism/csd-codec": "0.1.0",
|
|
22
|
+
"@inversealtruism/csd-crypto": "0.1.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@inversealtruism/csd-vectors": "0.1.0"
|
|
26
|
+
},
|
|
27
|
+
"license": "MIT",
|
|
28
|
+
"sideEffects": false,
|
|
29
|
+
"publishConfig": {
|
|
30
|
+
"access": "public"
|
|
31
|
+
},
|
|
32
|
+
"repository": {
|
|
33
|
+
"type": "git",
|
|
34
|
+
"url": "git+https://github.com/InverseAltruism/csd-sdk.git",
|
|
35
|
+
"directory": "packages/tx"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://cairn-substrate.com",
|
|
38
|
+
"scripts": {
|
|
39
|
+
"build": "tsup src/index.ts --format esm,cjs --dts --clean",
|
|
40
|
+
"test": "tsx test/tx.test.ts"
|
|
41
|
+
}
|
|
42
|
+
}
|