@gardenfi/wallet-connectors 2.5.0 → 3.0.0-beta.10
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/dist/index.cjs +1 -1
- package/dist/index.js +10 -5
- package/dist/index10.cjs +1 -1
- package/dist/index10.js +72 -70
- package/dist/index11.cjs +1 -1
- package/dist/index11.js +82 -40
- package/dist/index12.cjs +1 -1
- package/dist/index12.js +71 -159
- package/dist/index13.cjs +1 -1
- package/dist/index13.js +40 -180
- package/dist/index14.cjs +1 -1
- package/dist/index14.js +169 -3
- package/dist/index15.cjs +1 -1
- package/dist/index15.js +206 -2
- package/dist/index16.cjs +1 -1
- package/dist/index16.js +8 -2
- package/dist/index17.cjs +1 -3
- package/dist/index17.js +2 -114
- package/dist/index18.cjs +1 -1
- package/dist/index18.js +3 -2
- package/dist/index19.cjs +1 -0
- package/dist/index19.js +4 -0
- package/dist/index2.cjs +1 -1
- package/dist/index2.js +5 -5
- package/dist/index20.cjs +1 -0
- package/dist/index20.js +4 -0
- package/dist/index21.cjs +3 -0
- package/dist/index21.js +116 -0
- package/dist/index22.cjs +1 -0
- package/dist/index22.js +4 -0
- package/dist/index5.cjs +1 -1
- package/dist/index5.js +2 -2
- package/dist/index7.cjs +1 -1
- package/dist/index7.js +110 -60
- package/dist/index8.cjs +1 -1
- package/dist/index8.js +90 -72
- package/dist/index9.cjs +1 -1
- package/dist/index9.js +38 -84
- package/dist/src/index.d.ts +1 -0
- package/dist/src/spark/constants.d.ts +14 -0
- package/dist/src/spark/index.d.ts +5 -0
- package/dist/src/spark/providers/sparkWalletsProvider.d.ts +13 -0
- package/dist/src/spark/providers/sparkWalletsProvider.types.d.ts +22 -0
- package/dist/src/spark/providers/xverse/provider.d.ts +28 -0
- package/dist/src/spark/providers/xverse/xverse.types.d.ts +11 -0
- package/dist/src/spark/spark.types.d.ts +51 -0
- package/package.json +4 -3
package/dist/index12.js
CHANGED
|
@@ -1,171 +1,83 @@
|
|
|
1
|
-
var
|
|
2
|
-
throw TypeError(
|
|
1
|
+
var N = (s) => {
|
|
2
|
+
throw TypeError(s);
|
|
3
3
|
};
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
import
|
|
7
|
-
import
|
|
8
|
-
import {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
4
|
+
var k = (s, e, t) => e.has(s) || N("Cannot " + t);
|
|
5
|
+
var i = (s, e, t) => (k(s, e, "read from private field"), t ? t.call(s) : e.get(s)), m = (s, e, t) => e.has(s) ? N("Cannot add the same private member more than once") : e instanceof WeakSet ? e.add(s) : e.set(s, t), f = (s, e, t, r) => (k(s, e, "write to private field"), r ? r.call(s, t) : e.set(s, t), t);
|
|
6
|
+
import { WALLET_CONFIG as u } from "./index3.js";
|
|
7
|
+
import { XverseBitcoinNetworkType as h } from "./index19.js";
|
|
8
|
+
import { Network as l, Err as a, Ok as d, executeWithTryCatch as w } from "@gardenfi/utils";
|
|
9
|
+
var n;
|
|
10
|
+
class T {
|
|
11
|
+
constructor(e) {
|
|
12
|
+
m(this, n);
|
|
13
|
+
this.address = "", this.id = u.Xverse.id, this.name = u.Xverse.name, this.icon = u.Xverse.icon, this.connect = async (t) => {
|
|
14
|
+
try {
|
|
15
|
+
t || (t = l.MAINNET), await i(this, n).request("wallet_connect", null);
|
|
16
|
+
const r = await this.getNetwork();
|
|
17
|
+
if (r.error)
|
|
18
|
+
return a("Could not get network", r.error);
|
|
19
|
+
if (r.val !== t) {
|
|
20
|
+
const c = await this.switchNetwork();
|
|
21
|
+
if (c.error)
|
|
22
|
+
return a("Failed to switch network", c.error);
|
|
23
|
+
}
|
|
24
|
+
const o = await this.getAccounts();
|
|
25
|
+
return o.ok && o.val.length > 0 && (this.address = o.val[0]), d({
|
|
26
|
+
address: this.address,
|
|
27
|
+
provider: this,
|
|
28
|
+
network: t,
|
|
29
|
+
id: u.Xverse.id
|
|
30
|
+
});
|
|
31
|
+
} catch (r) {
|
|
32
|
+
return a("Error while connecting to the XVerse wallet", r);
|
|
33
|
+
}
|
|
34
|
+
}, this.getBalance = async () => await w(async () => (await i(this, n).request("getBalance", {})).result, "Error while getting balance from XVerse wallet"), this.requestAccounts = async () => await w(async () => (await i(this, n).request("getAccounts", {
|
|
35
|
+
purposes: ["payment"],
|
|
36
|
+
message: "I want to connect"
|
|
37
|
+
})).result.map((r) => r.address)), this.getAccounts = async () => await w(async () => (await i(this, n).request("getAddresses", {
|
|
38
|
+
purposes: ["payment"]
|
|
39
|
+
})).result.addresses.map((r) => r.address)), this.sendBitcoin = async (t, r) => await w(async () => {
|
|
40
|
+
var g;
|
|
41
|
+
const o = await i(this, n).request("sendTransfer", {
|
|
42
|
+
recipients: [{ address: t, amount: r }]
|
|
43
|
+
}), c = (g = o.result) == null ? void 0 : g.txid;
|
|
44
|
+
if (c)
|
|
45
|
+
return d(c);
|
|
46
|
+
throw new Error(o.error);
|
|
47
|
+
}, "Error while sending bitcoin from Xverse wallet"), this.on = () => {
|
|
48
|
+
}, this.off = () => {
|
|
49
|
+
}, this.disconnect = () => (this.address = "", Promise.resolve(d("Disconnected"))), f(this, n, e);
|
|
39
50
|
}
|
|
40
51
|
async getNetwork() {
|
|
41
|
-
return w(
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
const t = await this.connect();
|
|
46
|
-
return t.error ? r(
|
|
47
|
-
`Failed to connect to ${n(this, a)}: ${t.error}`
|
|
48
|
-
) : w(n(this, a));
|
|
49
|
-
}
|
|
50
|
-
async getBalance() {
|
|
51
|
-
let t = await this.getAccounts();
|
|
52
|
-
if (!t.ok) return r("Failed to get address");
|
|
53
|
-
this.address = t.val[0];
|
|
54
|
-
const e = await this.getNetwork();
|
|
55
|
-
return e.ok ? (g(this, a, e.val), await A(this.address, n(this, a))) : r("Failed to get network");
|
|
56
|
-
}
|
|
57
|
-
async sendLitecoin(t, e) {
|
|
58
|
-
try {
|
|
59
|
-
const s = await this.getAccounts();
|
|
60
|
-
if (!s.ok) return r("Failed to get address");
|
|
61
|
-
this.address = s.val[0];
|
|
62
|
-
const b = await this.getNetwork();
|
|
63
|
-
if (!b.ok)
|
|
64
|
-
return r("Failed to get network");
|
|
65
|
-
if (g(this, a, b.val), !t || t.trim() === "")
|
|
66
|
-
return r("Invalid destination address");
|
|
67
|
-
if (e <= 0)
|
|
68
|
-
return r("Amount must be greater than 0");
|
|
69
|
-
const l = await this.getBalance();
|
|
70
|
-
if (!l.ok)
|
|
71
|
-
return r("Failed to get balance", l.error);
|
|
72
|
-
if (l.val.total < e)
|
|
73
|
-
return r("Insufficient balance");
|
|
74
|
-
const d = await O(this.address, n(this, a));
|
|
75
|
-
if (!d.ok)
|
|
76
|
-
return r("Failed to fetch UTXOs", d.error);
|
|
77
|
-
const k = d.val;
|
|
78
|
-
if (!k || k.length === 0)
|
|
79
|
-
return r("No UTXOs available");
|
|
80
|
-
const h = await this.buildTransaction(
|
|
81
|
-
k,
|
|
82
|
-
t,
|
|
83
|
-
e,
|
|
84
|
-
this.address
|
|
52
|
+
return await w(async () => {
|
|
53
|
+
const e = await i(this, n).request(
|
|
54
|
+
"wallet_getNetwork",
|
|
55
|
+
null
|
|
85
56
|
);
|
|
86
|
-
if (
|
|
87
|
-
return
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
return c.ok ? w(c.val) : r("Failed to broadcast transaction", c.error);
|
|
93
|
-
} catch (s) {
|
|
94
|
-
return s instanceof Error ? s.message.includes("User rejected") || s.message.includes("cancelled") || s.message.includes("denied") ? r("Transaction cancelled by user") : r("Error while sending Litecoin: " + s.message) : r("Error while sending Litecoin", String(s));
|
|
95
|
-
}
|
|
57
|
+
if (e.result.bitcoin.name === h.Mainnet)
|
|
58
|
+
return l.MAINNET;
|
|
59
|
+
if (e.result.bitcoin.name === h.Testnet4)
|
|
60
|
+
return l.TESTNET;
|
|
61
|
+
throw new Error(e.result);
|
|
62
|
+
}, "Error while getting network from Xverse wallet");
|
|
96
63
|
}
|
|
97
|
-
async
|
|
64
|
+
async switchNetwork() {
|
|
98
65
|
try {
|
|
99
|
-
const
|
|
100
|
-
if (
|
|
101
|
-
return
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
if (c >= s + f * 250) break;
|
|
106
|
-
let y;
|
|
107
|
-
try {
|
|
108
|
-
y = {
|
|
109
|
-
script: T.address.toOutputScript(
|
|
110
|
-
b,
|
|
111
|
-
l
|
|
112
|
-
),
|
|
113
|
-
value: u.value
|
|
114
|
-
};
|
|
115
|
-
} catch {
|
|
116
|
-
if (u.scriptPubKey)
|
|
117
|
-
y = {
|
|
118
|
-
script: Buffer.from(u.scriptPubKey, "hex"),
|
|
119
|
-
value: u.value
|
|
120
|
-
};
|
|
121
|
-
else
|
|
122
|
-
throw new Error("Cannot determine script for UTXO");
|
|
123
|
-
}
|
|
124
|
-
d.addInput({
|
|
125
|
-
hash: u.txid,
|
|
126
|
-
index: u.vout,
|
|
127
|
-
witnessUtxo: y
|
|
128
|
-
}), c += u.value;
|
|
129
|
-
}
|
|
130
|
-
if (c === 0)
|
|
131
|
-
return r("No valid UTXOs found");
|
|
132
|
-
const E = (d.txInputs.length * 148 + 2 * 34 + 10) * f;
|
|
133
|
-
if (c < s + E)
|
|
134
|
-
return r(
|
|
135
|
-
`Insufficient funds: need ${s + E} sats, have ${c} sats`
|
|
136
|
-
);
|
|
137
|
-
d.addOutput({
|
|
138
|
-
address: e,
|
|
139
|
-
value: s
|
|
66
|
+
const e = await this.getNetwork();
|
|
67
|
+
if (e.error)
|
|
68
|
+
return a("Failed to get current network");
|
|
69
|
+
const t = e.val === l.MAINNET ? h.Testnet4 : h.Mainnet;
|
|
70
|
+
await i(this, n).request("wallet_changeNetwork", {
|
|
71
|
+
name: t
|
|
140
72
|
});
|
|
141
|
-
const
|
|
142
|
-
return
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
}), w(d.toBase64());
|
|
146
|
-
} catch (l) {
|
|
147
|
-
return r("Error building transaction", String(l));
|
|
73
|
+
const r = await this.getNetwork();
|
|
74
|
+
return r.ok ? d(r.val) : a("Failed to verify network switch");
|
|
75
|
+
} catch (e) {
|
|
76
|
+
return a("Error while switching network in Xverse:", e);
|
|
148
77
|
}
|
|
149
78
|
}
|
|
150
|
-
async signPsbt(t) {
|
|
151
|
-
return await x(async () => await n(this, o).signPsbt(t), "Error while signing transaction");
|
|
152
|
-
}
|
|
153
|
-
async broadcastTransaction(t) {
|
|
154
|
-
const e = typeof n(this, o).pushPsbt == "function" ? n(this, o).pushPsbt.bind(n(this, o)) : void 0;
|
|
155
|
-
return await S(
|
|
156
|
-
t,
|
|
157
|
-
n(this, a),
|
|
158
|
-
e
|
|
159
|
-
);
|
|
160
|
-
}
|
|
161
|
-
on(t, e) {
|
|
162
|
-
n(this, o).on(t, e);
|
|
163
|
-
}
|
|
164
|
-
off(t, e) {
|
|
165
|
-
n(this, o).off(t, e);
|
|
166
|
-
}
|
|
167
79
|
}
|
|
168
|
-
|
|
80
|
+
n = new WeakMap();
|
|
169
81
|
export {
|
|
170
|
-
|
|
82
|
+
T as XverseProvider
|
|
171
83
|
};
|
package/dist/index13.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var
|
|
1
|
+
"use strict";var o=r=>{throw TypeError(r)};var l=(r,e,t)=>e.has(r)||o("Cannot "+t);var s=(r,e,t)=>(l(r,e,"read from private field"),t?t.call(r):e.get(r)),u=(r,e,t)=>e.has(r)?o("Cannot add the same private member more than once"):e instanceof WeakSet?e.add(r):e.set(r,t),h=(r,e,t,c)=>(l(r,e,"write to private field"),c?c.call(r,t):e.set(r,t),t);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const w=require("./index20.cjs"),a=require("./index3.cjs"),n=require("@gardenfi/utils");var i;class p{constructor(e){u(this,i);this.address="",this.id=a.WALLET_CONFIG.Keplr.id,this.name=a.WALLET_CONFIG.Keplr.name,this.icon=a.WALLET_CONFIG.Keplr.icon,h(this,i,e)}async connect(e){if(e||(e=n.Network.MAINNET),e===n.Network.TESTNET)return n.Err("Keplr wallet does not support testnet4");try{const t=await s(this,i).requestAccounts();return t.length>0&&(this.address=t[0]),n.Ok({address:this.address,provider:this,network:e,id:a.WALLET_CONFIG.Keplr.id})}catch(t){return n.Err("Error while connecting to the Keplr wallet",t)}}async requestAccounts(){return await n.executeWithTryCatch(async()=>await s(this,i).requestAccounts(),"Error while requesting accounts from the Keplr wallet")}async getAccounts(){return await n.executeWithTryCatch(async()=>await s(this,i).getAccounts(),"Error while getting accounts from the Keplr wallet")}async getNetwork(){return await n.executeWithTryCatch(async()=>{const e=await s(this,i).getChain();if(e.enum===w.KeplrBitcoinChainType.MAINNET)return n.Network.MAINNET;if(e.enum===w.KeplrBitcoinChainType.TESTNET)return n.Network.TESTNET;throw new Error("Invalid or unsupported network"+e.enum)},"Error while getting network from the Keplr wallet")}async switchNetwork(){return n.Err("Keplr wallet does not support testnet4")}async getBalance(){return await n.executeWithTryCatch(async()=>await s(this,i).getBalance(),"Error while getting balance from Keplr wallet")}async sendBitcoin(e,t){return await n.executeWithTryCatch(async()=>await s(this,i).sendBitcoin(e,t),"Error while sending bitcoin from Keplr wallet")}on(e,t){s(this,i).on(e,t)}off(e,t){s(this,i).off(e,t)}async disconnect(){return await s(this,i).disconnect(),this.address="",n.Ok("Disconnected Keplr wallet")}}i=new WeakMap;exports.KeplrProvider=p;
|
package/dist/index13.js
CHANGED
|
@@ -1,208 +1,68 @@
|
|
|
1
|
-
var
|
|
2
|
-
throw TypeError(
|
|
1
|
+
var u = (r) => {
|
|
2
|
+
throw TypeError(r);
|
|
3
3
|
};
|
|
4
|
-
var
|
|
5
|
-
var
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
import { getBalance as O, getLitecoinNetwork as R, getRecommendedFeeRate as S, broadcastTransaction as A } from "./index17.js";
|
|
12
|
-
x.initEccLib(I);
|
|
13
|
-
var n, o;
|
|
14
|
-
class j {
|
|
4
|
+
var h = (r, t, e) => t.has(r) || u("Cannot " + e);
|
|
5
|
+
var s = (r, t, e) => (h(r, t, "read from private field"), e ? e.call(r) : t.get(r)), w = (r, t, e) => t.has(r) ? u("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(r) : t.set(r, e), p = (r, t, e, l) => (h(r, t, "write to private field"), l ? l.call(r, e) : t.set(r, e), e);
|
|
6
|
+
import { KeplrBitcoinChainType as d } from "./index20.js";
|
|
7
|
+
import { WALLET_CONFIG as a } from "./index3.js";
|
|
8
|
+
import { Network as o, Err as c, Ok as E, executeWithTryCatch as i } from "@gardenfi/utils";
|
|
9
|
+
var n;
|
|
10
|
+
class K {
|
|
15
11
|
constructor(t) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
this.address = "", this.id = N.LiteScribe.id, this.name = N.LiteScribe.name, this.icon = N.LiteScribe.icon, k(this, n, t), k(this, o, p.TESTNET);
|
|
12
|
+
w(this, n);
|
|
13
|
+
this.address = "", this.id = a.Keplr.id, this.name = a.Keplr.name, this.icon = a.Keplr.icon, p(this, n, t);
|
|
19
14
|
}
|
|
20
15
|
async connect(t) {
|
|
16
|
+
if (t || (t = o.MAINNET), t === o.TESTNET)
|
|
17
|
+
return c("Keplr wallet does not support testnet4");
|
|
21
18
|
try {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
if (!e.ok)
|
|
25
|
-
return r("Could not get network", e.error);
|
|
26
|
-
if (e.val !== t) {
|
|
27
|
-
const l = await this.switchNetwork();
|
|
28
|
-
if (!l.ok)
|
|
29
|
-
return r("Failed to switch network", l.error);
|
|
30
|
-
}
|
|
31
|
-
const s = await i(this, n).requestAccounts();
|
|
32
|
-
return s.length > 0 && (this.address = s[0]), v({
|
|
19
|
+
const e = await s(this, n).requestAccounts();
|
|
20
|
+
return e.length > 0 && (this.address = e[0]), E({
|
|
33
21
|
address: this.address,
|
|
34
22
|
provider: this,
|
|
35
23
|
network: t,
|
|
36
|
-
id:
|
|
24
|
+
id: a.Keplr.id
|
|
37
25
|
});
|
|
38
26
|
} catch (e) {
|
|
39
|
-
return
|
|
27
|
+
return c("Error while connecting to the Keplr wallet", e);
|
|
40
28
|
}
|
|
41
29
|
}
|
|
42
|
-
async getBalance() {
|
|
43
|
-
let t = await this.getAccounts();
|
|
44
|
-
if (!t.ok) return r("Failed to get address");
|
|
45
|
-
this.address = t.val[0];
|
|
46
|
-
const e = await this.getNetwork();
|
|
47
|
-
return e.ok ? (k(this, o, e.val), await O(this.address, i(this, o))) : r("Failed to get network");
|
|
48
|
-
}
|
|
49
30
|
async requestAccounts() {
|
|
50
|
-
return await
|
|
31
|
+
return await i(async () => await s(this, n).requestAccounts(), "Error while requesting accounts from the Keplr wallet");
|
|
51
32
|
}
|
|
52
33
|
async getAccounts() {
|
|
53
|
-
return await
|
|
54
|
-
}
|
|
55
|
-
async sendLitecoin(t, e) {
|
|
56
|
-
try {
|
|
57
|
-
const s = await this.getAccounts();
|
|
58
|
-
if (!s.ok) return r("Failed to get address");
|
|
59
|
-
this.address = s.val[0];
|
|
60
|
-
const l = await this.getNetwork();
|
|
61
|
-
if (!l.ok)
|
|
62
|
-
return r("Failed to get network");
|
|
63
|
-
if (k(this, o, l.val), !t || t.trim() === "")
|
|
64
|
-
return r("Invalid destination address");
|
|
65
|
-
if (e <= 0)
|
|
66
|
-
return r("Amount must be greater than 0");
|
|
67
|
-
const h = await this.getBalance();
|
|
68
|
-
if (!h.ok)
|
|
69
|
-
return r("Failed to get balance", h.error);
|
|
70
|
-
if (h.val.total < e)
|
|
71
|
-
return r("Insufficient balance");
|
|
72
|
-
const u = await this.getUTXOs();
|
|
73
|
-
if (!u.ok)
|
|
74
|
-
return r("Failed to fetch UTXOs", u.error);
|
|
75
|
-
const g = u.val;
|
|
76
|
-
if (!g || g.length === 0)
|
|
77
|
-
return r("No UTXOs available");
|
|
78
|
-
const w = await this.buildTransaction(
|
|
79
|
-
g,
|
|
80
|
-
t,
|
|
81
|
-
e,
|
|
82
|
-
this.address
|
|
83
|
-
);
|
|
84
|
-
if (!w.ok)
|
|
85
|
-
return r("Failed to build transaction", w.error);
|
|
86
|
-
console.log("psbtResult", w.val);
|
|
87
|
-
const f = await this.signPsbt(w.val);
|
|
88
|
-
if (!f.ok)
|
|
89
|
-
return r("Failed to sign transaction", f.error);
|
|
90
|
-
const c = await this.broadcastTransaction(f.val);
|
|
91
|
-
return c.ok ? v(c.val) : r("Failed to broadcast transaction", c.error);
|
|
92
|
-
} catch (s) {
|
|
93
|
-
return s instanceof Error ? s.message.includes("User rejected") || s.message.includes("cancelled") || s.message.includes("denied") ? r("Transaction cancelled by user") : r("Error while sending Litecoin: " + s.message) : r("Error while sending Litecoin", String(s));
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
async getUTXOs() {
|
|
97
|
-
return await T(async () => {
|
|
98
|
-
if (typeof i(this, n).getUtxos == "function")
|
|
99
|
-
return await i(this, n).getUtxos();
|
|
100
|
-
const t = i(this, o) === p.MAINNET ? `https://litecoinspace.org/api/address/${this.address}/utxo` : `https://litecoinspace.org/testnet/api/address/${this.address}/utxo`, e = await fetch(t);
|
|
101
|
-
if (!e.ok)
|
|
102
|
-
throw new Error(`Failed to fetch UTXOs: ${e.statusText}`);
|
|
103
|
-
return await e.json();
|
|
104
|
-
}, "Error while fetching UTXOs");
|
|
105
|
-
}
|
|
106
|
-
async buildTransaction(t, e, s, l) {
|
|
107
|
-
try {
|
|
108
|
-
const h = R(i(this, o)), u = new x.Psbt({ network: h }), g = t.sort((d, b) => b.value - d.value), w = await S(i(this, o));
|
|
109
|
-
if (!w.ok)
|
|
110
|
-
return r("Failed to fetch recommended fee rate", w.error);
|
|
111
|
-
const f = w.val;
|
|
112
|
-
let c = 0;
|
|
113
|
-
for (const d of g) {
|
|
114
|
-
if (c >= s + f * 250) break;
|
|
115
|
-
let b;
|
|
116
|
-
try {
|
|
117
|
-
b = {
|
|
118
|
-
script: x.address.toOutputScript(
|
|
119
|
-
l,
|
|
120
|
-
h
|
|
121
|
-
),
|
|
122
|
-
value: d.value
|
|
123
|
-
};
|
|
124
|
-
} catch {
|
|
125
|
-
if (d.scriptPubKey)
|
|
126
|
-
b = {
|
|
127
|
-
script: Buffer.from(d.scriptPubKey, "hex"),
|
|
128
|
-
value: d.value
|
|
129
|
-
};
|
|
130
|
-
else
|
|
131
|
-
throw new Error("Cannot determine script for UTXO");
|
|
132
|
-
}
|
|
133
|
-
u.addInput({
|
|
134
|
-
hash: d.txid,
|
|
135
|
-
index: d.vout,
|
|
136
|
-
witnessUtxo: b
|
|
137
|
-
}), c += d.value;
|
|
138
|
-
}
|
|
139
|
-
if (c === 0)
|
|
140
|
-
return r("No valid UTXOs found");
|
|
141
|
-
const m = (u.txInputs.length * 148 + 2 * 34 + 10) * f;
|
|
142
|
-
if (c < s + m)
|
|
143
|
-
return r(
|
|
144
|
-
`Insufficient funds: need ${s + m} sats, have ${c} sats`
|
|
145
|
-
);
|
|
146
|
-
u.addOutput({
|
|
147
|
-
address: e,
|
|
148
|
-
value: s
|
|
149
|
-
});
|
|
150
|
-
const F = c - s - m;
|
|
151
|
-
return F > 546 && u.addOutput({
|
|
152
|
-
address: l,
|
|
153
|
-
value: F
|
|
154
|
-
}), v(u.toBase64());
|
|
155
|
-
} catch (h) {
|
|
156
|
-
return r("Error building transaction", String(h));
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
async signPsbt(t) {
|
|
160
|
-
return await T(async () => await i(this, n).signPsbt(t), "Error while signing transaction");
|
|
161
|
-
}
|
|
162
|
-
async broadcastTransaction(t) {
|
|
163
|
-
const e = typeof i(this, n).pushPsbt == "function" ? i(this, n).pushPsbt.bind(
|
|
164
|
-
i(this, n)
|
|
165
|
-
) : void 0;
|
|
166
|
-
return await A(
|
|
167
|
-
t,
|
|
168
|
-
i(this, o),
|
|
169
|
-
e
|
|
170
|
-
);
|
|
34
|
+
return await i(async () => await s(this, n).getAccounts(), "Error while getting accounts from the Keplr wallet");
|
|
171
35
|
}
|
|
172
36
|
async getNetwork() {
|
|
173
|
-
return await
|
|
174
|
-
const t = await
|
|
175
|
-
if (t ===
|
|
176
|
-
return
|
|
177
|
-
if (t ===
|
|
178
|
-
return
|
|
179
|
-
throw new Error("Invalid or unsupported network" + t);
|
|
180
|
-
}, "Error while getting network from
|
|
37
|
+
return await i(async () => {
|
|
38
|
+
const t = await s(this, n).getChain();
|
|
39
|
+
if (t.enum === d.MAINNET)
|
|
40
|
+
return o.MAINNET;
|
|
41
|
+
if (t.enum === d.TESTNET)
|
|
42
|
+
return o.TESTNET;
|
|
43
|
+
throw new Error("Invalid or unsupported network" + t.enum);
|
|
44
|
+
}, "Error while getting network from the Keplr wallet");
|
|
181
45
|
}
|
|
182
46
|
async switchNetwork() {
|
|
183
|
-
|
|
184
|
-
const t = await this.getNetwork();
|
|
185
|
-
if (t.error)
|
|
186
|
-
return r("Failed to get current network");
|
|
187
|
-
const e = t.val === p.MAINNET ? E.TESTNET : E.LIVENET;
|
|
188
|
-
await i(this, n).switchNetwork(e);
|
|
189
|
-
const s = await this.getNetwork();
|
|
190
|
-
return s.ok ? v(s.val) : r("Failed to verify network switch");
|
|
191
|
-
} catch (t) {
|
|
192
|
-
return r("Error while switching network in litescribe:", t);
|
|
193
|
-
}
|
|
47
|
+
return c("Keplr wallet does not support testnet4");
|
|
194
48
|
}
|
|
195
|
-
async
|
|
196
|
-
return
|
|
49
|
+
async getBalance() {
|
|
50
|
+
return await i(async () => await s(this, n).getBalance(), "Error while getting balance from Keplr wallet");
|
|
51
|
+
}
|
|
52
|
+
async sendBitcoin(t, e) {
|
|
53
|
+
return await i(async () => await s(this, n).sendBitcoin(t, e), "Error while sending bitcoin from Keplr wallet");
|
|
197
54
|
}
|
|
198
55
|
on(t, e) {
|
|
199
|
-
|
|
56
|
+
s(this, n).on(t, e);
|
|
200
57
|
}
|
|
201
58
|
off(t, e) {
|
|
202
|
-
|
|
59
|
+
s(this, n).off(t, e);
|
|
60
|
+
}
|
|
61
|
+
async disconnect() {
|
|
62
|
+
return await s(this, n).disconnect(), this.address = "", E("Disconnected Keplr wallet");
|
|
203
63
|
}
|
|
204
64
|
}
|
|
205
|
-
n = new WeakMap()
|
|
65
|
+
n = new WeakMap();
|
|
206
66
|
export {
|
|
207
|
-
|
|
67
|
+
K as KeplrProvider
|
|
208
68
|
};
|
package/dist/index14.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});
|
|
1
|
+
"use strict";var O=s=>{throw TypeError(s)};var m=(s,t,r)=>t.has(s)||O("Cannot "+r);var i=(s,t,r)=>(m(s,t,"read from private field"),r?r.call(s):t.get(s)),v=(s,t,r)=>t.has(s)?O("Cannot add the same private member more than once"):t instanceof WeakSet?t.add(s):t.set(s,r),E=(s,t,r,n)=>(m(s,t,"write to private field"),n?n.call(s,r):t.set(s,r),r);Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const I=require("bitcoinjs-lib"),A=require("tiny-secp256k1"),e=require("@gardenfi/utils"),p=require("./index6.cjs"),b=require("./index21.cjs");function P(s){const t=Object.create(null,{[Symbol.toStringTag]:{value:"Module"}});if(s){for(const r in s)if(r!=="default"){const n=Object.getOwnPropertyDescriptor(s,r);Object.defineProperty(t,r,n.get?n:{enumerable:!0,get:()=>s[r]})}}return t.default=s,Object.freeze(t)}const N=P(I),L=P(A);N.initEccLib(L);var o,a;class x{constructor(t){v(this,o);v(this,a);this.address="",this.id=p.WALLET_CONFIG.Enkrypt.id,this.name=p.WALLET_CONFIG.Enkrypt.name,this.icon=p.WALLET_CONFIG.Enkrypt.icon,this.disconnect=()=>(this.address="",Promise.resolve(e.Ok("Disconnected Enkrypt wallet"))),E(this,o,t),E(this,a,e.Network.MAINNET)}async connect(t){if(t||(t=e.Network.MAINNET),t===e.Network.TESTNET)return e.Err("Enkrypt wallet does not support testnet");try{const r=await i(this,o).requestAccounts();return r.length>0&&(this.address=r[0]),e.Ok({address:this.address,provider:this,network:t,id:p.WALLET_CONFIG.Enkrypt.id})}catch(r){return e.Err("Error while connecting to Enkrypt wallet: "+r)}}async requestAccounts(){return await e.executeWithTryCatch(async()=>await i(this,o).requestAccounts(),"Error while requesting accounts from the Enkrypt wallet")}async getAccounts(){return this.requestAccounts()}async getNetwork(){return e.Ok(i(this,a))}async switchNetwork(){E(this,a,i(this,a)===e.Network.MAINNET?e.Network.TESTNET:e.Network.MAINNET);const t=await this.connect();return t.error?e.Err(`Failed to connect to ${i(this,a)}: ${t.error}`):e.Ok(i(this,a))}async getBalance(){let t=await this.getAccounts();if(!t.ok)return e.Err("Failed to get address");this.address=t.val[0];const r=await this.getNetwork();return r.ok?(E(this,a,r.val),await b.getBalance(this.address,i(this,a))):e.Err("Failed to get network")}async sendLitecoin(t,r){try{const n=await this.getAccounts();if(!n.ok)return e.Err("Failed to get address");this.address=n.val[0];const g=await this.getNetwork();if(!g.ok)return e.Err("Failed to get network");if(E(this,a,g.val),!t||t.trim()==="")return e.Err("Invalid destination address");if(r<=0)return e.Err("Amount must be greater than 0");const l=await this.getBalance();if(!l.ok)return e.Err("Failed to get balance",l.error);if(l.val.total<r)return e.Err("Insufficient balance");const u=await b.getUTXOs(this.address,i(this,a));if(!u.ok)return e.Err("Failed to fetch UTXOs",u.error);const w=u.val;if(!w||w.length===0)return e.Err("No UTXOs available");const h=await this.buildTransaction(w,t,r,this.address);if(!h.ok)return e.Err("Failed to build transaction",h.error);const f=await this.signPsbt(h.val);if(!f.ok)return e.Err("Failed to sign transaction",f.error);const c=await this.broadcastTransaction(f.val);return c.ok?e.Ok(c.val):e.Err("Failed to broadcast transaction",c.error)}catch(n){return n instanceof Error?n.message.includes("User rejected")||n.message.includes("cancelled")||n.message.includes("denied")?e.Err("Transaction cancelled by user"):e.Err("Error while sending Litecoin: "+n.message):e.Err("Error while sending Litecoin",String(n))}}async buildTransaction(t,r,n,g){try{const l=b.getLitecoinNetwork(i(this,a)),u=new N.Psbt({network:l}),w=t.sort((d,k)=>k.value-d.value),h=await b.getRecommendedFeeRate(i(this,a));if(!h.ok)return e.Err("Failed to fetch recommended fee rate",h.error);const f=h.val;let c=0;for(const d of w){if(c>=n+f*250)break;let k;try{k={script:N.address.toOutputScript(g,l),value:d.value}}catch{if(d.scriptPubKey)k={script:Buffer.from(d.scriptPubKey,"hex"),value:d.value};else throw new Error("Cannot determine script for UTXO")}u.addInput({hash:d.txid,index:d.vout,witnessUtxo:k}),c+=d.value}if(c===0)return e.Err("No valid UTXOs found");const y=(u.txInputs.length*148+2*34+10)*f;if(c<n+y)return e.Err(`Insufficient funds: need ${n+y} sats, have ${c} sats`);u.addOutput({address:r,value:n});const T=c-n-y;return T>546&&u.addOutput({address:g,value:T}),e.Ok(u.toBase64())}catch(l){return e.Err("Error building transaction",String(l))}}async signPsbt(t){return await e.executeWithTryCatch(async()=>await i(this,o).signPsbt(t),"Error while signing transaction")}async broadcastTransaction(t){const r=typeof i(this,o).pushPsbt=="function"?i(this,o).pushPsbt.bind(i(this,o)):void 0;return await b.broadcastTransaction(t,i(this,a),r)}on(t,r){i(this,o).on(t,r)}off(t,r){i(this,o).off(t,r)}}o=new WeakMap,a=new WeakMap;exports.EnkryptProvider=x;
|