@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,249 @@
|
|
|
1
|
+
// src/transactions/signer.ts
|
|
2
|
+
function txid(tx) {
|
|
3
|
+
return txidFromBytes(serializeTransaction(tx));
|
|
4
|
+
}
|
|
5
|
+
function signBegin(tx) {
|
|
6
|
+
const cleared = {
|
|
7
|
+
...tx,
|
|
8
|
+
auth: intoInitialSighashAuth(tx.auth)
|
|
9
|
+
};
|
|
10
|
+
return txid(cleared);
|
|
11
|
+
}
|
|
12
|
+
function signTransaction(tx, privateKey) {
|
|
13
|
+
const sigHash = signBegin(tx);
|
|
14
|
+
const condition = tx.auth.spendingCondition;
|
|
15
|
+
const { nextSig, nextSigHash } = nextSignature(sigHash, AuthType.Standard, condition.fee, condition.nonce, privateKey);
|
|
16
|
+
return {
|
|
17
|
+
...tx,
|
|
18
|
+
auth: {
|
|
19
|
+
...tx.auth,
|
|
20
|
+
spendingCondition: {
|
|
21
|
+
...condition,
|
|
22
|
+
signature: nextSig
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
async function signTransactionWithAccount(tx, account) {
|
|
28
|
+
const sigHash = signBegin(tx);
|
|
29
|
+
const condition = tx.auth.spendingCondition;
|
|
30
|
+
const sigHashPre = sigHashPreSign(sigHash, AuthType.Standard, condition.fee, condition.nonce);
|
|
31
|
+
const sigBytes = await account.sign(hexToBytes(sigHashPre));
|
|
32
|
+
const nextSig = bytesToHex(sigBytes);
|
|
33
|
+
const pubKeyBytes = hexToBytes(account.publicKey);
|
|
34
|
+
const pubKeyEncoding = pubKeyBytes.length === 33 ? PubKeyEncoding.Compressed : PubKeyEncoding.Uncompressed;
|
|
35
|
+
return {
|
|
36
|
+
...tx,
|
|
37
|
+
auth: {
|
|
38
|
+
...tx.auth,
|
|
39
|
+
spendingCondition: {
|
|
40
|
+
...condition,
|
|
41
|
+
signature: nextSig
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
function getTransactionId(tx) {
|
|
47
|
+
return txid(tx);
|
|
48
|
+
}
|
|
49
|
+
function getOriginSigHash(tx) {
|
|
50
|
+
const condition = tx.auth.spendingCondition;
|
|
51
|
+
if ("fields" in condition) {
|
|
52
|
+
return replayMultiSigSigHash(tx);
|
|
53
|
+
}
|
|
54
|
+
const initialSigHash = signBegin(tx);
|
|
55
|
+
const sc = condition;
|
|
56
|
+
const preSign = sigHashPreSign(initialSigHash, AuthType.Standard, sc.fee, sc.nonce);
|
|
57
|
+
return sigHashPostSign(preSign, sc.keyEncoding, sc.signature);
|
|
58
|
+
}
|
|
59
|
+
function signSponsor(tx, privateKey) {
|
|
60
|
+
if (tx.auth.authType !== AuthType.Sponsored) {
|
|
61
|
+
throw new Error("Transaction must be sponsored");
|
|
62
|
+
}
|
|
63
|
+
const auth = tx.auth;
|
|
64
|
+
const sponsorCondition = auth.sponsorSpendingCondition;
|
|
65
|
+
const originSigHash = getOriginSigHash(tx);
|
|
66
|
+
const { nextSig } = nextSignature(originSigHash, AuthType.Sponsored, sponsorCondition.fee, sponsorCondition.nonce, privateKey);
|
|
67
|
+
return {
|
|
68
|
+
...tx,
|
|
69
|
+
auth: {
|
|
70
|
+
...auth,
|
|
71
|
+
sponsorSpendingCondition: {
|
|
72
|
+
...sponsorCondition,
|
|
73
|
+
signature: nextSig
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
async function signSponsorWithAccount(tx, account) {
|
|
79
|
+
if (tx.auth.authType !== AuthType.Sponsored) {
|
|
80
|
+
throw new Error("Transaction must be sponsored");
|
|
81
|
+
}
|
|
82
|
+
const auth = tx.auth;
|
|
83
|
+
const sponsorCondition = auth.sponsorSpendingCondition;
|
|
84
|
+
const originSigHash = getOriginSigHash(tx);
|
|
85
|
+
const sigHashPre = sigHashPreSign(originSigHash, AuthType.Sponsored, sponsorCondition.fee, sponsorCondition.nonce);
|
|
86
|
+
const sigBytes = await account.sign(hexToBytes(sigHashPre));
|
|
87
|
+
const nextSig = bytesToHex(sigBytes);
|
|
88
|
+
return {
|
|
89
|
+
...tx,
|
|
90
|
+
auth: {
|
|
91
|
+
...auth,
|
|
92
|
+
sponsorSpendingCondition: {
|
|
93
|
+
...sponsorCondition,
|
|
94
|
+
signature: nextSig
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// src/transactions/build.ts
|
|
101
|
+
function resolvePcMode(mode) {
|
|
102
|
+
return mode === "allow" ? PostConditionModeWire.Allow : PostConditionModeWire.Deny;
|
|
103
|
+
}
|
|
104
|
+
function convertPostConditions(pcs) {
|
|
105
|
+
if (!pcs || pcs.length === 0)
|
|
106
|
+
return [];
|
|
107
|
+
return pcs.map(convertPostCondition);
|
|
108
|
+
}
|
|
109
|
+
function resolvePrincipal(address) {
|
|
110
|
+
if (address === "origin")
|
|
111
|
+
return { type: "origin" };
|
|
112
|
+
const [addr, name] = address.split(".");
|
|
113
|
+
if (name) {
|
|
114
|
+
return { type: "contract", address: addr, contractName: name };
|
|
115
|
+
}
|
|
116
|
+
return { type: "standard", address: addr };
|
|
117
|
+
}
|
|
118
|
+
var FUNGIBLE_CODE_MAP = {
|
|
119
|
+
eq: FungibleConditionCode.Equal,
|
|
120
|
+
gt: FungibleConditionCode.Greater,
|
|
121
|
+
gte: FungibleConditionCode.GreaterEqual,
|
|
122
|
+
lt: FungibleConditionCode.Less,
|
|
123
|
+
lte: FungibleConditionCode.LessEqual
|
|
124
|
+
};
|
|
125
|
+
function convertPostCondition(pc) {
|
|
126
|
+
switch (pc.type) {
|
|
127
|
+
case "stx-postcondition":
|
|
128
|
+
return {
|
|
129
|
+
type: "stx",
|
|
130
|
+
principal: resolvePrincipal(pc.address),
|
|
131
|
+
conditionCode: FUNGIBLE_CODE_MAP[pc.condition],
|
|
132
|
+
amount: intToBigInt(pc.amount)
|
|
133
|
+
};
|
|
134
|
+
case "ft-postcondition": {
|
|
135
|
+
const [contractId, tokenName] = pc.asset.split("::");
|
|
136
|
+
const [addr, name] = parseContractId(contractId);
|
|
137
|
+
return {
|
|
138
|
+
type: "ft",
|
|
139
|
+
principal: resolvePrincipal(pc.address),
|
|
140
|
+
asset: { address: addr, contractName: name, assetName: tokenName },
|
|
141
|
+
conditionCode: FUNGIBLE_CODE_MAP[pc.condition],
|
|
142
|
+
amount: intToBigInt(pc.amount)
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
case "nft-postcondition": {
|
|
146
|
+
const [contractId, tokenName] = pc.asset.split("::");
|
|
147
|
+
const [addr, name] = parseContractId(contractId);
|
|
148
|
+
return {
|
|
149
|
+
type: "nft",
|
|
150
|
+
principal: resolvePrincipal(pc.address),
|
|
151
|
+
asset: { address: addr, contractName: name, assetName: tokenName },
|
|
152
|
+
conditionCode: pc.condition === "sent" ? NonFungibleConditionCode.Sends : NonFungibleConditionCode.DoesNotSend,
|
|
153
|
+
assetId: pc.assetId
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
function resolveSpendingCondition(opts, nonce, fee) {
|
|
159
|
+
if (opts.publicKeys) {
|
|
160
|
+
return createMultiSigSpendingCondition(opts.publicKeys, opts.signaturesRequired, nonce, fee, opts.hashMode);
|
|
161
|
+
}
|
|
162
|
+
return createSingleSigSpendingCondition(opts.publicKey, nonce, fee);
|
|
163
|
+
}
|
|
164
|
+
function resolveAuth(spendingCondition, sponsored) {
|
|
165
|
+
return sponsored ? createSponsoredAuth(spendingCondition) : createStandardAuth(spendingCondition);
|
|
166
|
+
}
|
|
167
|
+
function resolveVersionAndChainId(chain) {
|
|
168
|
+
if (!chain)
|
|
169
|
+
return { version: 0, chainId: 1 };
|
|
170
|
+
return { version: chain.transactionVersion, chainId: chain.id };
|
|
171
|
+
}
|
|
172
|
+
function buildTokenTransfer(options) {
|
|
173
|
+
const { version, chainId } = resolveVersionAndChainId(options.chain);
|
|
174
|
+
const fee = intToBigInt(options.fee);
|
|
175
|
+
const nonce = intToBigInt(options.nonce);
|
|
176
|
+
const spendingCondition = resolveSpendingCondition(options, nonce, fee);
|
|
177
|
+
const recipient = Cl.principal(options.recipient);
|
|
178
|
+
const tx = {
|
|
179
|
+
version,
|
|
180
|
+
chainId,
|
|
181
|
+
auth: resolveAuth(spendingCondition, options.sponsored),
|
|
182
|
+
anchorMode: AnchorMode.Any,
|
|
183
|
+
postConditionMode: resolvePcMode(options.postConditionMode),
|
|
184
|
+
postConditions: convertPostConditions(options.postConditions),
|
|
185
|
+
payload: {
|
|
186
|
+
payloadType: PayloadType.TokenTransfer,
|
|
187
|
+
recipient,
|
|
188
|
+
amount: intToBigInt(options.amount),
|
|
189
|
+
memo: options.memo ?? ""
|
|
190
|
+
}
|
|
191
|
+
};
|
|
192
|
+
if (options.publicKeys) {
|
|
193
|
+
tx._multisig = { publicKeys: options.publicKeys };
|
|
194
|
+
}
|
|
195
|
+
return tx;
|
|
196
|
+
}
|
|
197
|
+
function buildContractCall(options) {
|
|
198
|
+
const { version, chainId } = resolveVersionAndChainId(options.chain);
|
|
199
|
+
const fee = intToBigInt(options.fee);
|
|
200
|
+
const nonce = intToBigInt(options.nonce);
|
|
201
|
+
const spendingCondition = resolveSpendingCondition(options, nonce, fee);
|
|
202
|
+
const tx = {
|
|
203
|
+
version,
|
|
204
|
+
chainId,
|
|
205
|
+
auth: resolveAuth(spendingCondition, options.sponsored),
|
|
206
|
+
anchorMode: AnchorMode.Any,
|
|
207
|
+
postConditionMode: resolvePcMode(options.postConditionMode),
|
|
208
|
+
postConditions: convertPostConditions(options.postConditions),
|
|
209
|
+
payload: {
|
|
210
|
+
payloadType: PayloadType.ContractCall,
|
|
211
|
+
contractAddress: options.contractAddress,
|
|
212
|
+
contractName: options.contractName,
|
|
213
|
+
functionName: options.functionName,
|
|
214
|
+
functionArgs: options.functionArgs
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
if (options.publicKeys) {
|
|
218
|
+
tx._multisig = { publicKeys: options.publicKeys };
|
|
219
|
+
}
|
|
220
|
+
return tx;
|
|
221
|
+
}
|
|
222
|
+
function buildContractDeploy(options) {
|
|
223
|
+
const { version, chainId } = resolveVersionAndChainId(options.chain);
|
|
224
|
+
const fee = intToBigInt(options.fee);
|
|
225
|
+
const nonce = intToBigInt(options.nonce);
|
|
226
|
+
const spendingCondition = resolveSpendingCondition(options, nonce, fee);
|
|
227
|
+
const useVersioned = options.clarityVersion !== undefined;
|
|
228
|
+
const tx = {
|
|
229
|
+
version,
|
|
230
|
+
chainId,
|
|
231
|
+
auth: resolveAuth(spendingCondition, options.sponsored),
|
|
232
|
+
anchorMode: AnchorMode.Any,
|
|
233
|
+
postConditionMode: resolvePcMode(options.postConditionMode),
|
|
234
|
+
postConditions: convertPostConditions(options.postConditions),
|
|
235
|
+
payload: {
|
|
236
|
+
payloadType: useVersioned ? PayloadType.VersionedSmartContract : PayloadType.SmartContract,
|
|
237
|
+
clarityVersion: options.clarityVersion,
|
|
238
|
+
contractName: options.contractName,
|
|
239
|
+
codeBody: options.codeBody
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
if (options.publicKeys) {
|
|
243
|
+
tx._multisig = { publicKeys: options.publicKeys };
|
|
244
|
+
}
|
|
245
|
+
return tx;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
//# debugId=B2AF18736399320664756E2164756E21
|
|
249
|
+
//# sourceMappingURL=stacks-zyv9qqme.cjs.map
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/transactions/signer.ts", "../src/transactions/build.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import { bytesToHex, hexToBytes } from \"../utils/encoding.ts\";\nimport { txidFromBytes } from \"../utils/hash.ts\";\nimport {\n intoInitialSighashAuth,\n nextSignature,\n sigHashPreSign,\n sigHashPostSign,\n createSingleSigSpendingCondition,\n} from \"./authorization.ts\";\nimport { replayMultiSigSigHash } from \"./multisig.ts\";\nimport { serializeTransaction } from \"./wire/serialize.ts\";\nimport {\n AuthType,\n PubKeyEncoding,\n RECOVERABLE_ECDSA_SIG_LENGTH_BYTES,\n type StacksTransaction,\n type SingleSigSpendingCondition,\n type MultiSigSpendingCondition,\n type SponsoredAuthorization,\n} from \"./types.ts\";\nimport type { LocalAccount, CustomAccount } from \"../accounts/types.ts\";\n\nfunction txid(tx: StacksTransaction): string {\n return txidFromBytes(serializeTransaction(tx));\n}\n\n/** Compute the initial sighash for a transaction */\nexport function signBegin(tx: StacksTransaction): string {\n const cleared: StacksTransaction = {\n ...tx,\n auth: intoInitialSighashAuth(tx.auth),\n };\n return txid(cleared);\n}\n\n/** Sign a single-sig transaction with a private key, returning the signed transaction */\nexport function signTransaction(\n tx: StacksTransaction,\n privateKey: string\n): StacksTransaction {\n const sigHash = signBegin(tx);\n const condition = tx.auth.spendingCondition as SingleSigSpendingCondition;\n\n // Origin always signs with AuthType.Standard (even for sponsored txs)\n const { nextSig, nextSigHash } = nextSignature(\n sigHash,\n AuthType.Standard,\n condition.fee,\n condition.nonce,\n privateKey\n );\n\n return {\n ...tx,\n auth: {\n ...tx.auth,\n spendingCondition: {\n ...condition,\n signature: nextSig,\n },\n },\n };\n}\n\n/** Sign a single-sig transaction using an account (LocalAccount or CustomAccount) */\nexport async function signTransactionWithAccount(\n tx: StacksTransaction,\n account: LocalAccount | CustomAccount\n): Promise<StacksTransaction> {\n const sigHash = signBegin(tx);\n const condition = tx.auth.spendingCondition as SingleSigSpendingCondition;\n\n // Origin always signs with AuthType.Standard (even for sponsored txs)\n const sigHashPre = sigHashPreSign(\n sigHash,\n AuthType.Standard,\n condition.fee,\n condition.nonce\n );\n\n // account.sign returns 65-byte VRS (recovery + r + s)\n const sigBytes = await account.sign(hexToBytes(sigHashPre));\n const nextSig = bytesToHex(sigBytes);\n\n const pubKeyBytes = hexToBytes(account.publicKey);\n const pubKeyEncoding =\n pubKeyBytes.length === 33\n ? PubKeyEncoding.Compressed\n : PubKeyEncoding.Uncompressed;\n\n return {\n ...tx,\n auth: {\n ...tx.auth,\n spendingCondition: {\n ...condition,\n signature: nextSig,\n },\n },\n };\n}\n\n/** Get the txid of a transaction */\nexport function getTransactionId(tx: StacksTransaction): string {\n return txid(tx);\n}\n\n/** Reconstruct the sighash after origin signing (needed for sponsor signing) */\nexport function getOriginSigHash(tx: StacksTransaction): string {\n const condition = tx.auth.spendingCondition;\n\n // Multi-sig: replay through all fields\n if (\"fields\" in condition) {\n return replayMultiSigSigHash(tx);\n }\n\n // Single-sig\n const initialSigHash = signBegin(tx);\n const sc = condition as SingleSigSpendingCondition;\n const preSign = sigHashPreSign(initialSigHash, AuthType.Standard, sc.fee, sc.nonce);\n return sigHashPostSign(preSign, sc.keyEncoding, sc.signature);\n}\n\n/** Sign a sponsored transaction as the sponsor with a private key */\nexport function signSponsor(tx: StacksTransaction, privateKey: string): StacksTransaction {\n if (tx.auth.authType !== AuthType.Sponsored) {\n throw new Error(\"Transaction must be sponsored\");\n }\n const auth = tx.auth as SponsoredAuthorization;\n const sponsorCondition = auth.sponsorSpendingCondition as SingleSigSpendingCondition;\n const originSigHash = getOriginSigHash(tx);\n\n const { nextSig } = nextSignature(\n originSigHash,\n AuthType.Sponsored,\n sponsorCondition.fee,\n sponsorCondition.nonce,\n privateKey\n );\n\n return {\n ...tx,\n auth: {\n ...auth,\n sponsorSpendingCondition: {\n ...sponsorCondition,\n signature: nextSig,\n },\n },\n };\n}\n\n/** Sign a sponsored transaction as the sponsor using an account */\nexport async function signSponsorWithAccount(\n tx: StacksTransaction,\n account: LocalAccount | CustomAccount\n): Promise<StacksTransaction> {\n if (tx.auth.authType !== AuthType.Sponsored) {\n throw new Error(\"Transaction must be sponsored\");\n }\n const auth = tx.auth as SponsoredAuthorization;\n const sponsorCondition = auth.sponsorSpendingCondition as SingleSigSpendingCondition;\n const originSigHash = getOriginSigHash(tx);\n\n const sigHashPre = sigHashPreSign(\n originSigHash,\n AuthType.Sponsored,\n sponsorCondition.fee,\n sponsorCondition.nonce\n );\n\n const sigBytes = await account.sign(hexToBytes(sigHashPre));\n const nextSig = bytesToHex(sigBytes);\n\n return {\n ...tx,\n auth: {\n ...auth,\n sponsorSpendingCondition: {\n ...sponsorCondition,\n signature: nextSig,\n },\n },\n };\n}\n",
|
|
6
|
+
"import type { StacksChain } from \"../chains/types.ts\";\nimport type { ClarityValue } from \"../clarity/types.ts\";\nimport type { PostCondition } from \"../postconditions/types.ts\";\nimport type { MultiSigHashMode, SpendingCondition } from \"./types.ts\";\nimport {\n createSingleSigSpendingCondition,\n createStandardAuth,\n createSponsoredAuth,\n} from \"./authorization.ts\";\nimport { createMultiSigSpendingCondition } from \"./multisig.ts\";\nimport {\n PayloadType,\n AnchorMode,\n PostConditionModeWire,\n FungibleConditionCode,\n NonFungibleConditionCode,\n PostConditionPrincipalId,\n type StacksTransaction,\n type PostConditionWire,\n type PostConditionPrincipalWire,\n type ClarityVersion,\n} from \"./types.ts\";\nimport { Cl } from \"../clarity/values.ts\";\nimport { intToBigInt, type IntegerType } from \"../utils/encoding.ts\";\nimport { validateStacksAddress, parseContractId } from \"../utils/address.ts\";\n\ntype MultiSigOptions = {\n publicKeys?: string[];\n signaturesRequired?: number;\n hashMode?: MultiSigHashMode;\n};\n\nexport type BuildTokenTransferOptions = MultiSigOptions & {\n recipient: string;\n amount: IntegerType;\n memo?: string;\n fee: IntegerType;\n nonce: IntegerType;\n publicKey?: string;\n chain?: StacksChain;\n postConditionMode?: \"allow\" | \"deny\";\n postConditions?: PostCondition[];\n sponsored?: boolean;\n};\n\nexport type BuildContractCallOptions = MultiSigOptions & {\n contractAddress: string;\n contractName: string;\n functionName: string;\n functionArgs: ClarityValue[];\n fee: IntegerType;\n nonce: IntegerType;\n publicKey?: string;\n chain?: StacksChain;\n postConditionMode?: \"allow\" | \"deny\";\n postConditions?: PostCondition[];\n sponsored?: boolean;\n};\n\nexport type BuildContractDeployOptions = MultiSigOptions & {\n contractName: string;\n codeBody: string;\n clarityVersion?: ClarityVersion;\n fee: IntegerType;\n nonce: IntegerType;\n publicKey?: string;\n chain?: StacksChain;\n postConditionMode?: \"allow\" | \"deny\";\n postConditions?: PostCondition[];\n sponsored?: boolean;\n};\n\nfunction resolvePcMode(mode?: \"allow\" | \"deny\"): PostConditionModeWire {\n return mode === \"allow\" ? PostConditionModeWire.Allow : PostConditionModeWire.Deny;\n}\n\nfunction convertPostConditions(pcs?: PostCondition[]): PostConditionWire[] {\n if (!pcs || pcs.length === 0) return [];\n return pcs.map(convertPostCondition);\n}\n\nfunction resolvePrincipal(address: string): PostConditionPrincipalWire {\n if (address === \"origin\") return { type: \"origin\" };\n const [addr, name] = address.split(\".\");\n if (name) {\n return { type: \"contract\", address: addr!, contractName: name };\n }\n return { type: \"standard\", address: addr! };\n}\n\nconst FUNGIBLE_CODE_MAP: Record<string, number> = {\n eq: FungibleConditionCode.Equal,\n gt: FungibleConditionCode.Greater,\n gte: FungibleConditionCode.GreaterEqual,\n lt: FungibleConditionCode.Less,\n lte: FungibleConditionCode.LessEqual,\n};\n\nfunction convertPostCondition(pc: PostCondition): PostConditionWire {\n switch (pc.type) {\n case \"stx-postcondition\":\n return {\n type: \"stx\",\n principal: resolvePrincipal(pc.address),\n conditionCode: FUNGIBLE_CODE_MAP[pc.condition]!,\n amount: intToBigInt(pc.amount),\n };\n case \"ft-postcondition\": {\n const [contractId, tokenName] = pc.asset.split(\"::\");\n const [addr, name] = parseContractId(contractId!);\n return {\n type: \"ft\",\n principal: resolvePrincipal(pc.address),\n asset: { address: addr, contractName: name, assetName: tokenName! },\n conditionCode: FUNGIBLE_CODE_MAP[pc.condition]!,\n amount: intToBigInt(pc.amount),\n };\n }\n case \"nft-postcondition\": {\n const [contractId, tokenName] = pc.asset.split(\"::\");\n const [addr, name] = parseContractId(contractId!);\n return {\n type: \"nft\",\n principal: resolvePrincipal(pc.address),\n asset: { address: addr, contractName: name, assetName: tokenName! },\n conditionCode: pc.condition === \"sent\" ? NonFungibleConditionCode.Sends : NonFungibleConditionCode.DoesNotSend,\n assetId: pc.assetId,\n };\n }\n }\n}\n\nimport type { Authorization } from \"./types.ts\";\n\nfunction resolveSpendingCondition(\n opts: MultiSigOptions & { publicKey?: string },\n nonce: bigint,\n fee: bigint\n): SpendingCondition {\n if (opts.publicKeys) {\n return createMultiSigSpendingCondition(\n opts.publicKeys,\n opts.signaturesRequired!,\n nonce,\n fee,\n opts.hashMode\n );\n }\n return createSingleSigSpendingCondition(opts.publicKey!, nonce, fee);\n}\n\nfunction resolveAuth(spendingCondition: any, sponsored?: boolean): Authorization {\n return sponsored\n ? createSponsoredAuth(spendingCondition)\n : createStandardAuth(spendingCondition);\n}\n\nfunction resolveVersionAndChainId(chain?: StacksChain): { version: number; chainId: number } {\n if (!chain) return { version: 0x00, chainId: 0x00000001 }; // mainnet defaults\n return { version: chain.transactionVersion, chainId: chain.id };\n}\n\nexport function buildTokenTransfer(options: BuildTokenTransferOptions): StacksTransaction {\n const { version, chainId } = resolveVersionAndChainId(options.chain);\n const fee = intToBigInt(options.fee);\n const nonce = intToBigInt(options.nonce);\n\n const spendingCondition = resolveSpendingCondition(options, nonce, fee);\n const recipient = Cl.principal(options.recipient);\n\n const tx: StacksTransaction = {\n version,\n chainId,\n auth: resolveAuth(spendingCondition, options.sponsored),\n anchorMode: AnchorMode.Any,\n postConditionMode: resolvePcMode(options.postConditionMode),\n postConditions: convertPostConditions(options.postConditions),\n payload: {\n payloadType: PayloadType.TokenTransfer,\n recipient,\n amount: intToBigInt(options.amount),\n memo: options.memo ?? \"\",\n },\n };\n\n if (options.publicKeys) {\n (tx as any)._multisig = { publicKeys: options.publicKeys };\n }\n return tx;\n}\n\nexport function buildContractCall(options: BuildContractCallOptions): StacksTransaction {\n const { version, chainId } = resolveVersionAndChainId(options.chain);\n const fee = intToBigInt(options.fee);\n const nonce = intToBigInt(options.nonce);\n\n const spendingCondition = resolveSpendingCondition(options, nonce, fee);\n\n const tx: StacksTransaction = {\n version,\n chainId,\n auth: resolveAuth(spendingCondition, options.sponsored),\n anchorMode: AnchorMode.Any,\n postConditionMode: resolvePcMode(options.postConditionMode),\n postConditions: convertPostConditions(options.postConditions),\n payload: {\n payloadType: PayloadType.ContractCall,\n contractAddress: options.contractAddress,\n contractName: options.contractName,\n functionName: options.functionName,\n functionArgs: options.functionArgs,\n },\n };\n\n if (options.publicKeys) {\n (tx as any)._multisig = { publicKeys: options.publicKeys };\n }\n return tx;\n}\n\nexport function buildContractDeploy(options: BuildContractDeployOptions): StacksTransaction {\n const { version, chainId } = resolveVersionAndChainId(options.chain);\n const fee = intToBigInt(options.fee);\n const nonce = intToBigInt(options.nonce);\n\n const spendingCondition = resolveSpendingCondition(options, nonce, fee);\n const useVersioned = options.clarityVersion !== undefined;\n\n const tx: StacksTransaction = {\n version,\n chainId,\n auth: resolveAuth(spendingCondition, options.sponsored),\n anchorMode: AnchorMode.Any,\n postConditionMode: resolvePcMode(options.postConditionMode),\n postConditions: convertPostConditions(options.postConditions),\n payload: {\n payloadType: useVersioned ? PayloadType.VersionedSmartContract : PayloadType.SmartContract,\n clarityVersion: options.clarityVersion,\n contractName: options.contractName,\n codeBody: options.codeBody,\n },\n };\n\n if (options.publicKeys) {\n (tx as any)._multisig = { publicKeys: options.publicKeys };\n }\n return tx;\n}\n"
|
|
7
|
+
],
|
|
8
|
+
"mappings": ";AAsBA,SAAS,IAAI,CAAC,IAA+B;AAAA,EAC3C,OAAO,cAAc,qBAAqB,EAAE,CAAC;AAAA;AAIxC,SAAS,SAAS,CAAC,IAA+B;AAAA,EACvD,MAAM,UAA6B;AAAA,OAC9B;AAAA,IACH,MAAM,uBAAuB,GAAG,IAAI;AAAA,EACtC;AAAA,EACA,OAAO,KAAK,OAAO;AAAA;AAId,SAAS,eAAe,CAC7B,IACA,YACmB;AAAA,EACnB,MAAM,UAAU,UAAU,EAAE;AAAA,EAC5B,MAAM,YAAY,GAAG,KAAK;AAAA,EAG1B,QAAQ,SAAS,gBAAgB,cAC/B,SACA,SAAS,UACT,UAAU,KACV,UAAU,OACV,UACF;AAAA,EAEA,OAAO;AAAA,OACF;AAAA,IACH,MAAM;AAAA,SACD,GAAG;AAAA,MACN,mBAAmB;AAAA,WACd;AAAA,QACH,WAAW;AAAA,MACb;AAAA,IACF;AAAA,EACF;AAAA;AAIF,eAAsB,0BAA0B,CAC9C,IACA,SAC4B;AAAA,EAC5B,MAAM,UAAU,UAAU,EAAE;AAAA,EAC5B,MAAM,YAAY,GAAG,KAAK;AAAA,EAG1B,MAAM,aAAa,eACjB,SACA,SA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EAC3D;AAAA,EACA,OAAO;AAAA;",
|
|
9
|
+
"debugId": "B2AF18736399320664756E2164756E21",
|
|
10
|
+
"names": []
|
|
11
|
+
}
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
// src/accounts/index.ts
|
|
2
|
+
var exports_accounts = {};
|
|
3
|
+
__export(exports_accounts, {
|
|
4
|
+
toAccount: () => toAccount,
|
|
5
|
+
providerToAccount: () => providerToAccount,
|
|
6
|
+
privateKeyToAccount: () => privateKeyToAccount,
|
|
7
|
+
mnemonicToAccount: () => mnemonicToAccount,
|
|
8
|
+
compressPrivateKey: () => compressPrivateKey
|
|
9
|
+
});
|
|
10
|
+
module.exports = __toCommonJS(exports_accounts);
|
|
11
|
+
|
|
12
|
+
// src/accounts/privateKeyToAccount.ts
|
|
13
|
+
var import_secp256k1 = require("@noble/secp256k1");
|
|
14
|
+
var import_hmac = require("@noble/hashes/hmac.js");
|
|
15
|
+
var import_sha2 = require("@noble/hashes/sha2.js");
|
|
16
|
+
function ensureSyncSigning() {
|
|
17
|
+
if (!import_secp256k1.etc.hmacSha256Sync) {
|
|
18
|
+
import_secp256k1.etc.hmacSha256Sync = (key, ...msgs) => {
|
|
19
|
+
const h = import_hmac.hmac.create(import_sha2.sha256, key);
|
|
20
|
+
msgs.forEach((msg) => h.update(msg));
|
|
21
|
+
return h.digest();
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
var PRIVATE_KEY_COMPRESSED_LENGTH = 33;
|
|
26
|
+
function privateKeyToAccount(privateKey, options) {
|
|
27
|
+
ensureSyncSigning();
|
|
28
|
+
const keyBytes = normalizePrivateKey(privateKey);
|
|
29
|
+
const rawKey = keyBytes.slice(0, 32);
|
|
30
|
+
const publicKeyBytes = import_secp256k1.getPublicKey(rawKey, true);
|
|
31
|
+
const publicKeyHex = bytesToHex(publicKeyBytes);
|
|
32
|
+
const addressVersion = options?.addressVersion ?? 22;
|
|
33
|
+
const address = c32address(addressVersion, bytesToHex(hash160(publicKeyBytes)));
|
|
34
|
+
return {
|
|
35
|
+
type: "local",
|
|
36
|
+
address,
|
|
37
|
+
publicKey: publicKeyHex,
|
|
38
|
+
sign(hash) {
|
|
39
|
+
const sig = import_secp256k1.sign(hash, rawKey, { lowS: true });
|
|
40
|
+
const result = new Uint8Array(65);
|
|
41
|
+
result[0] = sig.recovery;
|
|
42
|
+
result.set(sig.toCompactRawBytes(), 1);
|
|
43
|
+
return result;
|
|
44
|
+
},
|
|
45
|
+
signMessage(message) {
|
|
46
|
+
const msgBytes = typeof message === "string" ? new TextEncoder().encode(message) : message;
|
|
47
|
+
const msgHash = import_sha2.sha256(msgBytes);
|
|
48
|
+
const sigBytes = this.sign(msgHash);
|
|
49
|
+
return bytesToHex(sigBytes);
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
function normalizePrivateKey(key) {
|
|
54
|
+
if (typeof key === "string") {
|
|
55
|
+
return hexToBytes(without0x(key));
|
|
56
|
+
}
|
|
57
|
+
return key;
|
|
58
|
+
}
|
|
59
|
+
function compressPrivateKey(key) {
|
|
60
|
+
const hex = typeof key === "string" ? without0x(key) : bytesToHex(key);
|
|
61
|
+
if (hex.length === PRIVATE_KEY_COMPRESSED_LENGTH * 2)
|
|
62
|
+
return hex;
|
|
63
|
+
return `${hex}01`;
|
|
64
|
+
}
|
|
65
|
+
// src/accounts/mnemonicToAccount.ts
|
|
66
|
+
var import_bip32 = require("@scure/bip32");
|
|
67
|
+
var import_bip39 = require("@scure/bip39");
|
|
68
|
+
var STX_DERIVATION_PATH = "m/44'/5757'/0'/0";
|
|
69
|
+
function mnemonicToAccount(mnemonic, options) {
|
|
70
|
+
const { accountIndex = 0, addressVersion } = options ?? {};
|
|
71
|
+
const seed = import_bip39.mnemonicToSeedSync(mnemonic);
|
|
72
|
+
const root = import_bip32.HDKey.fromMasterSeed(seed);
|
|
73
|
+
const child = root.derive(`${STX_DERIVATION_PATH}/${accountIndex}`);
|
|
74
|
+
if (!child.privateKey)
|
|
75
|
+
throw new Error("Failed to derive private key");
|
|
76
|
+
const privateKeyHex = compressPrivateKey(bytesToHex(child.privateKey));
|
|
77
|
+
return privateKeyToAccount(privateKeyHex, { addressVersion });
|
|
78
|
+
}
|
|
79
|
+
// src/accounts/toAccount.ts
|
|
80
|
+
function toAccount(source) {
|
|
81
|
+
return {
|
|
82
|
+
type: "custom",
|
|
83
|
+
address: source.address,
|
|
84
|
+
publicKey: source.publicKey,
|
|
85
|
+
sign: source.sign
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
//# debugId=76E8F09310BEBB1864756E2164756E21
|
|
90
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/accounts/privateKeyToAccount.ts", "../src/accounts/mnemonicToAccount.ts", "../src/accounts/toAccount.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import {\n getPublicKey as nobleGetPublicKey,\n sign,\n etc,\n} from \"@noble/secp256k1\";\nimport { hmac } from \"@noble/hashes/hmac.js\";\nimport { sha256 } from \"@noble/hashes/sha2.js\";\nimport { c32address } from \"../utils/c32.ts\";\nimport { bytesToHex, hexToBytes, without0x } from \"../utils/encoding.ts\";\nimport { hash160 } from \"../utils/hash.ts\";\nimport type { LocalAccount } from \"./types.ts\";\n\nfunction ensureSyncSigning() {\n if (!etc.hmacSha256Sync) {\n etc.hmacSha256Sync = (key: Uint8Array, ...msgs: Uint8Array[]) => {\n const h = hmac.create(sha256, key);\n msgs.forEach((msg) => h.update(msg));\n return h.digest();\n };\n }\n}\n\nconst PRIVATE_KEY_COMPRESSED_LENGTH = 33;\n\nexport function privateKeyToAccount(\n privateKey: string | Uint8Array,\n options?: { addressVersion?: number }\n): LocalAccount {\n ensureSyncSigning();\n const keyBytes = normalizePrivateKey(privateKey);\n const rawKey = keyBytes.slice(0, 32);\n\n // Always derive compressed public key\n const publicKeyBytes = nobleGetPublicKey(rawKey, true);\n const publicKeyHex = bytesToHex(publicKeyBytes);\n\n // Default to mainnet single-sig (22)\n const addressVersion = options?.addressVersion ?? 22;\n const address = c32address(addressVersion, bytesToHex(hash160(publicKeyBytes)));\n\n return {\n type: \"local\",\n address,\n publicKey: publicKeyHex,\n\n sign(hash: Uint8Array): Uint8Array {\n const sig = sign(hash, rawKey, { lowS: true });\n const result = new Uint8Array(65);\n result[0] = sig.recovery;\n result.set(sig.toCompactRawBytes(), 1);\n return result;\n },\n\n signMessage(message: string | Uint8Array): string {\n const msgBytes =\n typeof message === \"string\"\n ? new TextEncoder().encode(message)\n : message;\n const msgHash = sha256(msgBytes);\n const sigBytes = this.sign(msgHash);\n return bytesToHex(sigBytes);\n },\n };\n}\n\nfunction normalizePrivateKey(key: string | Uint8Array): Uint8Array {\n if (typeof key === \"string\") {\n return hexToBytes(without0x(key));\n }\n return key;\n}\n\n/** Append 0x01 suffix if not already compressed */\nexport function compressPrivateKey(key: string | Uint8Array): string {\n const hex = typeof key === \"string\" ? without0x(key) : bytesToHex(key);\n if (hex.length === PRIVATE_KEY_COMPRESSED_LENGTH * 2) return hex;\n return `${hex}01`;\n}\n",
|
|
6
|
+
"import { HDKey } from \"@scure/bip32\";\nimport { mnemonicToSeedSync } from \"@scure/bip39\";\nimport { bytesToHex } from \"../utils/encoding.ts\";\nimport { privateKeyToAccount, compressPrivateKey } from \"./privateKeyToAccount.ts\";\nimport type { LocalAccount } from \"./types.ts\";\n\nconst STX_DERIVATION_PATH = \"m/44'/5757'/0'/0\";\n\nexport function mnemonicToAccount(\n mnemonic: string,\n options?: {\n accountIndex?: number;\n addressVersion?: number;\n }\n): LocalAccount {\n const { accountIndex = 0, addressVersion } = options ?? {};\n\n const seed = mnemonicToSeedSync(mnemonic);\n const root = HDKey.fromMasterSeed(seed);\n const child = root.derive(`${STX_DERIVATION_PATH}/${accountIndex}`);\n\n if (!child.privateKey) throw new Error(\"Failed to derive private key\");\n\n const privateKeyHex = compressPrivateKey(bytesToHex(child.privateKey));\n return privateKeyToAccount(privateKeyHex, { addressVersion });\n}\n",
|
|
7
|
+
"import type { AccountSource, CustomAccount } from \"./types.ts\";\n\nexport function toAccount(source: AccountSource): CustomAccount {\n return {\n type: \"custom\",\n address: source.address,\n publicKey: source.publicKey,\n sign: source.sign,\n };\n}\n"
|
|
8
|
+
],
|
|
9
|
+
"mappings": ";;;;;;;;;;;;AAIO,IAJP;AAKqB,IAArB;AACuB,IAAvB;AAMA,SAAS,iBAAiB,GAAG;AAAA,EAC3B,IAAI,CAAC,qBAAI,gBAAgB;AAAA,IACvB,qBAAI,iBAAiB,CAAC,QAAoB,SAAuB;AAAA,MAC/D,MAAM,IAAI,iBAAK,OAAO,oBAAQ,GAAG;AAAA,MACjC,KAAK,QAAQ,CAAC,QAAQ,EAAE,OAAO,GAAG,CAAC;AAAA,MACnC,OAAO,EAAE,OAAO;AAAA;AAAA,EAEpB;AAAA;AAGF,IAAM,gCAAgC;AAE/B,SAAS,mBAAmB,CACjC,YACA,SACc;AAAA,EACd,kBAAkB;AAAA,EAClB,MAAM,WAAW,oBAAoB,UAAU;AAAA,EAC/C,MAAM,SAAS,SAAS,MAAM,GAAG,EAAE;AAAA,EAGnC,MAAM,iBAAiB,8BAAkB,QAAQ,IAAI;AAAA,EACrD,MAAM,eAAe,WAAW,cAAc;AAAA,EAG9C,MAAM,iBAAiB,SAAS,kBAAkB;AAAA,EAClD,MAAM,UAAU,WAAW,gBAAgB,WAAW,QAAQ,cAAc,CAAC,CAAC;AAAA,EAE9E,OAAO;AAAA,IACL,MAAM;AAAA,IACN;AAAA,IACA,WAAW;AAAA,IAEX,IAAI,CAAC,MAA8B;AAAA,MACjC,MAAM,MAAM,sBAAK,MAAM,QAAQ,EAAE,MAAM,KAAK,CAAC;AAAA,MAC7C,MAAM,SAAS,IAAI,WAAW,EAAE;AAAA,MAChC,OAAO,KAAK,IAAI;AAAA,MAChB,OAAO,IAAI,IA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MAAM,OAAO;AAAA,EACf;AAAA;",
|
|
10
|
+
"debugId": "76E8F09310BEBB1864756E2164756E21",
|
|
11
|
+
"names": []
|
|
12
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** Account derived from a local private key (mnemonic or raw key). */
|
|
2
|
+
type LocalAccount = {
|
|
3
|
+
type: "local"
|
|
4
|
+
address: string
|
|
5
|
+
/** Compressed public key (hex) */
|
|
6
|
+
publicKey: string
|
|
7
|
+
/** Raw ECDSA sign over a hash */
|
|
8
|
+
sign(hash: Uint8Array): Uint8Array
|
|
9
|
+
/** Sign a structured message (SIP-018) */
|
|
10
|
+
signMessage(message: string | Uint8Array): string
|
|
11
|
+
};
|
|
12
|
+
/** Account with a user-provided signing function (sync or async). */
|
|
13
|
+
type CustomAccount = {
|
|
14
|
+
type: "custom"
|
|
15
|
+
address: string
|
|
16
|
+
publicKey: string
|
|
17
|
+
sign(hash: Uint8Array): Promise<Uint8Array> | Uint8Array
|
|
18
|
+
};
|
|
19
|
+
/** Browser wallet provider interface (e.g. Leather, Xverse). */
|
|
20
|
+
type StacksProvider = {
|
|
21
|
+
request(method: string, params?: any): Promise<any>
|
|
22
|
+
};
|
|
23
|
+
/** Account backed by a browser wallet {@link StacksProvider}. */
|
|
24
|
+
type ProviderAccount = {
|
|
25
|
+
type: "provider"
|
|
26
|
+
address: string
|
|
27
|
+
publicKey: string
|
|
28
|
+
provider: StacksProvider
|
|
29
|
+
};
|
|
30
|
+
type AccountSource = {
|
|
31
|
+
address: string
|
|
32
|
+
publicKey: string
|
|
33
|
+
sign(hash: Uint8Array): Promise<Uint8Array> | Uint8Array
|
|
34
|
+
};
|
|
35
|
+
declare function privateKeyToAccount(privateKey: string | Uint8Array, options?: {
|
|
36
|
+
addressVersion?: number
|
|
37
|
+
}): LocalAccount;
|
|
38
|
+
/** Append 0x01 suffix if not already compressed */
|
|
39
|
+
declare function compressPrivateKey(key: string | Uint8Array): string;
|
|
40
|
+
declare function mnemonicToAccount(mnemonic: string, options?: {
|
|
41
|
+
accountIndex?: number
|
|
42
|
+
addressVersion?: number
|
|
43
|
+
}): LocalAccount;
|
|
44
|
+
declare function toAccount(source: AccountSource): CustomAccount;
|
|
45
|
+
/** Create a ProviderAccount by querying the wallet for addresses */
|
|
46
|
+
declare function providerToAccount(provider: StacksProvider): Promise<ProviderAccount>;
|
|
47
|
+
export { toAccount, providerToAccount, privateKeyToAccount, mnemonicToAccount, compressPrivateKey, StacksProvider, ProviderAccount, LocalAccount, CustomAccount, AccountSource };
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/** Account derived from a local private key (mnemonic or raw key). */
|
|
2
|
+
type LocalAccount = {
|
|
3
|
+
type: "local"
|
|
4
|
+
address: string
|
|
5
|
+
/** Compressed public key (hex) */
|
|
6
|
+
publicKey: string
|
|
7
|
+
/** Raw ECDSA sign over a hash */
|
|
8
|
+
sign(hash: Uint8Array): Uint8Array
|
|
9
|
+
/** Sign a structured message (SIP-018) */
|
|
10
|
+
signMessage(message: string | Uint8Array): string
|
|
11
|
+
};
|
|
12
|
+
/** Account with a user-provided signing function (sync or async). */
|
|
13
|
+
type CustomAccount = {
|
|
14
|
+
type: "custom"
|
|
15
|
+
address: string
|
|
16
|
+
publicKey: string
|
|
17
|
+
sign(hash: Uint8Array): Promise<Uint8Array> | Uint8Array
|
|
18
|
+
};
|
|
19
|
+
/** Browser wallet provider interface (e.g. Leather, Xverse). */
|
|
20
|
+
type StacksProvider = {
|
|
21
|
+
request(method: string, params?: any): Promise<any>
|
|
22
|
+
};
|
|
23
|
+
/** Account backed by a browser wallet {@link StacksProvider}. */
|
|
24
|
+
type ProviderAccount = {
|
|
25
|
+
type: "provider"
|
|
26
|
+
address: string
|
|
27
|
+
publicKey: string
|
|
28
|
+
provider: StacksProvider
|
|
29
|
+
};
|
|
30
|
+
type AccountSource = {
|
|
31
|
+
address: string
|
|
32
|
+
publicKey: string
|
|
33
|
+
sign(hash: Uint8Array): Promise<Uint8Array> | Uint8Array
|
|
34
|
+
};
|
|
35
|
+
declare function privateKeyToAccount(privateKey: string | Uint8Array, options?: {
|
|
36
|
+
addressVersion?: number
|
|
37
|
+
}): LocalAccount;
|
|
38
|
+
/** Append 0x01 suffix if not already compressed */
|
|
39
|
+
declare function compressPrivateKey(key: string | Uint8Array): string;
|
|
40
|
+
declare function mnemonicToAccount(mnemonic: string, options?: {
|
|
41
|
+
accountIndex?: number
|
|
42
|
+
addressVersion?: number
|
|
43
|
+
}): LocalAccount;
|
|
44
|
+
declare function toAccount(source: AccountSource): CustomAccount;
|
|
45
|
+
/** Create a ProviderAccount by querying the wallet for addresses */
|
|
46
|
+
declare function providerToAccount(provider: StacksProvider): Promise<ProviderAccount>;
|
|
47
|
+
export { toAccount, providerToAccount, privateKeyToAccount, mnemonicToAccount, compressPrivateKey, StacksProvider, ProviderAccount, LocalAccount, CustomAccount, AccountSource };
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import {
|
|
2
|
+
providerToAccount
|
|
3
|
+
} from "../../shared/stacks-qcae5852.js";
|
|
4
|
+
import {
|
|
5
|
+
hash160
|
|
6
|
+
} from "../../shared/stacks-bycy6t9k.js";
|
|
7
|
+
import {
|
|
8
|
+
bytesToHex,
|
|
9
|
+
c32address,
|
|
10
|
+
hexToBytes,
|
|
11
|
+
without0x
|
|
12
|
+
} from "../../shared/stacks-nqytwfcc.js";
|
|
13
|
+
import"../../shared/stacks-8m9byk2j.js";
|
|
14
|
+
|
|
15
|
+
// src/accounts/privateKeyToAccount.ts
|
|
16
|
+
import {
|
|
17
|
+
getPublicKey as nobleGetPublicKey,
|
|
18
|
+
sign,
|
|
19
|
+
etc
|
|
20
|
+
} from "@noble/secp256k1";
|
|
21
|
+
import { hmac } from "@noble/hashes/hmac.js";
|
|
22
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
23
|
+
function ensureSyncSigning() {
|
|
24
|
+
if (!etc.hmacSha256Sync) {
|
|
25
|
+
etc.hmacSha256Sync = (key, ...msgs) => {
|
|
26
|
+
const h = hmac.create(sha256, key);
|
|
27
|
+
msgs.forEach((msg) => h.update(msg));
|
|
28
|
+
return h.digest();
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
var PRIVATE_KEY_COMPRESSED_LENGTH = 33;
|
|
33
|
+
function privateKeyToAccount(privateKey, options) {
|
|
34
|
+
ensureSyncSigning();
|
|
35
|
+
const keyBytes = normalizePrivateKey(privateKey);
|
|
36
|
+
const rawKey = keyBytes.slice(0, 32);
|
|
37
|
+
const publicKeyBytes = nobleGetPublicKey(rawKey, true);
|
|
38
|
+
const publicKeyHex = bytesToHex(publicKeyBytes);
|
|
39
|
+
const addressVersion = options?.addressVersion ?? 22;
|
|
40
|
+
const address = c32address(addressVersion, bytesToHex(hash160(publicKeyBytes)));
|
|
41
|
+
return {
|
|
42
|
+
type: "local",
|
|
43
|
+
address,
|
|
44
|
+
publicKey: publicKeyHex,
|
|
45
|
+
sign(hash) {
|
|
46
|
+
const sig = sign(hash, rawKey, { lowS: true });
|
|
47
|
+
const result = new Uint8Array(65);
|
|
48
|
+
result[0] = sig.recovery;
|
|
49
|
+
result.set(sig.toCompactRawBytes(), 1);
|
|
50
|
+
return result;
|
|
51
|
+
},
|
|
52
|
+
signMessage(message) {
|
|
53
|
+
const msgBytes = typeof message === "string" ? new TextEncoder().encode(message) : message;
|
|
54
|
+
const msgHash = sha256(msgBytes);
|
|
55
|
+
const sigBytes = this.sign(msgHash);
|
|
56
|
+
return bytesToHex(sigBytes);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
function normalizePrivateKey(key) {
|
|
61
|
+
if (typeof key === "string") {
|
|
62
|
+
return hexToBytes(without0x(key));
|
|
63
|
+
}
|
|
64
|
+
return key;
|
|
65
|
+
}
|
|
66
|
+
function compressPrivateKey(key) {
|
|
67
|
+
const hex = typeof key === "string" ? without0x(key) : bytesToHex(key);
|
|
68
|
+
if (hex.length === PRIVATE_KEY_COMPRESSED_LENGTH * 2)
|
|
69
|
+
return hex;
|
|
70
|
+
return `${hex}01`;
|
|
71
|
+
}
|
|
72
|
+
// src/accounts/mnemonicToAccount.ts
|
|
73
|
+
import { HDKey } from "@scure/bip32";
|
|
74
|
+
import { mnemonicToSeedSync } from "@scure/bip39";
|
|
75
|
+
var STX_DERIVATION_PATH = "m/44'/5757'/0'/0";
|
|
76
|
+
function mnemonicToAccount(mnemonic, options) {
|
|
77
|
+
const { accountIndex = 0, addressVersion } = options ?? {};
|
|
78
|
+
const seed = mnemonicToSeedSync(mnemonic);
|
|
79
|
+
const root = HDKey.fromMasterSeed(seed);
|
|
80
|
+
const child = root.derive(`${STX_DERIVATION_PATH}/${accountIndex}`);
|
|
81
|
+
if (!child.privateKey)
|
|
82
|
+
throw new Error("Failed to derive private key");
|
|
83
|
+
const privateKeyHex = compressPrivateKey(bytesToHex(child.privateKey));
|
|
84
|
+
return privateKeyToAccount(privateKeyHex, { addressVersion });
|
|
85
|
+
}
|
|
86
|
+
// src/accounts/toAccount.ts
|
|
87
|
+
function toAccount(source) {
|
|
88
|
+
return {
|
|
89
|
+
type: "custom",
|
|
90
|
+
address: source.address,
|
|
91
|
+
publicKey: source.publicKey,
|
|
92
|
+
sign: source.sign
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
export {
|
|
96
|
+
toAccount,
|
|
97
|
+
providerToAccount,
|
|
98
|
+
privateKeyToAccount,
|
|
99
|
+
mnemonicToAccount,
|
|
100
|
+
compressPrivateKey
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
//# debugId=1B49DD7F09F3CD0764756E2164756E21
|
|
104
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"version": 3,
|
|
3
|
+
"sources": ["../src/accounts/privateKeyToAccount.ts", "../src/accounts/mnemonicToAccount.ts", "../src/accounts/toAccount.ts"],
|
|
4
|
+
"sourcesContent": [
|
|
5
|
+
"import {\n getPublicKey as nobleGetPublicKey,\n sign,\n etc,\n} from \"@noble/secp256k1\";\nimport { hmac } from \"@noble/hashes/hmac.js\";\nimport { sha256 } from \"@noble/hashes/sha2.js\";\nimport { c32address } from \"../utils/c32.ts\";\nimport { bytesToHex, hexToBytes, without0x } from \"../utils/encoding.ts\";\nimport { hash160 } from \"../utils/hash.ts\";\nimport type { LocalAccount } from \"./types.ts\";\n\nfunction ensureSyncSigning() {\n if (!etc.hmacSha256Sync) {\n etc.hmacSha256Sync = (key: Uint8Array, ...msgs: Uint8Array[]) => {\n const h = hmac.create(sha256, key);\n msgs.forEach((msg) => h.update(msg));\n return h.digest();\n };\n }\n}\n\nconst PRIVATE_KEY_COMPRESSED_LENGTH = 33;\n\nexport function privateKeyToAccount(\n privateKey: string | Uint8Array,\n options?: { addressVersion?: number }\n): LocalAccount {\n ensureSyncSigning();\n const keyBytes = normalizePrivateKey(privateKey);\n const rawKey = keyBytes.slice(0, 32);\n\n // Always derive compressed public key\n const publicKeyBytes = nobleGetPublicKey(rawKey, true);\n const publicKeyHex = bytesToHex(publicKeyBytes);\n\n // Default to mainnet single-sig (22)\n const addressVersion = options?.addressVersion ?? 22;\n const address = c32address(addressVersion, bytesToHex(hash160(publicKeyBytes)));\n\n return {\n type: \"local\",\n address,\n publicKey: publicKeyHex,\n\n sign(hash: Uint8Array): Uint8Array {\n const sig = sign(hash, rawKey, { lowS: true });\n const result = new Uint8Array(65);\n result[0] = sig.recovery;\n result.set(sig.toCompactRawBytes(), 1);\n return result;\n },\n\n signMessage(message: string | Uint8Array): string {\n const msgBytes =\n typeof message === \"string\"\n ? new TextEncoder().encode(message)\n : message;\n const msgHash = sha256(msgBytes);\n const sigBytes = this.sign(msgHash);\n return bytesToHex(sigBytes);\n },\n };\n}\n\nfunction normalizePrivateKey(key: string | Uint8Array): Uint8Array {\n if (typeof key === \"string\") {\n return hexToBytes(without0x(key));\n }\n return key;\n}\n\n/** Append 0x01 suffix if not already compressed */\nexport function compressPrivateKey(key: string | Uint8Array): string {\n const hex = typeof key === \"string\" ? without0x(key) : bytesToHex(key);\n if (hex.length === PRIVATE_KEY_COMPRESSED_LENGTH * 2) return hex;\n return `${hex}01`;\n}\n",
|
|
6
|
+
"import { HDKey } from \"@scure/bip32\";\nimport { mnemonicToSeedSync } from \"@scure/bip39\";\nimport { bytesToHex } from \"../utils/encoding.ts\";\nimport { privateKeyToAccount, compressPrivateKey } from \"./privateKeyToAccount.ts\";\nimport type { LocalAccount } from \"./types.ts\";\n\nconst STX_DERIVATION_PATH = \"m/44'/5757'/0'/0\";\n\nexport function mnemonicToAccount(\n mnemonic: string,\n options?: {\n accountIndex?: number;\n addressVersion?: number;\n }\n): LocalAccount {\n const { accountIndex = 0, addressVersion } = options ?? {};\n\n const seed = mnemonicToSeedSync(mnemonic);\n const root = HDKey.fromMasterSeed(seed);\n const child = root.derive(`${STX_DERIVATION_PATH}/${accountIndex}`);\n\n if (!child.privateKey) throw new Error(\"Failed to derive private key\");\n\n const privateKeyHex = compressPrivateKey(bytesToHex(child.privateKey));\n return privateKeyToAccount(privateKeyHex, { addressVersion });\n}\n",
|
|
7
|
+
"import type { AccountSource, CustomAccount } from \"./types.ts\";\n\nexport function toAccount(source: AccountSource): CustomAccount {\n return {\n type: \"custom\",\n address: source.address,\n publicKey: source.publicKey,\n sign: source.sign,\n };\n}\n"
|
|
8
|
+
],
|
|
9
|
+
"mappings": ";;;;;;;;;;;;;;;AAAA;AAAA,kBACE;AAAA;AAAA;AAAA;AAIF;AACA;AAMA,SAAS,iBAAiB,GAAG;AAAA,EAC3B,IAAI,CAAC,IAAI,gBAAgB;AAAA,IACvB,IAAI,iBAAiB,CAAC,QAAoB,SAAuB;AAAA,MAC/D,MAAM,IAAI,KAAK,OAAO,QAAQ,GAAG;AAAA,MACjC,KAAK,QAAQ,CAAC,QAAQ,EAAE,OAAO,GAAG,CAAC;AAAA,MACnC,OAAO,EAAE,OAAO;AAAA;AAAA,EAEpB;AAAA;AAGF,IAAM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