@arkade-os/sdk 0.0.16
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 +312 -0
- package/dist/cjs/arknote/index.js +86 -0
- package/dist/cjs/forfeit.js +38 -0
- package/dist/cjs/identity/inMemoryKey.js +40 -0
- package/dist/cjs/identity/index.js +2 -0
- package/dist/cjs/index.js +48 -0
- package/dist/cjs/musig2/index.js +10 -0
- package/dist/cjs/musig2/keys.js +57 -0
- package/dist/cjs/musig2/nonces.js +44 -0
- package/dist/cjs/musig2/sign.js +102 -0
- package/dist/cjs/networks.js +26 -0
- package/dist/cjs/package.json +3 -0
- package/dist/cjs/providers/ark.js +530 -0
- package/dist/cjs/providers/onchain.js +61 -0
- package/dist/cjs/script/address.js +45 -0
- package/dist/cjs/script/base.js +51 -0
- package/dist/cjs/script/default.js +40 -0
- package/dist/cjs/script/tapscript.js +528 -0
- package/dist/cjs/script/vhtlc.js +84 -0
- package/dist/cjs/tree/signingSession.js +238 -0
- package/dist/cjs/tree/validation.js +184 -0
- package/dist/cjs/tree/vtxoTree.js +197 -0
- package/dist/cjs/utils/bip21.js +114 -0
- package/dist/cjs/utils/coinselect.js +73 -0
- package/dist/cjs/utils/psbt.js +124 -0
- package/dist/cjs/utils/transactionHistory.js +148 -0
- package/dist/cjs/utils/txSizeEstimator.js +95 -0
- package/dist/cjs/wallet/index.js +8 -0
- package/dist/cjs/wallet/serviceWorker/db/vtxo/idb.js +153 -0
- package/dist/cjs/wallet/serviceWorker/db/vtxo/index.js +2 -0
- package/dist/cjs/wallet/serviceWorker/request.js +75 -0
- package/dist/cjs/wallet/serviceWorker/response.js +187 -0
- package/dist/cjs/wallet/serviceWorker/wallet.js +332 -0
- package/dist/cjs/wallet/serviceWorker/worker.js +452 -0
- package/dist/cjs/wallet/wallet.js +720 -0
- package/dist/esm/arknote/index.js +81 -0
- package/dist/esm/forfeit.js +35 -0
- package/dist/esm/identity/inMemoryKey.js +36 -0
- package/dist/esm/identity/index.js +1 -0
- package/dist/esm/index.js +39 -0
- package/dist/esm/musig2/index.js +3 -0
- package/dist/esm/musig2/keys.js +21 -0
- package/dist/esm/musig2/nonces.js +8 -0
- package/dist/esm/musig2/sign.js +63 -0
- package/dist/esm/networks.js +22 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/providers/ark.js +526 -0
- package/dist/esm/providers/onchain.js +57 -0
- package/dist/esm/script/address.js +41 -0
- package/dist/esm/script/base.js +46 -0
- package/dist/esm/script/default.js +37 -0
- package/dist/esm/script/tapscript.js +491 -0
- package/dist/esm/script/vhtlc.js +81 -0
- package/dist/esm/tree/signingSession.js +200 -0
- package/dist/esm/tree/validation.js +179 -0
- package/dist/esm/tree/vtxoTree.js +157 -0
- package/dist/esm/utils/bip21.js +110 -0
- package/dist/esm/utils/coinselect.js +69 -0
- package/dist/esm/utils/psbt.js +118 -0
- package/dist/esm/utils/transactionHistory.js +145 -0
- package/dist/esm/utils/txSizeEstimator.js +91 -0
- package/dist/esm/wallet/index.js +5 -0
- package/dist/esm/wallet/serviceWorker/db/vtxo/idb.js +149 -0
- package/dist/esm/wallet/serviceWorker/db/vtxo/index.js +1 -0
- package/dist/esm/wallet/serviceWorker/request.js +72 -0
- package/dist/esm/wallet/serviceWorker/response.js +184 -0
- package/dist/esm/wallet/serviceWorker/wallet.js +328 -0
- package/dist/esm/wallet/serviceWorker/worker.js +448 -0
- package/dist/esm/wallet/wallet.js +716 -0
- package/dist/types/arknote/index.d.ts +17 -0
- package/dist/types/forfeit.d.ts +15 -0
- package/dist/types/identity/inMemoryKey.d.ts +12 -0
- package/dist/types/identity/index.d.ts +7 -0
- package/dist/types/index.d.ts +22 -0
- package/dist/types/musig2/index.d.ts +4 -0
- package/dist/types/musig2/keys.d.ts +9 -0
- package/dist/types/musig2/nonces.d.ts +13 -0
- package/dist/types/musig2/sign.d.ts +27 -0
- package/dist/types/networks.d.ts +16 -0
- package/dist/types/providers/ark.d.ts +126 -0
- package/dist/types/providers/onchain.d.ts +36 -0
- package/dist/types/script/address.d.ts +10 -0
- package/dist/types/script/base.d.ts +26 -0
- package/dist/types/script/default.d.ts +19 -0
- package/dist/types/script/tapscript.d.ts +94 -0
- package/dist/types/script/vhtlc.d.ts +31 -0
- package/dist/types/tree/signingSession.d.ts +32 -0
- package/dist/types/tree/validation.d.ts +22 -0
- package/dist/types/tree/vtxoTree.d.ts +32 -0
- package/dist/types/utils/bip21.d.ts +21 -0
- package/dist/types/utils/coinselect.d.ts +21 -0
- package/dist/types/utils/psbt.d.ts +11 -0
- package/dist/types/utils/transactionHistory.d.ts +2 -0
- package/dist/types/utils/txSizeEstimator.d.ts +27 -0
- package/dist/types/wallet/index.d.ts +122 -0
- package/dist/types/wallet/serviceWorker/db/vtxo/idb.d.ts +18 -0
- package/dist/types/wallet/serviceWorker/db/vtxo/index.d.ts +12 -0
- package/dist/types/wallet/serviceWorker/request.d.ts +68 -0
- package/dist/types/wallet/serviceWorker/response.d.ts +107 -0
- package/dist/types/wallet/serviceWorker/wallet.d.ts +23 -0
- package/dist/types/wallet/serviceWorker/worker.d.ts +26 -0
- package/dist/types/wallet/wallet.d.ts +42 -0
- package/package.json +88 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EsploraProvider = exports.ESPLORA_URL = void 0;
|
|
4
|
+
exports.ESPLORA_URL = {
|
|
5
|
+
bitcoin: "https://mempool.space/api",
|
|
6
|
+
testnet: "https://mempool.space/testnet/api",
|
|
7
|
+
signet: "https://mempool.space/signet/api",
|
|
8
|
+
mutinynet: "https://mutinynet.com/api",
|
|
9
|
+
regtest: "http://localhost:3000",
|
|
10
|
+
};
|
|
11
|
+
class EsploraProvider {
|
|
12
|
+
constructor(baseUrl) {
|
|
13
|
+
this.baseUrl = baseUrl;
|
|
14
|
+
}
|
|
15
|
+
async getCoins(address) {
|
|
16
|
+
const response = await fetch(`${this.baseUrl}/address/${address}/utxo`);
|
|
17
|
+
if (!response.ok) {
|
|
18
|
+
throw new Error(`Failed to fetch UTXOs: ${response.statusText}`);
|
|
19
|
+
}
|
|
20
|
+
return response.json();
|
|
21
|
+
}
|
|
22
|
+
async getFeeRate() {
|
|
23
|
+
const response = await fetch(`${this.baseUrl}/v1/fees/recommended`);
|
|
24
|
+
if (!response.ok) {
|
|
25
|
+
throw new Error(`Failed to fetch fee rate: ${response.statusText}`);
|
|
26
|
+
}
|
|
27
|
+
const fees = await response.json();
|
|
28
|
+
return fees.halfHourFee; // Return the "medium" priority fee rate
|
|
29
|
+
}
|
|
30
|
+
async broadcastTransaction(txHex) {
|
|
31
|
+
const response = await fetch(`${this.baseUrl}/tx`, {
|
|
32
|
+
method: "POST",
|
|
33
|
+
headers: {
|
|
34
|
+
"Content-Type": "text/plain",
|
|
35
|
+
},
|
|
36
|
+
body: txHex,
|
|
37
|
+
});
|
|
38
|
+
if (!response.ok) {
|
|
39
|
+
const error = await response.text();
|
|
40
|
+
throw new Error(`Failed to broadcast transaction: ${error}`);
|
|
41
|
+
}
|
|
42
|
+
return response.text(); // Returns the txid
|
|
43
|
+
}
|
|
44
|
+
async getTxOutspends(txid) {
|
|
45
|
+
const response = await fetch(`${this.baseUrl}/tx/${txid}/outspends`);
|
|
46
|
+
if (!response.ok) {
|
|
47
|
+
const error = await response.text();
|
|
48
|
+
throw new Error(`Failed to get transaction outspends: ${error}`);
|
|
49
|
+
}
|
|
50
|
+
return response.json();
|
|
51
|
+
}
|
|
52
|
+
async getTransactions(address) {
|
|
53
|
+
const response = await fetch(`${this.baseUrl}/address/${address}/txs`);
|
|
54
|
+
if (!response.ok) {
|
|
55
|
+
const error = await response.text();
|
|
56
|
+
throw new Error(`Failed to get transactions: ${error}`);
|
|
57
|
+
}
|
|
58
|
+
return response.json();
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
exports.EsploraProvider = EsploraProvider;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ArkAddress = void 0;
|
|
4
|
+
const base_1 = require("@scure/base");
|
|
5
|
+
const btc_signer_1 = require("@scure/btc-signer");
|
|
6
|
+
// ArkAddress is a bech32m encoded address with a custom HRP (ark/tark)
|
|
7
|
+
class ArkAddress {
|
|
8
|
+
constructor(serverPubKey, tweakedPubKey, hrp) {
|
|
9
|
+
this.serverPubKey = serverPubKey;
|
|
10
|
+
this.tweakedPubKey = tweakedPubKey;
|
|
11
|
+
this.hrp = hrp;
|
|
12
|
+
if (serverPubKey.length !== 32) {
|
|
13
|
+
throw new Error("Invalid server public key length");
|
|
14
|
+
}
|
|
15
|
+
if (tweakedPubKey.length !== 32) {
|
|
16
|
+
throw new Error("Invalid tweaked public key length");
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
static decode(address) {
|
|
20
|
+
const decoded = base_1.bech32m.decodeUnsafe(address, 1023);
|
|
21
|
+
if (!decoded) {
|
|
22
|
+
throw new Error("Invalid address");
|
|
23
|
+
}
|
|
24
|
+
const data = new Uint8Array(base_1.bech32m.fromWords(decoded.words));
|
|
25
|
+
// First 32 bytes are server pubkey, next 32 bytes are tweaked pubkey
|
|
26
|
+
if (data.length !== 64) {
|
|
27
|
+
throw new Error("Invalid data length");
|
|
28
|
+
}
|
|
29
|
+
const serverPubKey = data.slice(0, 32);
|
|
30
|
+
const tweakedPubKey = data.slice(32, 64);
|
|
31
|
+
return new ArkAddress(serverPubKey, tweakedPubKey, decoded.prefix);
|
|
32
|
+
}
|
|
33
|
+
encode() {
|
|
34
|
+
// Combine server pubkey and tweaked pubkey
|
|
35
|
+
const data = new Uint8Array(64);
|
|
36
|
+
data.set(this.serverPubKey, 0);
|
|
37
|
+
data.set(this.tweakedPubKey, 32);
|
|
38
|
+
const words = base_1.bech32m.toWords(data);
|
|
39
|
+
return base_1.bech32m.encode(this.hrp, words, 1023);
|
|
40
|
+
}
|
|
41
|
+
get pkScript() {
|
|
42
|
+
return btc_signer_1.Script.encode(["OP_1", this.tweakedPubKey]);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
exports.ArkAddress = ArkAddress;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VtxoScript = void 0;
|
|
4
|
+
exports.scriptFromTapLeafScript = scriptFromTapLeafScript;
|
|
5
|
+
const payment_1 = require("@scure/btc-signer/payment");
|
|
6
|
+
const utils_1 = require("@scure/btc-signer/utils");
|
|
7
|
+
const address_1 = require("./address");
|
|
8
|
+
const btc_signer_1 = require("@scure/btc-signer");
|
|
9
|
+
const base_1 = require("@scure/base");
|
|
10
|
+
function scriptFromTapLeafScript(leaf) {
|
|
11
|
+
return leaf[1].subarray(0, leaf[1].length - 1); // remove the version byte
|
|
12
|
+
}
|
|
13
|
+
class VtxoScript {
|
|
14
|
+
static decode(scripts) {
|
|
15
|
+
return new VtxoScript(scripts.map(base_1.hex.decode));
|
|
16
|
+
}
|
|
17
|
+
constructor(scripts) {
|
|
18
|
+
this.scripts = scripts;
|
|
19
|
+
const tapTree = (0, payment_1.taprootListToTree)(scripts.map((script) => ({ script, leafVersion: payment_1.TAP_LEAF_VERSION })));
|
|
20
|
+
const payment = (0, payment_1.p2tr)(utils_1.TAPROOT_UNSPENDABLE_KEY, tapTree, undefined, true);
|
|
21
|
+
if (!payment.tapLeafScript ||
|
|
22
|
+
payment.tapLeafScript.length !== scripts.length) {
|
|
23
|
+
throw new Error("invalid scripts");
|
|
24
|
+
}
|
|
25
|
+
this.leaves = payment.tapLeafScript;
|
|
26
|
+
this.tweakedPublicKey = payment.tweakedPubkey;
|
|
27
|
+
}
|
|
28
|
+
encode() {
|
|
29
|
+
return this.scripts.map(base_1.hex.encode);
|
|
30
|
+
}
|
|
31
|
+
address(prefix, serverPubKey) {
|
|
32
|
+
return new address_1.ArkAddress(serverPubKey, this.tweakedPublicKey, prefix);
|
|
33
|
+
}
|
|
34
|
+
get pkScript() {
|
|
35
|
+
return btc_signer_1.Script.encode(["OP_1", this.tweakedPublicKey]);
|
|
36
|
+
}
|
|
37
|
+
onchainAddress(network) {
|
|
38
|
+
return (0, payment_1.Address)(network).encode({
|
|
39
|
+
type: "tr",
|
|
40
|
+
pubkey: this.tweakedPublicKey,
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
findLeaf(scriptHex) {
|
|
44
|
+
const leaf = this.leaves.find((leaf) => base_1.hex.encode(scriptFromTapLeafScript(leaf)) === scriptHex);
|
|
45
|
+
if (!leaf) {
|
|
46
|
+
throw new Error(`leaf '${scriptHex}' not found`);
|
|
47
|
+
}
|
|
48
|
+
return leaf;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.VtxoScript = VtxoScript;
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DefaultVtxo = void 0;
|
|
4
|
+
const base_1 = require("./base");
|
|
5
|
+
const tapscript_1 = require("./tapscript");
|
|
6
|
+
const base_2 = require("@scure/base");
|
|
7
|
+
// DefaultVtxo is the default implementation of a VtxoScript.
|
|
8
|
+
// it contains 1 forfeit path and 1 exit path.
|
|
9
|
+
// forfeit = (Alice + Server)
|
|
10
|
+
// exit = (Alice) after csvTimelock
|
|
11
|
+
var DefaultVtxo;
|
|
12
|
+
(function (DefaultVtxo) {
|
|
13
|
+
class Script extends base_1.VtxoScript {
|
|
14
|
+
constructor(options) {
|
|
15
|
+
const { pubKey, serverPubKey, csvTimelock = Script.DEFAULT_TIMELOCK, } = options;
|
|
16
|
+
const forfeitScript = tapscript_1.MultisigTapscript.encode({
|
|
17
|
+
pubkeys: [pubKey, serverPubKey],
|
|
18
|
+
}).script;
|
|
19
|
+
const exitScript = tapscript_1.CSVMultisigTapscript.encode({
|
|
20
|
+
timelock: csvTimelock,
|
|
21
|
+
pubkeys: [pubKey],
|
|
22
|
+
}).script;
|
|
23
|
+
super([forfeitScript, exitScript]);
|
|
24
|
+
this.options = options;
|
|
25
|
+
this.forfeitScript = base_2.hex.encode(forfeitScript);
|
|
26
|
+
this.exitScript = base_2.hex.encode(exitScript);
|
|
27
|
+
}
|
|
28
|
+
forfeit() {
|
|
29
|
+
return this.findLeaf(this.forfeitScript);
|
|
30
|
+
}
|
|
31
|
+
exit() {
|
|
32
|
+
return this.findLeaf(this.exitScript);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
Script.DEFAULT_TIMELOCK = {
|
|
36
|
+
value: 144n,
|
|
37
|
+
type: "blocks",
|
|
38
|
+
}; // 1 day in blocks
|
|
39
|
+
DefaultVtxo.Script = Script;
|
|
40
|
+
})(DefaultVtxo || (exports.DefaultVtxo = DefaultVtxo = {}));
|