@secondlayer/stacks 0.0.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/README.md +92 -0
- package/dist/shared/stacks-02actsrx.cjs +17 -0
- package/dist/shared/stacks-02actsrx.cjs.map +10 -0
- package/dist/shared/stacks-1aqm7c2h.cjs +110 -0
- package/dist/shared/stacks-1aqm7c2h.cjs.map +11 -0
- package/dist/shared/stacks-24h5k94q.cjs +74 -0
- package/dist/shared/stacks-24h5k94q.cjs.map +11 -0
- package/dist/shared/stacks-2ar4prfz.cjs +328 -0
- package/dist/shared/stacks-2ar4prfz.cjs.map +11 -0
- package/dist/shared/stacks-4py5pm0r.cjs +237 -0
- package/dist/shared/stacks-4py5pm0r.cjs.map +10 -0
- package/dist/shared/stacks-5tprhns9.cjs +54 -0
- package/dist/shared/stacks-5tprhns9.cjs.map +11 -0
- package/dist/shared/stacks-61rvzkz1.cjs +132 -0
- package/dist/shared/stacks-61rvzkz1.cjs.map +17 -0
- package/dist/shared/stacks-709tra19.js +30 -0
- package/dist/shared/stacks-709tra19.js.map +10 -0
- package/dist/shared/stacks-74k8cxgb.cjs +567 -0
- package/dist/shared/stacks-74k8cxgb.cjs.map +13 -0
- package/dist/shared/stacks-8m9byk2j.js +7 -0
- package/dist/shared/stacks-8m9byk2j.js.map +9 -0
- package/dist/shared/stacks-8yykgdww.js +125 -0
- package/dist/shared/stacks-8yykgdww.js.map +11 -0
- package/dist/shared/stacks-9g0jhab3.cjs +40 -0
- package/dist/shared/stacks-9g0jhab3.cjs.map +10 -0
- package/dist/shared/stacks-an5k8n4t.cjs +291 -0
- package/dist/shared/stacks-an5k8n4t.cjs.map +23 -0
- package/dist/shared/stacks-b008c3w8.cjs +214 -0
- package/dist/shared/stacks-b008c3w8.cjs.map +11 -0
- package/dist/shared/stacks-bnvfmt7y.cjs +31 -0
- package/dist/shared/stacks-bnvfmt7y.cjs.map +9 -0
- package/dist/shared/stacks-bycy6t9k.js +22 -0
- package/dist/shared/stacks-bycy6t9k.js.map +10 -0
- package/dist/shared/stacks-d1fmkv3p.js +258 -0
- package/dist/shared/stacks-d1fmkv3p.js.map +13 -0
- package/dist/shared/stacks-dr5en54r.cjs +119 -0
- package/dist/shared/stacks-dr5en54r.cjs.map +10 -0
- package/dist/shared/stacks-dxhntr76.js +278 -0
- package/dist/shared/stacks-dxhntr76.js.map +10 -0
- package/dist/shared/stacks-evnzth1q.cjs +35 -0
- package/dist/shared/stacks-evnzth1q.cjs.map +10 -0
- package/dist/shared/stacks-f3tk2k5s.js +27 -0
- package/dist/shared/stacks-f3tk2k5s.js.map +9 -0
- package/dist/shared/stacks-f7wdrzs3.js +285 -0
- package/dist/shared/stacks-f7wdrzs3.js.map +11 -0
- package/dist/shared/stacks-fcaefc5b.cjs +16 -0
- package/dist/shared/stacks-fcaefc5b.cjs.map +10 -0
- package/dist/shared/stacks-g9mcjcqg.cjs +3 -0
- package/dist/shared/stacks-g9mcjcqg.cjs.map +9 -0
- package/dist/shared/stacks-gck1sq7h.cjs +28 -0
- package/dist/shared/stacks-gck1sq7h.cjs.map +10 -0
- package/dist/shared/stacks-gjvbq6w7.js +46 -0
- package/dist/shared/stacks-gjvbq6w7.js.map +10 -0
- package/dist/shared/stacks-j2cy7qtt.js +237 -0
- package/dist/shared/stacks-j2cy7qtt.js.map +11 -0
- package/dist/shared/stacks-k08tgzyp.cjs +232 -0
- package/dist/shared/stacks-k08tgzyp.cjs.map +13 -0
- package/dist/shared/stacks-k2p5tv6x.js +37 -0
- package/dist/shared/stacks-k2p5tv6x.js.map +10 -0
- package/dist/shared/stacks-k58k970n.js +234 -0
- package/dist/shared/stacks-k58k970n.js.map +10 -0
- package/dist/shared/stacks-n2g56wgg.js +338 -0
- package/dist/shared/stacks-n2g56wgg.js.map +23 -0
- package/dist/shared/stacks-nqytwfcc.js +243 -0
- package/dist/shared/stacks-nqytwfcc.js.map +11 -0
- package/dist/shared/stacks-qcae5852.js +19 -0
- package/dist/shared/stacks-qcae5852.js.map +10 -0
- package/dist/shared/stacks-qt8ehebn.js +76 -0
- package/dist/shared/stacks-qt8ehebn.js.map +11 -0
- package/dist/shared/stacks-qvg7c24g.cjs +232 -0
- package/dist/shared/stacks-qvg7c24g.cjs.map +10 -0
- package/dist/shared/stacks-r1t6g3c3.cjs +241 -0
- package/dist/shared/stacks-r1t6g3c3.cjs.map +11 -0
- package/dist/shared/stacks-tjxmrssa.js +282 -0
- package/dist/shared/stacks-tjxmrssa.js.map +11 -0
- package/dist/shared/stacks-v9jqcpd4.js +18 -0
- package/dist/shared/stacks-v9jqcpd4.js.map +10 -0
- package/dist/shared/stacks-vdfag81a.cjs +121 -0
- package/dist/shared/stacks-vdfag81a.cjs.map +10 -0
- package/dist/shared/stacks-vdfs3e5v.js +134 -0
- package/dist/shared/stacks-vdfs3e5v.js.map +10 -0
- package/dist/shared/stacks-y3dh7hv1.js +60 -0
- package/dist/shared/stacks-y3dh7hv1.js.map +11 -0
- package/dist/shared/stacks-ya3x01yd.cjs +16 -0
- package/dist/shared/stacks-ya3x01yd.cjs.map +10 -0
- package/dist/shared/stacks-yfgsm6ap.js +599 -0
- package/dist/shared/stacks-yfgsm6ap.js.map +13 -0
- package/dist/shared/stacks-yfveb68e.js +121 -0
- package/dist/shared/stacks-yfveb68e.js.map +10 -0
- package/dist/shared/stacks-zm0b3tgy.js +160 -0
- package/dist/shared/stacks-zm0b3tgy.js.map +17 -0
- package/dist/shared/stacks-zyv9qqme.cjs +249 -0
- package/dist/shared/stacks-zyv9qqme.cjs.map +11 -0
- package/dist/src/accounts/index.cjs +90 -0
- package/dist/src/accounts/index.cjs.map +12 -0
- package/dist/src/accounts/index.d.cts +47 -0
- package/dist/src/accounts/index.d.ts +47 -0
- package/dist/src/accounts/index.js +104 -0
- package/dist/src/accounts/index.js.map +12 -0
- package/dist/src/actions/index.cjs +36 -0
- package/dist/src/actions/index.cjs.map +9 -0
- package/dist/src/actions/index.d.cts +886 -0
- package/dist/src/actions/index.d.ts +886 -0
- package/dist/src/actions/index.js +81 -0
- package/dist/src/actions/index.js.map +9 -0
- package/dist/src/bns/index.cjs +590 -0
- package/dist/src/bns/index.cjs.map +15 -0
- package/dist/src/bns/index.d.cts +241 -0
- package/dist/src/bns/index.d.ts +241 -0
- package/dist/src/bns/index.js +611 -0
- package/dist/src/bns/index.js.map +15 -0
- package/dist/src/chains/index.cjs +13 -0
- package/dist/src/chains/index.cjs.map +9 -0
- package/dist/src/chains/index.d.cts +53 -0
- package/dist/src/chains/index.d.ts +53 -0
- package/dist/src/chains/index.js +18 -0
- package/dist/src/chains/index.js.map +9 -0
- package/dist/src/clarity/index.cjs +3 -0
- package/dist/src/clarity/index.cjs.map +9 -0
- package/dist/src/clarity/index.d.cts +821 -0
- package/dist/src/clarity/index.d.ts +821 -0
- package/dist/src/clarity/index.js +145 -0
- package/dist/src/clarity/index.js.map +9 -0
- package/dist/src/connect/index.cjs +480 -0
- package/dist/src/connect/index.cjs.map +19 -0
- package/dist/src/connect/index.d.cts +110 -0
- package/dist/src/connect/index.d.ts +110 -0
- package/dist/src/connect/index.js +448 -0
- package/dist/src/connect/index.js.map +19 -0
- package/dist/src/connect/walletconnect/index.cjs +1322 -0
- package/dist/src/connect/walletconnect/index.cjs.map +18 -0
- package/dist/src/connect/walletconnect/index.d.cts +132 -0
- package/dist/src/connect/walletconnect/index.d.ts +132 -0
- package/dist/src/connect/walletconnect/index.js +1290 -0
- package/dist/src/connect/walletconnect/index.js.map +18 -0
- package/dist/src/index.cjs +557 -0
- package/dist/src/index.cjs.map +21 -0
- package/dist/src/index.d.cts +847 -0
- package/dist/src/index.d.ts +847 -0
- package/dist/src/index.js +629 -0
- package/dist/src/index.js.map +21 -0
- package/dist/src/postconditions/index.cjs +3 -0
- package/dist/src/postconditions/index.cjs.map +9 -0
- package/dist/src/postconditions/index.d.cts +91 -0
- package/dist/src/postconditions/index.d.ts +91 -0
- package/dist/src/postconditions/index.js +12 -0
- package/dist/src/postconditions/index.js.map +9 -0
- package/dist/src/pox/index.cjs +860 -0
- package/dist/src/pox/index.cjs.map +15 -0
- package/dist/src/pox/index.d.cts +244 -0
- package/dist/src/pox/index.d.ts +244 -0
- package/dist/src/pox/index.js +877 -0
- package/dist/src/pox/index.js.map +15 -0
- package/dist/src/stackingdao/index.cjs +300 -0
- package/dist/src/stackingdao/index.cjs.map +13 -0
- package/dist/src/stackingdao/index.d.cts +179 -0
- package/dist/src/stackingdao/index.d.ts +179 -0
- package/dist/src/stackingdao/index.js +318 -0
- package/dist/src/stackingdao/index.js.map +13 -0
- package/dist/src/subscriptions/index.cjs +14 -0
- package/dist/src/subscriptions/index.cjs.map +9 -0
- package/dist/src/subscriptions/index.d.cts +250 -0
- package/dist/src/subscriptions/index.d.ts +250 -0
- package/dist/src/subscriptions/index.js +21 -0
- package/dist/src/subscriptions/index.js.map +9 -0
- package/dist/src/transactions/index.cjs +50 -0
- package/dist/src/transactions/index.cjs.map +9 -0
- package/dist/src/transactions/index.d.cts +412 -0
- package/dist/src/transactions/index.d.ts +412 -0
- package/dist/src/transactions/index.js +101 -0
- package/dist/src/transactions/index.js.map +9 -0
- package/dist/src/utils/index.cjs +168 -0
- package/dist/src/utils/index.cjs.map +11 -0
- package/dist/src/utils/index.d.cts +140 -0
- package/dist/src/utils/index.d.ts +140 -0
- package/dist/src/utils/index.js +222 -0
- package/dist/src/utils/index.js.map +11 -0
- package/package.json +105 -0
|
@@ -0,0 +1,599 @@
|
|
|
1
|
+
import {
|
|
2
|
+
serializeCVBytes
|
|
3
|
+
} from "./stacks-8yykgdww.js";
|
|
4
|
+
import {
|
|
5
|
+
hash160,
|
|
6
|
+
hashP2PKH,
|
|
7
|
+
txidFromBytes
|
|
8
|
+
} from "./stacks-bycy6t9k.js";
|
|
9
|
+
import {
|
|
10
|
+
asciiToBytes,
|
|
11
|
+
bytesToHex,
|
|
12
|
+
c32address,
|
|
13
|
+
c32addressDecode,
|
|
14
|
+
concatBytes,
|
|
15
|
+
hexToBytes,
|
|
16
|
+
intToBytes,
|
|
17
|
+
intToHex,
|
|
18
|
+
utf8ToBytes,
|
|
19
|
+
writeUInt16BE,
|
|
20
|
+
writeUInt32BE,
|
|
21
|
+
writeUInt8
|
|
22
|
+
} from "./stacks-nqytwfcc.js";
|
|
23
|
+
|
|
24
|
+
// src/transactions/multisig.ts
|
|
25
|
+
import {
|
|
26
|
+
getPublicKey as nobleGetPublicKey2,
|
|
27
|
+
sign as nobleSign
|
|
28
|
+
} from "@noble/secp256k1";
|
|
29
|
+
|
|
30
|
+
// src/transactions/authorization.ts
|
|
31
|
+
import {
|
|
32
|
+
getPublicKey as nobleGetPublicKey,
|
|
33
|
+
sign,
|
|
34
|
+
etc
|
|
35
|
+
} from "@noble/secp256k1";
|
|
36
|
+
import { hmac } from "@noble/hashes/hmac.js";
|
|
37
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
38
|
+
|
|
39
|
+
// src/transactions/types.ts
|
|
40
|
+
var AuthType = {
|
|
41
|
+
Standard: 4,
|
|
42
|
+
Sponsored: 5
|
|
43
|
+
};
|
|
44
|
+
var PayloadType = {
|
|
45
|
+
TokenTransfer: 0,
|
|
46
|
+
SmartContract: 1,
|
|
47
|
+
ContractCall: 2,
|
|
48
|
+
PoisonMicroblock: 3,
|
|
49
|
+
Coinbase: 4,
|
|
50
|
+
CoinbaseToAltRecipient: 5,
|
|
51
|
+
VersionedSmartContract: 6,
|
|
52
|
+
TenureChange: 7,
|
|
53
|
+
NakamotoCoinbase: 8
|
|
54
|
+
};
|
|
55
|
+
var ClarityVersion = {
|
|
56
|
+
Clarity1: 1,
|
|
57
|
+
Clarity2: 2,
|
|
58
|
+
Clarity3: 3
|
|
59
|
+
};
|
|
60
|
+
var AnchorMode = {
|
|
61
|
+
OnChainOnly: 1,
|
|
62
|
+
OffChainOnly: 2,
|
|
63
|
+
Any: 3
|
|
64
|
+
};
|
|
65
|
+
var PostConditionModeWire = {
|
|
66
|
+
Allow: 1,
|
|
67
|
+
Deny: 2
|
|
68
|
+
};
|
|
69
|
+
var AddressHashMode = {
|
|
70
|
+
P2PKH: 0,
|
|
71
|
+
P2SH: 1,
|
|
72
|
+
P2WPKH: 2,
|
|
73
|
+
P2WSH: 3,
|
|
74
|
+
P2SH_NonSequential: 5,
|
|
75
|
+
P2WSH_P2SH_NonSequential: 7
|
|
76
|
+
};
|
|
77
|
+
var PubKeyEncoding = {
|
|
78
|
+
Compressed: 0,
|
|
79
|
+
Uncompressed: 1
|
|
80
|
+
};
|
|
81
|
+
var FungibleConditionCode = {
|
|
82
|
+
Equal: 1,
|
|
83
|
+
Greater: 2,
|
|
84
|
+
GreaterEqual: 3,
|
|
85
|
+
Less: 4,
|
|
86
|
+
LessEqual: 5
|
|
87
|
+
};
|
|
88
|
+
var NonFungibleConditionCode = {
|
|
89
|
+
Sends: 16,
|
|
90
|
+
DoesNotSend: 17
|
|
91
|
+
};
|
|
92
|
+
var PostConditionPrincipalId = {
|
|
93
|
+
Origin: 1,
|
|
94
|
+
Standard: 2,
|
|
95
|
+
Contract: 3
|
|
96
|
+
};
|
|
97
|
+
var AssetType = {
|
|
98
|
+
STX: 0,
|
|
99
|
+
Fungible: 1,
|
|
100
|
+
NonFungible: 2
|
|
101
|
+
};
|
|
102
|
+
var AuthFieldType = {
|
|
103
|
+
PublicKeyCompressed: 0,
|
|
104
|
+
PublicKeyUncompressed: 1,
|
|
105
|
+
SignatureCompressed: 2,
|
|
106
|
+
SignatureUncompressed: 3
|
|
107
|
+
};
|
|
108
|
+
var RECOVERABLE_ECDSA_SIG_LENGTH_BYTES = 65;
|
|
109
|
+
var MEMO_MAX_LENGTH_BYTES = 34;
|
|
110
|
+
var MAX_STRING_LENGTH_BYTES = 128;
|
|
111
|
+
|
|
112
|
+
// src/transactions/authorization.ts
|
|
113
|
+
etc.hmacSha256Sync = (key, ...msgs) => {
|
|
114
|
+
const h = hmac.create(sha256, key);
|
|
115
|
+
msgs.forEach((msg) => h.update(msg));
|
|
116
|
+
return h.digest();
|
|
117
|
+
};
|
|
118
|
+
var EMPTY_SIG = bytesToHex(new Uint8Array(RECOVERABLE_ECDSA_SIG_LENGTH_BYTES));
|
|
119
|
+
function createSingleSigSpendingCondition(publicKey, nonce, fee) {
|
|
120
|
+
const pubKeyBytes = hexToBytes(publicKey);
|
|
121
|
+
const signer = hashP2PKH(pubKeyBytes);
|
|
122
|
+
const isCompressed = pubKeyBytes.length === 33;
|
|
123
|
+
return {
|
|
124
|
+
hashMode: AddressHashMode.P2PKH,
|
|
125
|
+
signer,
|
|
126
|
+
nonce,
|
|
127
|
+
fee,
|
|
128
|
+
keyEncoding: isCompressed ? PubKeyEncoding.Compressed : PubKeyEncoding.Uncompressed,
|
|
129
|
+
signature: EMPTY_SIG
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
function createStandardAuth(spendingCondition) {
|
|
133
|
+
return { authType: AuthType.Standard, spendingCondition };
|
|
134
|
+
}
|
|
135
|
+
function createSponsoredAuth(spendingCondition, sponsorSpendingCondition) {
|
|
136
|
+
return {
|
|
137
|
+
authType: AuthType.Sponsored,
|
|
138
|
+
spendingCondition,
|
|
139
|
+
sponsorSpendingCondition: sponsorSpendingCondition ?? createSingleSigSpendingCondition("0".repeat(66), 0n, 0n)
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
function clearCondition(condition) {
|
|
143
|
+
if ("signature" in condition) {
|
|
144
|
+
return {
|
|
145
|
+
...condition,
|
|
146
|
+
nonce: 0n,
|
|
147
|
+
fee: 0n,
|
|
148
|
+
signature: EMPTY_SIG
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
return {
|
|
152
|
+
...condition,
|
|
153
|
+
nonce: 0n,
|
|
154
|
+
fee: 0n,
|
|
155
|
+
fields: []
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
function intoInitialSighashAuth(auth) {
|
|
159
|
+
if (auth.authType === AuthType.Standard) {
|
|
160
|
+
return createStandardAuth(clearCondition(auth.spendingCondition));
|
|
161
|
+
}
|
|
162
|
+
return createSponsoredAuth(clearCondition(auth.spendingCondition), clearCondition(createSingleSigSpendingCondition("0".repeat(66), 0n, 0n)));
|
|
163
|
+
}
|
|
164
|
+
function sigHashPreSign(curSigHash, authType, fee, nonce) {
|
|
165
|
+
const data = concatBytes(hexToBytes(curSigHash), new Uint8Array([authType]), intToBytes(fee, 8), intToBytes(nonce, 8));
|
|
166
|
+
return txidFromBytes(data);
|
|
167
|
+
}
|
|
168
|
+
function sigHashPostSign(curSigHash, pubKeyEncoding, signature) {
|
|
169
|
+
const data = concatBytes(hexToBytes(curSigHash), new Uint8Array([pubKeyEncoding]), hexToBytes(signature));
|
|
170
|
+
return txidFromBytes(data);
|
|
171
|
+
}
|
|
172
|
+
function signWithKey(privateKey, messageHash) {
|
|
173
|
+
const keyBytes = hexToBytes(privateKey).slice(0, 32);
|
|
174
|
+
const sig = sign(messageHash, keyBytes, { lowS: true });
|
|
175
|
+
const recoveryIdHex = intToHex(sig.recovery, 1);
|
|
176
|
+
return recoveryIdHex + sig.toCompactHex();
|
|
177
|
+
}
|
|
178
|
+
function nextSignature(curSigHash, authType, fee, nonce, privateKey) {
|
|
179
|
+
const sigHashPre = sigHashPreSign(curSigHash, authType, fee, nonce);
|
|
180
|
+
const keyBytes = hexToBytes(privateKey).slice(0, 32);
|
|
181
|
+
const pubKey = nobleGetPublicKey(keyBytes, true);
|
|
182
|
+
const isCompressed = pubKey.length === 33;
|
|
183
|
+
const pubKeyEncoding = isCompressed ? PubKeyEncoding.Compressed : PubKeyEncoding.Uncompressed;
|
|
184
|
+
const nextSig = signWithKey(privateKey, sigHashPre);
|
|
185
|
+
const nextSigHash = sigHashPostSign(sigHashPre, pubKeyEncoding, nextSig);
|
|
186
|
+
return { nextSig, nextSigHash };
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
// src/transactions/wire/serialize.ts
|
|
190
|
+
function serializeAddress(c32Address) {
|
|
191
|
+
const [version, hash1602] = c32addressDecode(c32Address);
|
|
192
|
+
return concatBytes(new Uint8Array([version]), hexToBytes(hash1602));
|
|
193
|
+
}
|
|
194
|
+
function serializeLPString(str, prefixBytes = 1) {
|
|
195
|
+
const content = utf8ToBytes(str);
|
|
196
|
+
const prefix = hexToBytes(intToHex(content.byteLength, prefixBytes));
|
|
197
|
+
return concatBytes(prefix, content);
|
|
198
|
+
}
|
|
199
|
+
function serializeLPStringLong(str) {
|
|
200
|
+
return serializeLPString(str, 4);
|
|
201
|
+
}
|
|
202
|
+
function serializeMemo(memo) {
|
|
203
|
+
const content = asciiToBytes(memo);
|
|
204
|
+
const padded = new Uint8Array(MEMO_MAX_LENGTH_BYTES);
|
|
205
|
+
padded.set(content.slice(0, MEMO_MAX_LENGTH_BYTES));
|
|
206
|
+
return concatBytes(writeUInt8(content.length), padded);
|
|
207
|
+
}
|
|
208
|
+
function serializeSpendingCondition(condition) {
|
|
209
|
+
const parts = [
|
|
210
|
+
writeUInt8(condition.hashMode),
|
|
211
|
+
hexToBytes(condition.signer),
|
|
212
|
+
intToBytes(condition.nonce, 8),
|
|
213
|
+
intToBytes(condition.fee, 8)
|
|
214
|
+
];
|
|
215
|
+
if ("signature" in condition) {
|
|
216
|
+
const sc = condition;
|
|
217
|
+
parts.push(writeUInt8(sc.keyEncoding));
|
|
218
|
+
parts.push(hexToBytes(sc.signature));
|
|
219
|
+
} else {
|
|
220
|
+
const mc = condition;
|
|
221
|
+
parts.push(writeUInt32BE(mc.fields.length));
|
|
222
|
+
for (const field of mc.fields) {
|
|
223
|
+
parts.push(serializeAuthField(field));
|
|
224
|
+
}
|
|
225
|
+
parts.push(writeUInt16BE(mc.signaturesRequired));
|
|
226
|
+
}
|
|
227
|
+
return concatBytes(...parts);
|
|
228
|
+
}
|
|
229
|
+
function serializeAuthField(field) {
|
|
230
|
+
if (field.type === "publicKey") {
|
|
231
|
+
const typeId = field.pubKeyEncoding === 0 ? 0 : 1;
|
|
232
|
+
return concatBytes(writeUInt8(typeId), hexToBytes(field.data));
|
|
233
|
+
} else {
|
|
234
|
+
const typeId = field.pubKeyEncoding === 0 ? 2 : 3;
|
|
235
|
+
return concatBytes(writeUInt8(typeId), hexToBytes(field.data));
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
function serializeAuthorization(auth) {
|
|
239
|
+
const parts = [writeUInt8(auth.authType)];
|
|
240
|
+
parts.push(serializeSpendingCondition(auth.spendingCondition));
|
|
241
|
+
if (auth.authType === AuthType.Sponsored) {
|
|
242
|
+
parts.push(serializeSpendingCondition(auth.sponsorSpendingCondition));
|
|
243
|
+
}
|
|
244
|
+
return concatBytes(...parts);
|
|
245
|
+
}
|
|
246
|
+
function serializePrincipal(principal) {
|
|
247
|
+
switch (principal.type) {
|
|
248
|
+
case "origin":
|
|
249
|
+
return writeUInt8(PostConditionPrincipalId.Origin);
|
|
250
|
+
case "standard":
|
|
251
|
+
return concatBytes(writeUInt8(PostConditionPrincipalId.Standard), serializeAddress(principal.address));
|
|
252
|
+
case "contract":
|
|
253
|
+
return concatBytes(writeUInt8(PostConditionPrincipalId.Contract), serializeAddress(principal.address), serializeLPString(principal.contractName));
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
function serializeAssetInfo(asset) {
|
|
257
|
+
return concatBytes(serializeAddress(asset.address), serializeLPString(asset.contractName), serializeLPString(asset.assetName));
|
|
258
|
+
}
|
|
259
|
+
function serializePostCondition(pc) {
|
|
260
|
+
const parts = [serializePrincipal(pc.principal)];
|
|
261
|
+
switch (pc.type) {
|
|
262
|
+
case "stx":
|
|
263
|
+
parts.push(writeUInt8(AssetType.STX));
|
|
264
|
+
parts.push(writeUInt8(pc.conditionCode));
|
|
265
|
+
parts.push(intToBytes(pc.amount, 8));
|
|
266
|
+
break;
|
|
267
|
+
case "ft":
|
|
268
|
+
parts.push(writeUInt8(AssetType.Fungible));
|
|
269
|
+
parts.push(serializeAssetInfo(pc.asset));
|
|
270
|
+
parts.push(writeUInt8(pc.conditionCode));
|
|
271
|
+
parts.push(intToBytes(pc.amount, 8));
|
|
272
|
+
break;
|
|
273
|
+
case "nft":
|
|
274
|
+
parts.push(writeUInt8(AssetType.NonFungible));
|
|
275
|
+
parts.push(serializeAssetInfo(pc.asset));
|
|
276
|
+
parts.push(serializeCVBytes(pc.assetId));
|
|
277
|
+
parts.push(writeUInt8(pc.conditionCode));
|
|
278
|
+
break;
|
|
279
|
+
}
|
|
280
|
+
return concatBytes(...parts);
|
|
281
|
+
}
|
|
282
|
+
function serializePostConditions(pcs) {
|
|
283
|
+
const parts = [writeUInt32BE(pcs.length)];
|
|
284
|
+
for (const pc of pcs) {
|
|
285
|
+
parts.push(serializePostCondition(pc));
|
|
286
|
+
}
|
|
287
|
+
return concatBytes(...parts);
|
|
288
|
+
}
|
|
289
|
+
function serializePayload(payload) {
|
|
290
|
+
const parts = [writeUInt8(payload.payloadType)];
|
|
291
|
+
switch (payload.payloadType) {
|
|
292
|
+
case PayloadType.TokenTransfer:
|
|
293
|
+
parts.push(serializeCVBytes(payload.recipient));
|
|
294
|
+
parts.push(intToBytes(payload.amount, 8));
|
|
295
|
+
parts.push(serializeMemo(payload.memo));
|
|
296
|
+
break;
|
|
297
|
+
case PayloadType.ContractCall:
|
|
298
|
+
parts.push(serializeAddress(payload.contractAddress));
|
|
299
|
+
parts.push(serializeLPString(payload.contractName));
|
|
300
|
+
parts.push(serializeLPString(payload.functionName));
|
|
301
|
+
parts.push(writeUInt32BE(payload.functionArgs.length));
|
|
302
|
+
for (const arg of payload.functionArgs) {
|
|
303
|
+
parts.push(serializeCVBytes(arg));
|
|
304
|
+
}
|
|
305
|
+
break;
|
|
306
|
+
case PayloadType.SmartContract:
|
|
307
|
+
parts.push(serializeLPString(payload.contractName));
|
|
308
|
+
parts.push(serializeLPStringLong(payload.codeBody));
|
|
309
|
+
break;
|
|
310
|
+
case PayloadType.VersionedSmartContract:
|
|
311
|
+
parts.push(writeUInt8(payload.clarityVersion ?? 2));
|
|
312
|
+
parts.push(serializeLPString(payload.contractName));
|
|
313
|
+
parts.push(serializeLPStringLong(payload.codeBody));
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
return concatBytes(...parts);
|
|
317
|
+
}
|
|
318
|
+
function serializeTransaction(tx) {
|
|
319
|
+
return concatBytes(writeUInt8(tx.version), writeUInt32BE(tx.chainId), serializeAuthorization(tx.auth), writeUInt8(tx.anchorMode), writeUInt8(tx.postConditionMode), serializePostConditions(tx.postConditions), serializePayload(tx.payload));
|
|
320
|
+
}
|
|
321
|
+
function serializeTransactionHex(tx) {
|
|
322
|
+
return bytesToHex(serializeTransaction(tx));
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// src/transactions/multisig.ts
|
|
326
|
+
var OP_CHECKMULTISIG = 174;
|
|
327
|
+
function isNonSequential(hashMode) {
|
|
328
|
+
return (hashMode & 4) !== 0;
|
|
329
|
+
}
|
|
330
|
+
function makeRedeemScript(publicKeys, signaturesRequired) {
|
|
331
|
+
const parts = [];
|
|
332
|
+
parts.push(new Uint8Array([80 + signaturesRequired]));
|
|
333
|
+
for (const pk of publicKeys) {
|
|
334
|
+
const pkBytes = hexToBytes(pk);
|
|
335
|
+
parts.push(new Uint8Array([pkBytes.length]));
|
|
336
|
+
parts.push(pkBytes);
|
|
337
|
+
}
|
|
338
|
+
parts.push(new Uint8Array([80 + publicKeys.length]));
|
|
339
|
+
parts.push(new Uint8Array([OP_CHECKMULTISIG]));
|
|
340
|
+
return concatBytes(...parts);
|
|
341
|
+
}
|
|
342
|
+
function makeMultiSigAddress(publicKeys, signaturesRequired, chain) {
|
|
343
|
+
if (signaturesRequired < 1 || signaturesRequired > publicKeys.length) {
|
|
344
|
+
throw new Error(`signaturesRequired must be between 1 and ${publicKeys.length}`);
|
|
345
|
+
}
|
|
346
|
+
const redeemScript = makeRedeemScript(publicKeys, signaturesRequired);
|
|
347
|
+
const signer = hash160(redeemScript);
|
|
348
|
+
const version = chain?.addressVersion?.multiSig ?? 20;
|
|
349
|
+
return c32address(version, bytesToHex(signer));
|
|
350
|
+
}
|
|
351
|
+
function createMultiSigSpendingCondition(publicKeys, signaturesRequired, nonce, fee, hashMode = AddressHashMode.P2SH) {
|
|
352
|
+
const redeemScript = makeRedeemScript(publicKeys, signaturesRequired);
|
|
353
|
+
const signer = bytesToHex(hash160(redeemScript));
|
|
354
|
+
return {
|
|
355
|
+
hashMode,
|
|
356
|
+
signer,
|
|
357
|
+
nonce,
|
|
358
|
+
fee,
|
|
359
|
+
fields: [],
|
|
360
|
+
signaturesRequired
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
function initialSigHash(tx) {
|
|
364
|
+
const cleared = {
|
|
365
|
+
...tx,
|
|
366
|
+
auth: intoInitialSighashAuth(tx.auth)
|
|
367
|
+
};
|
|
368
|
+
return txidFromBytes(serializeTransaction(cleared));
|
|
369
|
+
}
|
|
370
|
+
function nonSequentialPreSignHash(tx) {
|
|
371
|
+
const condition = tx.auth.spendingCondition;
|
|
372
|
+
const initHash = initialSigHash(tx);
|
|
373
|
+
return sigHashPreSign(initHash, AuthType.Standard, condition.fee, condition.nonce);
|
|
374
|
+
}
|
|
375
|
+
function replayMultiSigSigHash(tx) {
|
|
376
|
+
const condition = tx.auth.spendingCondition;
|
|
377
|
+
if (isNonSequential(condition.hashMode)) {
|
|
378
|
+
const preSign = nonSequentialPreSignHash(tx);
|
|
379
|
+
let curSigHash2 = preSign;
|
|
380
|
+
for (const field of condition.fields) {
|
|
381
|
+
curSigHash2 = sigHashPostSign(curSigHash2, field.pubKeyEncoding, field.data);
|
|
382
|
+
}
|
|
383
|
+
return curSigHash2;
|
|
384
|
+
}
|
|
385
|
+
let curSigHash = initialSigHash(tx);
|
|
386
|
+
for (const field of condition.fields) {
|
|
387
|
+
const preSign = sigHashPreSign(curSigHash, AuthType.Standard, condition.fee, condition.nonce);
|
|
388
|
+
curSigHash = sigHashPostSign(preSign, field.pubKeyEncoding, field.data);
|
|
389
|
+
}
|
|
390
|
+
return curSigHash;
|
|
391
|
+
}
|
|
392
|
+
function signMultiSig(tx, privateKey, publicKeys) {
|
|
393
|
+
const keyBytes = hexToBytes(privateKey).slice(0, 32);
|
|
394
|
+
const signerPubKey = bytesToHex(nobleGetPublicKey2(keyBytes, true));
|
|
395
|
+
const signerIndex = publicKeys.findIndex((pk) => pk === signerPubKey);
|
|
396
|
+
if (signerIndex === -1) {
|
|
397
|
+
throw new Error("Private key does not correspond to any signer in the multi-sig");
|
|
398
|
+
}
|
|
399
|
+
const condition = { ...tx.auth.spendingCondition };
|
|
400
|
+
if (isNonSequential(condition.hashMode)) {
|
|
401
|
+
return signNonSequential(tx, condition, privateKey);
|
|
402
|
+
}
|
|
403
|
+
return signSequential(tx, condition, privateKey, publicKeys, signerIndex);
|
|
404
|
+
}
|
|
405
|
+
function signNonSequential(tx, condition, privateKey) {
|
|
406
|
+
const fields = [...condition.fields];
|
|
407
|
+
const preSignHash = nonSequentialPreSignHash(tx);
|
|
408
|
+
const keyBytes = hexToBytes(privateKey).slice(0, 32);
|
|
409
|
+
const sig = nobleSign(preSignHash, keyBytes, { lowS: true });
|
|
410
|
+
const recoveryIdHex = intToHex(sig.recovery, 1);
|
|
411
|
+
const nextSig = recoveryIdHex + sig.toCompactHex();
|
|
412
|
+
fields.push({
|
|
413
|
+
type: "signature",
|
|
414
|
+
pubKeyEncoding: PubKeyEncoding.Compressed,
|
|
415
|
+
data: nextSig
|
|
416
|
+
});
|
|
417
|
+
const newCondition = { ...condition, fields };
|
|
418
|
+
const sigCount = fields.filter((f) => f.type === "signature").length;
|
|
419
|
+
if (sigCount >= condition.signaturesRequired) {
|
|
420
|
+
return {
|
|
421
|
+
...tx,
|
|
422
|
+
auth: { ...tx.auth, spendingCondition: newCondition }
|
|
423
|
+
};
|
|
424
|
+
}
|
|
425
|
+
return {
|
|
426
|
+
...tx,
|
|
427
|
+
auth: { ...tx.auth, spendingCondition: newCondition }
|
|
428
|
+
};
|
|
429
|
+
}
|
|
430
|
+
function signSequential(tx, condition, privateKey, publicKeys, signerIndex) {
|
|
431
|
+
const fields = [...condition.fields];
|
|
432
|
+
let curSigHash = initialSigHash(tx);
|
|
433
|
+
for (const field of fields) {
|
|
434
|
+
const preSign = sigHashPreSign(curSigHash, AuthType.Standard, condition.fee, condition.nonce);
|
|
435
|
+
curSigHash = sigHashPostSign(preSign, field.pubKeyEncoding, field.data);
|
|
436
|
+
}
|
|
437
|
+
for (let i = fields.length;i < signerIndex; i++) {
|
|
438
|
+
const pubKeyField = {
|
|
439
|
+
type: "publicKey",
|
|
440
|
+
pubKeyEncoding: PubKeyEncoding.Compressed,
|
|
441
|
+
data: publicKeys[i]
|
|
442
|
+
};
|
|
443
|
+
fields.push(pubKeyField);
|
|
444
|
+
const preSign = sigHashPreSign(curSigHash, AuthType.Standard, condition.fee, condition.nonce);
|
|
445
|
+
curSigHash = sigHashPostSign(preSign, pubKeyField.pubKeyEncoding, pubKeyField.data);
|
|
446
|
+
}
|
|
447
|
+
const { nextSig, nextSigHash } = nextSignature(curSigHash, AuthType.Standard, condition.fee, condition.nonce, privateKey);
|
|
448
|
+
fields.push({
|
|
449
|
+
type: "signature",
|
|
450
|
+
pubKeyEncoding: PubKeyEncoding.Compressed,
|
|
451
|
+
data: nextSig
|
|
452
|
+
});
|
|
453
|
+
curSigHash = nextSigHash;
|
|
454
|
+
const newCondition = { ...condition, fields };
|
|
455
|
+
const sigCount = fields.filter((f) => f.type === "signature").length;
|
|
456
|
+
let finalCondition = newCondition;
|
|
457
|
+
if (sigCount >= condition.signaturesRequired) {
|
|
458
|
+
finalCondition = finalizeMultiSigCondition(newCondition, publicKeys, curSigHash);
|
|
459
|
+
}
|
|
460
|
+
return {
|
|
461
|
+
...tx,
|
|
462
|
+
auth: {
|
|
463
|
+
...tx.auth,
|
|
464
|
+
spendingCondition: finalCondition
|
|
465
|
+
}
|
|
466
|
+
};
|
|
467
|
+
}
|
|
468
|
+
async function signMultiSigWithAccount(tx, account, publicKeys) {
|
|
469
|
+
const signerPubKey = account.publicKey;
|
|
470
|
+
const signerIndex = publicKeys.findIndex((pk) => pk === signerPubKey);
|
|
471
|
+
if (signerIndex === -1) {
|
|
472
|
+
throw new Error("Account does not correspond to any signer in the multi-sig");
|
|
473
|
+
}
|
|
474
|
+
const condition = { ...tx.auth.spendingCondition };
|
|
475
|
+
if (isNonSequential(condition.hashMode)) {
|
|
476
|
+
return signNonSequentialWithAccount(tx, condition, account);
|
|
477
|
+
}
|
|
478
|
+
return signSequentialWithAccount(tx, condition, account, publicKeys, signerIndex);
|
|
479
|
+
}
|
|
480
|
+
async function signNonSequentialWithAccount(tx, condition, account) {
|
|
481
|
+
const fields = [...condition.fields];
|
|
482
|
+
const preSignHash = nonSequentialPreSignHash(tx);
|
|
483
|
+
const sigBytes = await account.sign(hexToBytes(preSignHash));
|
|
484
|
+
const nextSig = bytesToHex(sigBytes);
|
|
485
|
+
fields.push({
|
|
486
|
+
type: "signature",
|
|
487
|
+
pubKeyEncoding: PubKeyEncoding.Compressed,
|
|
488
|
+
data: nextSig
|
|
489
|
+
});
|
|
490
|
+
const newCondition = { ...condition, fields };
|
|
491
|
+
return {
|
|
492
|
+
...tx,
|
|
493
|
+
auth: { ...tx.auth, spendingCondition: newCondition }
|
|
494
|
+
};
|
|
495
|
+
}
|
|
496
|
+
async function signSequentialWithAccount(tx, condition, account, publicKeys, signerIndex) {
|
|
497
|
+
const fields = [...condition.fields];
|
|
498
|
+
let curSigHash = initialSigHash(tx);
|
|
499
|
+
for (const field of fields) {
|
|
500
|
+
const preSign2 = sigHashPreSign(curSigHash, AuthType.Standard, condition.fee, condition.nonce);
|
|
501
|
+
curSigHash = sigHashPostSign(preSign2, field.pubKeyEncoding, field.data);
|
|
502
|
+
}
|
|
503
|
+
for (let i = fields.length;i < signerIndex; i++) {
|
|
504
|
+
const pubKeyField = {
|
|
505
|
+
type: "publicKey",
|
|
506
|
+
pubKeyEncoding: PubKeyEncoding.Compressed,
|
|
507
|
+
data: publicKeys[i]
|
|
508
|
+
};
|
|
509
|
+
fields.push(pubKeyField);
|
|
510
|
+
const preSign2 = sigHashPreSign(curSigHash, AuthType.Standard, condition.fee, condition.nonce);
|
|
511
|
+
curSigHash = sigHashPostSign(preSign2, pubKeyField.pubKeyEncoding, pubKeyField.data);
|
|
512
|
+
}
|
|
513
|
+
const preSign = sigHashPreSign(curSigHash, AuthType.Standard, condition.fee, condition.nonce);
|
|
514
|
+
const sigBytes = await account.sign(hexToBytes(preSign));
|
|
515
|
+
const nextSig = bytesToHex(sigBytes);
|
|
516
|
+
const nextSigHash = sigHashPostSign(preSign, PubKeyEncoding.Compressed, nextSig);
|
|
517
|
+
fields.push({
|
|
518
|
+
type: "signature",
|
|
519
|
+
pubKeyEncoding: PubKeyEncoding.Compressed,
|
|
520
|
+
data: nextSig
|
|
521
|
+
});
|
|
522
|
+
curSigHash = nextSigHash;
|
|
523
|
+
const newCondition = { ...condition, fields };
|
|
524
|
+
const sigCount = fields.filter((f) => f.type === "signature").length;
|
|
525
|
+
let finalCondition = newCondition;
|
|
526
|
+
if (sigCount >= condition.signaturesRequired) {
|
|
527
|
+
finalCondition = finalizeMultiSigCondition(newCondition, publicKeys, curSigHash);
|
|
528
|
+
}
|
|
529
|
+
return {
|
|
530
|
+
...tx,
|
|
531
|
+
auth: {
|
|
532
|
+
...tx.auth,
|
|
533
|
+
spendingCondition: finalCondition
|
|
534
|
+
}
|
|
535
|
+
};
|
|
536
|
+
}
|
|
537
|
+
function finalizeMultiSig(tx, publicKeys) {
|
|
538
|
+
const condition = tx.auth.spendingCondition;
|
|
539
|
+
if (isNonSequential(condition.hashMode)) {
|
|
540
|
+
return tx;
|
|
541
|
+
}
|
|
542
|
+
const curSigHash = replayMultiSigSigHash(tx);
|
|
543
|
+
const finalCondition = finalizeMultiSigCondition(condition, publicKeys, curSigHash);
|
|
544
|
+
return {
|
|
545
|
+
...tx,
|
|
546
|
+
auth: {
|
|
547
|
+
...tx.auth,
|
|
548
|
+
spendingCondition: finalCondition
|
|
549
|
+
}
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
function finalizeMultiSigCondition(condition, publicKeys, curSigHash) {
|
|
553
|
+
const fields = [...condition.fields];
|
|
554
|
+
let sigHash = curSigHash;
|
|
555
|
+
for (let i = fields.length;i < publicKeys.length; i++) {
|
|
556
|
+
const pubKeyField = {
|
|
557
|
+
type: "publicKey",
|
|
558
|
+
pubKeyEncoding: PubKeyEncoding.Compressed,
|
|
559
|
+
data: publicKeys[i]
|
|
560
|
+
};
|
|
561
|
+
fields.push(pubKeyField);
|
|
562
|
+
const preSign = sigHashPreSign(sigHash, AuthType.Standard, condition.fee, condition.nonce);
|
|
563
|
+
sigHash = sigHashPostSign(preSign, pubKeyField.pubKeyEncoding, pubKeyField.data);
|
|
564
|
+
}
|
|
565
|
+
return { ...condition, fields };
|
|
566
|
+
}
|
|
567
|
+
function combineMultiSigSignatures(baseTx, signedTxs) {
|
|
568
|
+
const condition = baseTx.auth.spendingCondition;
|
|
569
|
+
if (!isNonSequential(condition.hashMode)) {
|
|
570
|
+
throw new Error("combineMultiSigSignatures only works with non-sequential hash modes (SIP-027)");
|
|
571
|
+
}
|
|
572
|
+
const seenSigs = new Set;
|
|
573
|
+
const signatures = [];
|
|
574
|
+
for (const stx of signedTxs) {
|
|
575
|
+
const stxCondition = stx.auth.spendingCondition;
|
|
576
|
+
for (const field of stxCondition.fields) {
|
|
577
|
+
if (field.type === "signature" && !seenSigs.has(field.data)) {
|
|
578
|
+
seenSigs.add(field.data);
|
|
579
|
+
signatures.push(field);
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
if (signatures.length === 0) {
|
|
584
|
+
throw new Error("No signatures found in the provided transactions");
|
|
585
|
+
}
|
|
586
|
+
const newCondition = {
|
|
587
|
+
...condition,
|
|
588
|
+
fields: signatures
|
|
589
|
+
};
|
|
590
|
+
return {
|
|
591
|
+
...baseTx,
|
|
592
|
+
auth: { ...baseTx.auth, spendingCondition: newCondition }
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
export { AuthType, PayloadType, ClarityVersion, AnchorMode, PostConditionModeWire, AddressHashMode, PubKeyEncoding, FungibleConditionCode, NonFungibleConditionCode, PostConditionPrincipalId, AssetType, AuthFieldType, RECOVERABLE_ECDSA_SIG_LENGTH_BYTES, MEMO_MAX_LENGTH_BYTES, MAX_STRING_LENGTH_BYTES, serializePayload, serializeTransaction, serializeTransactionHex, createSingleSigSpendingCondition, createStandardAuth, createSponsoredAuth, intoInitialSighashAuth, sigHashPreSign, sigHashPostSign, nextSignature, isNonSequential, makeMultiSigAddress, createMultiSigSpendingCondition, replayMultiSigSigHash, signMultiSig, signMultiSigWithAccount, finalizeMultiSig, combineMultiSigSignatures };
|
|
597
|
+
|
|
598
|
+
//# debugId=99F694C3EE6B35BA64756E2164756E21
|
|
599
|
+
//# sourceMappingURL=stacks-yfgsm6ap.js.map
|