@dynamic-labs/embedded-wallet-bitcoin 4.18.7
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/CHANGELOG.md +5475 -0
- package/LICENSE +21 -0
- package/README.md +11 -0
- package/_virtual/_tslib.cjs +36 -0
- package/_virtual/_tslib.js +32 -0
- package/package.cjs +8 -0
- package/package.js +4 -0
- package/package.json +35 -0
- package/src/TurnkeyBitcoinWalletConnectors.cjs +23 -0
- package/src/TurnkeyBitcoinWalletConnectors.d.ts +2 -0
- package/src/TurnkeyBitcoinWalletConnectors.js +19 -0
- package/src/index.cjs +12 -0
- package/src/index.d.ts +1 -0
- package/src/index.js +6 -0
- package/src/lib/TurnkeyBitcoinWalletConnector/TurnkeyBitcoinWalletConnector.cjs +360 -0
- package/src/lib/TurnkeyBitcoinWalletConnector/TurnkeyBitcoinWalletConnector.d.ts +47 -0
- package/src/lib/TurnkeyBitcoinWalletConnector/TurnkeyBitcoinWalletConnector.js +352 -0
- package/src/lib/TurnkeyBitcoinWalletConnector/fees.cjs +50 -0
- package/src/lib/TurnkeyBitcoinWalletConnector/fees.d.ts +15 -0
- package/src/lib/TurnkeyBitcoinWalletConnector/fees.js +46 -0
- package/src/lib/TurnkeyBitcoinWalletConnector/index.d.ts +1 -0
- package/src/lib/TurnkeyBitcoinWalletConnector/signer.cjs +51 -0
- package/src/lib/TurnkeyBitcoinWalletConnector/signer.d.ts +23 -0
- package/src/lib/TurnkeyBitcoinWalletConnector/signer.js +47 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Dynamic Labs, Inc.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
# embedded-wallet-bitcoin
|
|
2
|
+
|
|
3
|
+
This library was generated with [Nx](https://nx.dev).
|
|
4
|
+
|
|
5
|
+
## Building
|
|
6
|
+
|
|
7
|
+
Run `nx build embedded-wallet-bitcoin` to build the library.
|
|
8
|
+
|
|
9
|
+
## Running unit tests
|
|
10
|
+
|
|
11
|
+
Run `nx test embedded-wallet-bitcoin` to execute the unit tests via [Jest](https://jestjs.io).
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
/******************************************************************************
|
|
7
|
+
Copyright (c) Microsoft Corporation.
|
|
8
|
+
|
|
9
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
10
|
+
purpose with or without fee is hereby granted.
|
|
11
|
+
|
|
12
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
13
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
14
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
15
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
16
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
17
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
18
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
19
|
+
***************************************************************************** */
|
|
20
|
+
|
|
21
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
22
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
23
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
24
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
25
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
26
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
27
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
32
|
+
var e = new Error(message);
|
|
33
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
exports.__awaiter = __awaiter;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
/******************************************************************************
|
|
3
|
+
Copyright (c) Microsoft Corporation.
|
|
4
|
+
|
|
5
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
|
6
|
+
purpose with or without fee is hereby granted.
|
|
7
|
+
|
|
8
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
|
9
|
+
REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
10
|
+
AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
11
|
+
INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
12
|
+
LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
|
13
|
+
OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
14
|
+
PERFORMANCE OF THIS SOFTWARE.
|
|
15
|
+
***************************************************************************** */
|
|
16
|
+
|
|
17
|
+
function __awaiter(thisArg, _arguments, P, generator) {
|
|
18
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
20
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
21
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
22
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
23
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
|
|
28
|
+
var e = new Error(message);
|
|
29
|
+
return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export { __awaiter };
|
package/package.cjs
ADDED
package/package.js
ADDED
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dynamic-labs/embedded-wallet-bitcoin",
|
|
3
|
+
"version": "4.18.7",
|
|
4
|
+
"description": "A React SDK for implementing wallet web3 authentication and authorization to your website.",
|
|
5
|
+
"author": "Dynamic Labs, Inc.",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"main": "./src/index.cjs",
|
|
8
|
+
"module": "./src/index.js",
|
|
9
|
+
"types": "./src/index.d.ts",
|
|
10
|
+
"type": "module",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": {
|
|
13
|
+
"types": "./src/index.d.ts",
|
|
14
|
+
"import": "./src/index.js",
|
|
15
|
+
"require": "./src/index.cjs"
|
|
16
|
+
},
|
|
17
|
+
"./package.json": "./package.json"
|
|
18
|
+
},
|
|
19
|
+
"homepage": "https://www.dynamic.xyz/",
|
|
20
|
+
"dependencies": {
|
|
21
|
+
"@bitcoinerlab/secp256k1": "1.1.1",
|
|
22
|
+
"@dynamic-labs/bitcoin": "4.18.7",
|
|
23
|
+
"@dynamic-labs/sdk-api-core": "0.0.672",
|
|
24
|
+
"@dynamic-labs/types": "4.18.7",
|
|
25
|
+
"@dynamic-labs/utils": "4.18.7",
|
|
26
|
+
"@turnkey/http": "2.15.0",
|
|
27
|
+
"bitcoinjs-lib": "6.1.5",
|
|
28
|
+
"ecpair": "2.1.0",
|
|
29
|
+
"@dynamic-labs/assert-package-version": "4.18.7",
|
|
30
|
+
"@dynamic-labs/embedded-wallet": "4.18.7",
|
|
31
|
+
"@dynamic-labs/wallet-book": "4.18.7",
|
|
32
|
+
"@dynamic-labs/wallet-connector-core": "4.18.7"
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {}
|
|
35
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var embeddedWallet = require('@dynamic-labs/embedded-wallet');
|
|
7
|
+
var TurnkeyBitcoinWalletConnector = require('./lib/TurnkeyBitcoinWalletConnector/TurnkeyBitcoinWalletConnector.cjs');
|
|
8
|
+
|
|
9
|
+
const TurnkeyBitcoinWalletConnectors = (props) => {
|
|
10
|
+
var _a;
|
|
11
|
+
if ((_a = props === null || props === void 0 ? void 0 : props.apiProviders) === null || _a === void 0 ? void 0 : _a.turnkey) {
|
|
12
|
+
return [
|
|
13
|
+
class extends TurnkeyBitcoinWalletConnector.TurnkeyBitcoinWalletConnector {
|
|
14
|
+
constructor(innerProps) {
|
|
15
|
+
super(embeddedWallet.TurnkeyWalletConnectorInfo.TurnkeyHD, Object.assign(Object.assign({}, props), innerProps));
|
|
16
|
+
}
|
|
17
|
+
},
|
|
18
|
+
];
|
|
19
|
+
}
|
|
20
|
+
return [];
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
exports.TurnkeyBitcoinWalletConnectors = TurnkeyBitcoinWalletConnectors;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { TurnkeyWalletConnectorInfo } from '@dynamic-labs/embedded-wallet';
|
|
3
|
+
import { TurnkeyBitcoinWalletConnector } from './lib/TurnkeyBitcoinWalletConnector/TurnkeyBitcoinWalletConnector.js';
|
|
4
|
+
|
|
5
|
+
const TurnkeyBitcoinWalletConnectors = (props) => {
|
|
6
|
+
var _a;
|
|
7
|
+
if ((_a = props === null || props === void 0 ? void 0 : props.apiProviders) === null || _a === void 0 ? void 0 : _a.turnkey) {
|
|
8
|
+
return [
|
|
9
|
+
class extends TurnkeyBitcoinWalletConnector {
|
|
10
|
+
constructor(innerProps) {
|
|
11
|
+
super(TurnkeyWalletConnectorInfo.TurnkeyHD, Object.assign(Object.assign({}, props), innerProps));
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
];
|
|
15
|
+
}
|
|
16
|
+
return [];
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export { TurnkeyBitcoinWalletConnectors };
|
package/src/index.cjs
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var assertPackageVersion = require('@dynamic-labs/assert-package-version');
|
|
7
|
+
var _package = require('../package.cjs');
|
|
8
|
+
var TurnkeyBitcoinWalletConnectors = require('./TurnkeyBitcoinWalletConnectors.cjs');
|
|
9
|
+
|
|
10
|
+
assertPackageVersion.assertPackageVersion('@dynamic-labs/embedded-wallet-bitcoin', _package.version);
|
|
11
|
+
|
|
12
|
+
exports.TurnkeyBitcoinWalletConnectors = TurnkeyBitcoinWalletConnectors.TurnkeyBitcoinWalletConnectors;
|
package/src/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './TurnkeyBitcoinWalletConnectors';
|
package/src/index.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
import { assertPackageVersion } from '@dynamic-labs/assert-package-version';
|
|
3
|
+
import { version } from '../package.js';
|
|
4
|
+
export { TurnkeyBitcoinWalletConnectors } from './TurnkeyBitcoinWalletConnectors.js';
|
|
5
|
+
|
|
6
|
+
assertPackageVersion('@dynamic-labs/embedded-wallet-bitcoin', version);
|
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
'use client'
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
5
|
+
|
|
6
|
+
var _tslib = require('../../../_virtual/_tslib.cjs');
|
|
7
|
+
var ecc = require('@bitcoinerlab/secp256k1');
|
|
8
|
+
var ecpair = require('ecpair');
|
|
9
|
+
var bitcoinjsLib = require('bitcoinjs-lib');
|
|
10
|
+
var bitcoin = require('@dynamic-labs/bitcoin');
|
|
11
|
+
var walletConnectorCore = require('@dynamic-labs/wallet-connector-core');
|
|
12
|
+
var utils = require('@dynamic-labs/utils');
|
|
13
|
+
var embeddedWallet = require('@dynamic-labs/embedded-wallet');
|
|
14
|
+
var fees = require('./fees.cjs');
|
|
15
|
+
var signer = require('./signer.cjs');
|
|
16
|
+
|
|
17
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
18
|
+
|
|
19
|
+
var ecc__default = /*#__PURE__*/_interopDefaultLegacy(ecc);
|
|
20
|
+
|
|
21
|
+
bitcoinjsLib.initEccLib(ecc__default["default"]);
|
|
22
|
+
class TurnkeyBitcoinWalletConnector extends embeddedWallet.TurnkeyWalletConnectorBase {
|
|
23
|
+
constructor(nameAndKey, props) {
|
|
24
|
+
super(nameAndKey, props);
|
|
25
|
+
this.ChainWallet = bitcoin.BitcoinWallet;
|
|
26
|
+
this.connectedChain = 'BTC';
|
|
27
|
+
this.supportedChains = ['BTC'];
|
|
28
|
+
this.canFetchConnectedAccounts = false;
|
|
29
|
+
this.isHardwareWalletEnabled = false;
|
|
30
|
+
}
|
|
31
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
32
|
+
isLedgerAddress(_address) {
|
|
33
|
+
return false;
|
|
34
|
+
}
|
|
35
|
+
clearConnectedAccounts() {
|
|
36
|
+
return Promise.resolve();
|
|
37
|
+
}
|
|
38
|
+
setVerifiedCredentials(verifiedCredentials) {
|
|
39
|
+
const turnkeyVerifiedCredentials = embeddedWallet.findTurnkeyVerifiedCredentials(verifiedCredentials, 'bip122');
|
|
40
|
+
const [turnkeyVerifiedCredential] = turnkeyVerifiedCredentials;
|
|
41
|
+
const didTurnkeyVerifiedCredentialsChanged = JSON.stringify(this.verifiedCredentials) !==
|
|
42
|
+
JSON.stringify(turnkeyVerifiedCredentials);
|
|
43
|
+
if (!didTurnkeyVerifiedCredentialsChanged) {
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
this.verifiedCredential = turnkeyVerifiedCredential;
|
|
47
|
+
this.verifiedCredentials = turnkeyVerifiedCredentials;
|
|
48
|
+
this.refreshTurnkeyAccount();
|
|
49
|
+
}
|
|
50
|
+
validateActiveWallet(expectedAddress) {
|
|
51
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
var _a, _b;
|
|
53
|
+
const activeAddress = ((_a = this.verifiedCredential) === null || _a === void 0 ? void 0 : _a.address) || '';
|
|
54
|
+
const isWalletActive = walletConnectorCore.isSameAddress(activeAddress, expectedAddress, this.connectedChain);
|
|
55
|
+
if (!isWalletActive) {
|
|
56
|
+
const targetActiveAccount = (_b = this.verifiedCredentials) === null || _b === void 0 ? void 0 : _b.find((vc) => (vc === null || vc === void 0 ? void 0 : vc.address) === expectedAddress);
|
|
57
|
+
if (!targetActiveAccount) {
|
|
58
|
+
throw new utils.DynamicError('Account not found');
|
|
59
|
+
}
|
|
60
|
+
this.verifiedCredential = targetActiveAccount;
|
|
61
|
+
this.refreshTurnkeyAccount();
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
refreshTurnkeyAccount() {
|
|
66
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
67
|
+
this._turnkeyAccount = undefined;
|
|
68
|
+
return this.getTurnkeyAccount();
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
createTurnkeyAccount() {
|
|
72
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
73
|
+
const turnkeyClient = yield this.getTurnkeyClient();
|
|
74
|
+
const account = turnkeyClient;
|
|
75
|
+
return account;
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
getTurnkeyAccount() {
|
|
79
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
var _a, _b;
|
|
81
|
+
const { turnkeySubOrganizationId } = (_a = this.walletProperties) !== null && _a !== void 0 ? _a : {};
|
|
82
|
+
const { address } = (_b = this.verifiedCredential) !== null && _b !== void 0 ? _b : {};
|
|
83
|
+
if (!turnkeySubOrganizationId || !address) {
|
|
84
|
+
return;
|
|
85
|
+
}
|
|
86
|
+
this._turnkeyAccount = yield this.createTurnkeyAccount();
|
|
87
|
+
this.setLoggerMetadata();
|
|
88
|
+
return this._turnkeyAccount;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
getUTXOs(address) {
|
|
92
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
try {
|
|
94
|
+
const url = bitcoin.getMempoolApiUrl(address);
|
|
95
|
+
const response = yield fetch(`${url}/address/${address}/utxo`);
|
|
96
|
+
return yield response.json();
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
embeddedWallet.logger.error('Error fetching UTXOs:', error);
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
getBitcoinNetwork(accountAddress) {
|
|
105
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
106
|
+
return accountAddress.startsWith('t') ? bitcoinjsLib.networks.testnet : bitcoinjsLib.networks.bitcoin;
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
sendRawTransaction(rawTransaction) {
|
|
110
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
111
|
+
if (!rawTransaction) {
|
|
112
|
+
throw new utils.DynamicError('No transaction specified!');
|
|
113
|
+
}
|
|
114
|
+
const accountAddress = this.getAccount();
|
|
115
|
+
if (!accountAddress) {
|
|
116
|
+
throw new utils.DynamicError('No account address found!');
|
|
117
|
+
}
|
|
118
|
+
const API_URL = bitcoin.getMempoolApiUrl(accountAddress);
|
|
119
|
+
const response = yield fetch(`${API_URL}/tx`, {
|
|
120
|
+
body: rawTransaction,
|
|
121
|
+
headers: {
|
|
122
|
+
'Content-Type': 'application/x-www-form-urlencoded',
|
|
123
|
+
},
|
|
124
|
+
method: 'POST',
|
|
125
|
+
});
|
|
126
|
+
if (!response.ok) {
|
|
127
|
+
if (response.status === 429) {
|
|
128
|
+
throw new utils.DynamicError('sendRawTransaction - mempool api rate limit exceeded');
|
|
129
|
+
}
|
|
130
|
+
const error = yield response.text();
|
|
131
|
+
embeddedWallet.logger.debug(`sendRawTransaction - response not ok: ${JSON.stringify(error)}`);
|
|
132
|
+
throw new utils.DynamicError('sendRawTransaction - failed to send transaction');
|
|
133
|
+
}
|
|
134
|
+
// this endpoint returns the transaction ID
|
|
135
|
+
return response.text();
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
sendBitcoin(_a) {
|
|
139
|
+
return _tslib.__awaiter(this, arguments, void 0, function* ({ recipientAddress, amount, }) {
|
|
140
|
+
var _b, _c;
|
|
141
|
+
yield this.createOrRestoreSession();
|
|
142
|
+
const accountAddress = this.getAccount();
|
|
143
|
+
const { walletAdditionalAddresses } = (_b = this.verifiedCredential) !== null && _b !== void 0 ? _b : {};
|
|
144
|
+
if (!accountAddress || !walletAdditionalAddresses) {
|
|
145
|
+
throw new Error('Failed to get address or wallet additional addresses');
|
|
146
|
+
}
|
|
147
|
+
const additionalAddress = walletAdditionalAddresses.find((additionalAddress) => additionalAddress.address === accountAddress);
|
|
148
|
+
const publicKeyCompressed = additionalAddress === null || additionalAddress === void 0 ? void 0 : additionalAddress.publicKey;
|
|
149
|
+
if (!publicKeyCompressed) {
|
|
150
|
+
throw new Error('Failed to get public key');
|
|
151
|
+
}
|
|
152
|
+
const ECPair = ecpair.ECPairFactory(ecc__default["default"]);
|
|
153
|
+
const pair = ECPair.fromPublicKey(Buffer.from(publicKeyCompressed, 'hex'));
|
|
154
|
+
const network = yield this.getBitcoinNetwork(accountAddress);
|
|
155
|
+
const psbt = new bitcoinjsLib.Psbt({ network });
|
|
156
|
+
const utxos = yield this.getUTXOs(accountAddress);
|
|
157
|
+
const feeEstimate = yield fees.estimateFees({
|
|
158
|
+
network,
|
|
159
|
+
numInputs: utxos.length,
|
|
160
|
+
numOutputs: 2,
|
|
161
|
+
});
|
|
162
|
+
const totalToSpend = utxos.reduce((total, utxo) => total + utxo.value, 0);
|
|
163
|
+
const maxToSpend = totalToSpend - feeEstimate;
|
|
164
|
+
const changeAmount = BigInt(maxToSpend) - BigInt(amount);
|
|
165
|
+
for (const utxo of utxos) {
|
|
166
|
+
if (accountAddress.startsWith('tb1p') ||
|
|
167
|
+
accountAddress.startsWith('bc1p')) {
|
|
168
|
+
const xOnlyPublicKey = pair.publicKey.slice(1, 33);
|
|
169
|
+
const outputScript = bitcoinjsLib.payments.p2tr({
|
|
170
|
+
internalPubkey: xOnlyPublicKey,
|
|
171
|
+
network: network,
|
|
172
|
+
}).output;
|
|
173
|
+
if (!outputScript) {
|
|
174
|
+
throw new Error('Failed to create output script');
|
|
175
|
+
}
|
|
176
|
+
psbt.addInput({
|
|
177
|
+
hash: utxo.txid,
|
|
178
|
+
index: utxo.vout,
|
|
179
|
+
tapInternalKey: xOnlyPublicKey,
|
|
180
|
+
witnessUtxo: {
|
|
181
|
+
script: outputScript,
|
|
182
|
+
value: utxo.value,
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
else {
|
|
187
|
+
// P2WPKH (SegWit)
|
|
188
|
+
const witnessUtxo = bitcoinjsLib.payments.p2wpkh({
|
|
189
|
+
network,
|
|
190
|
+
pubkey: pair.publicKey,
|
|
191
|
+
}).output;
|
|
192
|
+
if (!witnessUtxo) {
|
|
193
|
+
throw new Error('Failed to create witness utxo');
|
|
194
|
+
}
|
|
195
|
+
psbt.addInput({
|
|
196
|
+
hash: utxo.txid,
|
|
197
|
+
index: utxo.vout,
|
|
198
|
+
witnessUtxo: {
|
|
199
|
+
script: witnessUtxo,
|
|
200
|
+
value: utxo.value,
|
|
201
|
+
},
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
const DUST_LIMIT = 546; // Bitcoin's dust limit in satoshis
|
|
206
|
+
if (Number(amount) < DUST_LIMIT) {
|
|
207
|
+
throw new Error('Amount is below dust limit of 546 satoshis');
|
|
208
|
+
}
|
|
209
|
+
psbt.addOutput({
|
|
210
|
+
script: bitcoinjsLib.address.toOutputScript(recipientAddress, network),
|
|
211
|
+
value: Number(amount),
|
|
212
|
+
});
|
|
213
|
+
if (changeAmount > 0) {
|
|
214
|
+
psbt.addOutput({
|
|
215
|
+
script: bitcoinjsLib.address.toOutputScript(accountAddress, network),
|
|
216
|
+
value: Number(changeAmount),
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
const turnkeyClient = yield this.getTurnkeyAccount();
|
|
220
|
+
if (!turnkeyClient) {
|
|
221
|
+
throw new Error('Failed to get turnkey client');
|
|
222
|
+
}
|
|
223
|
+
const signer$1 = new signer.TurnkeySigner({
|
|
224
|
+
address: accountAddress,
|
|
225
|
+
client: turnkeyClient,
|
|
226
|
+
organizationId: (_c = this.walletProperties) === null || _c === void 0 ? void 0 : _c.turnkeySubOrganizationId,
|
|
227
|
+
publicKey: bitcoinjsLib.address.fromBech32(accountAddress).data,
|
|
228
|
+
});
|
|
229
|
+
yield Promise.all(utxos.map((_utxo, i) => _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
230
|
+
yield psbt.signInputAsync(i, signer$1);
|
|
231
|
+
})));
|
|
232
|
+
psbt.finalizeAllInputs();
|
|
233
|
+
const txHex = psbt.extractTransaction().toHex();
|
|
234
|
+
return this.sendRawTransaction(txHex);
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
signPsbt(_a) {
|
|
238
|
+
return _tslib.__awaiter(this, arguments, void 0, function* ({ allowedSighash, unsignedPsbtBase64, signature, }) {
|
|
239
|
+
var _b, _c, _d;
|
|
240
|
+
yield this.createOrRestoreSession();
|
|
241
|
+
const accountAddress = this.getAccount();
|
|
242
|
+
if (!accountAddress) {
|
|
243
|
+
throw new Error('Failed to get address');
|
|
244
|
+
}
|
|
245
|
+
const psbt = bitcoinjsLib.Psbt.fromBase64(unsignedPsbtBase64);
|
|
246
|
+
const turnkeyClient = yield this.getTurnkeyAccount();
|
|
247
|
+
if (!turnkeyClient) {
|
|
248
|
+
throw new Error('Failed to get turnkey client');
|
|
249
|
+
}
|
|
250
|
+
const signer$1 = new signer.TurnkeySigner({
|
|
251
|
+
address: accountAddress,
|
|
252
|
+
client: turnkeyClient,
|
|
253
|
+
organizationId: (_b = this.walletProperties) === null || _b === void 0 ? void 0 : _b.turnkeySubOrganizationId,
|
|
254
|
+
publicKey: bitcoinjsLib.address.fromBech32(accountAddress).data,
|
|
255
|
+
});
|
|
256
|
+
if (signature) {
|
|
257
|
+
for (let i = 0; i < signature.length; i++) {
|
|
258
|
+
const sigIdx = (_d = (_c = signature[i]) === null || _c === void 0 ? void 0 : _c.signingIndexes) === null || _d === void 0 ? void 0 : _d[0];
|
|
259
|
+
if (sigIdx !== undefined) {
|
|
260
|
+
yield psbt.signInputAsync(sigIdx, signer$1, allowedSighash);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
else {
|
|
265
|
+
yield psbt.signAllInputsAsync(signer$1, allowedSighash);
|
|
266
|
+
}
|
|
267
|
+
psbt.finalizeAllInputs();
|
|
268
|
+
return {
|
|
269
|
+
signedPsbt: psbt.toBase64(),
|
|
270
|
+
};
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
signPsbts(requests) {
|
|
274
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
275
|
+
const signedPsbts = yield Promise.all(requests.map((request) => this.signPsbt(request)));
|
|
276
|
+
return signedPsbts.map((response) => response.signedPsbt || '');
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
280
|
+
createUiTransaction(from) {
|
|
281
|
+
throw new Error('Method not implemented.');
|
|
282
|
+
}
|
|
283
|
+
canConnectWithHardwareWallet() {
|
|
284
|
+
return false;
|
|
285
|
+
}
|
|
286
|
+
isInstalledOnBrowser() {
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
apiGetBalance(address) {
|
|
290
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
291
|
+
const API_URL = bitcoin.getMempoolApiUrl(address);
|
|
292
|
+
const response = yield fetch(`${API_URL}/address/${address}`);
|
|
293
|
+
if (!response.ok) {
|
|
294
|
+
// if the request fails due to rate limits, return cached value
|
|
295
|
+
if (response.status === 429) {
|
|
296
|
+
return '0';
|
|
297
|
+
}
|
|
298
|
+
// new accounts not yet indexed will return a 404
|
|
299
|
+
if (response.status === 404) {
|
|
300
|
+
return '0';
|
|
301
|
+
}
|
|
302
|
+
return undefined;
|
|
303
|
+
}
|
|
304
|
+
const addressInfo = yield response.json();
|
|
305
|
+
if (!(addressInfo === null || addressInfo === void 0 ? void 0 : addressInfo.chain_stats) || !(addressInfo === null || addressInfo === void 0 ? void 0 : addressInfo.mempool_stats)) {
|
|
306
|
+
return undefined;
|
|
307
|
+
}
|
|
308
|
+
const confirmedBalanceInSats = Number(addressInfo.chain_stats.funded_txo_sum) -
|
|
309
|
+
Number(addressInfo.chain_stats.spent_txo_sum);
|
|
310
|
+
const unconfirmedBalanceInSats = Number(addressInfo.mempool_stats.funded_txo_sum) -
|
|
311
|
+
Number(addressInfo.mempool_stats.spent_txo_sum);
|
|
312
|
+
const balance = bitcoin.satoshisToBtc(confirmedBalanceInSats + unconfirmedBalanceInSats);
|
|
313
|
+
return balance.toString();
|
|
314
|
+
});
|
|
315
|
+
}
|
|
316
|
+
getBalance(address) {
|
|
317
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
318
|
+
return this.apiGetBalance(address);
|
|
319
|
+
});
|
|
320
|
+
}
|
|
321
|
+
getAccount() {
|
|
322
|
+
return this.turnkeyAddress;
|
|
323
|
+
}
|
|
324
|
+
signMessage(messageToSign, options) {
|
|
325
|
+
return _tslib.__awaiter(this, void 0, void 0, function* () {
|
|
326
|
+
var _a, _b;
|
|
327
|
+
yield this.createOrRestoreSession();
|
|
328
|
+
const turnkeyAccount = yield this.getTurnkeyAccount();
|
|
329
|
+
const accountAddress = this.getAccount();
|
|
330
|
+
if (!turnkeyAccount || !accountAddress) {
|
|
331
|
+
return;
|
|
332
|
+
}
|
|
333
|
+
const msgBuffer = new TextEncoder().encode(messageToSign);
|
|
334
|
+
const hashBuffer = yield crypto.subtle.digest('SHA-256', msgBuffer);
|
|
335
|
+
const hashArray = Array.from(new Uint8Array(hashBuffer));
|
|
336
|
+
const payload = hashArray
|
|
337
|
+
.map((b) => b.toString(16).padStart(2, '0'))
|
|
338
|
+
.join('');
|
|
339
|
+
const response = yield (turnkeyAccount === null || turnkeyAccount === void 0 ? void 0 : turnkeyAccount.signRawPayload({
|
|
340
|
+
organizationId: (_a = this.walletProperties) === null || _a === void 0 ? void 0 : _a.turnkeySubOrganizationId,
|
|
341
|
+
parameters: {
|
|
342
|
+
encoding: 'PAYLOAD_ENCODING_HEXADECIMAL',
|
|
343
|
+
hashFunction: 'HASH_FUNCTION_SHA256',
|
|
344
|
+
payload,
|
|
345
|
+
signWith: (_b = options === null || options === void 0 ? void 0 : options.address) !== null && _b !== void 0 ? _b : accountAddress, // default to ordinals address
|
|
346
|
+
},
|
|
347
|
+
timestampMs: new Date().getTime().toString(),
|
|
348
|
+
type: 'ACTIVITY_TYPE_SIGN_RAW_PAYLOAD_V2',
|
|
349
|
+
}));
|
|
350
|
+
const result = response.activity.result.signRawPayloadResult;
|
|
351
|
+
if (!result) {
|
|
352
|
+
return;
|
|
353
|
+
}
|
|
354
|
+
const { r, s } = result;
|
|
355
|
+
return Buffer.from(r + s).toString('hex');
|
|
356
|
+
});
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
exports.TurnkeyBitcoinWalletConnector = TurnkeyBitcoinWalletConnector;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { networks } from 'bitcoinjs-lib';
|
|
2
|
+
import { BitcoinTransaction, BitcoinWallet, BitcoinSignProtocol, BitcoinWalletStandardMethods } from '@dynamic-labs/bitcoin';
|
|
3
|
+
import { GenericNetwork, IUITransaction, WalletUiUtils } from '@dynamic-labs/types';
|
|
4
|
+
import { Chain, ISendBalanceWalletConnector, InternalWalletConnector, IBitcoinWalletConnector, Wallet, BitcoinSignPsbtRequest, BitcoinSignPsbtResponse } from '@dynamic-labs/wallet-connector-core';
|
|
5
|
+
import { WalletBookSchema } from '@dynamic-labs/wallet-book';
|
|
6
|
+
import { JwtVerifiedCredential } from '@dynamic-labs/sdk-api-core';
|
|
7
|
+
import { TurnkeyWalletConnectorBase, TurnkeyWalletConnectorNameAndKey } from '@dynamic-labs/embedded-wallet';
|
|
8
|
+
export type TurnkeyBitcoinConnectorProps = {
|
|
9
|
+
walletUiUtils: WalletUiUtils<InternalWalletConnector>;
|
|
10
|
+
walletBook: WalletBookSchema;
|
|
11
|
+
bitcoinNetworks: GenericNetwork[];
|
|
12
|
+
appName?: string;
|
|
13
|
+
};
|
|
14
|
+
export declare class TurnkeyBitcoinWalletConnector extends TurnkeyWalletConnectorBase implements ISendBalanceWalletConnector, IBitcoinWalletConnector {
|
|
15
|
+
ChainWallet: typeof BitcoinWallet;
|
|
16
|
+
connectedChain: Chain;
|
|
17
|
+
supportedChains: Chain[];
|
|
18
|
+
wallet: Wallet | undefined;
|
|
19
|
+
walletMethods: BitcoinWalletStandardMethods | undefined;
|
|
20
|
+
canFetchConnectedAccounts: boolean;
|
|
21
|
+
isHardwareWalletEnabled: boolean;
|
|
22
|
+
private _turnkeyAccount;
|
|
23
|
+
constructor(nameAndKey: TurnkeyWalletConnectorNameAndKey, props: TurnkeyBitcoinConnectorProps);
|
|
24
|
+
isLedgerAddress(_address: string): boolean;
|
|
25
|
+
clearConnectedAccounts(): Promise<void>;
|
|
26
|
+
setVerifiedCredentials(verifiedCredentials: JwtVerifiedCredential[]): void;
|
|
27
|
+
validateActiveWallet(expectedAddress: string): Promise<void>;
|
|
28
|
+
private refreshTurnkeyAccount;
|
|
29
|
+
private createTurnkeyAccount;
|
|
30
|
+
private getTurnkeyAccount;
|
|
31
|
+
getUTXOs(address: string): Promise<any>;
|
|
32
|
+
getBitcoinNetwork(accountAddress: string): Promise<networks.Network>;
|
|
33
|
+
sendRawTransaction(rawTransaction: string): Promise<string | undefined>;
|
|
34
|
+
sendBitcoin({ recipientAddress, amount, }: BitcoinTransaction): Promise<string | undefined>;
|
|
35
|
+
signPsbt({ allowedSighash, unsignedPsbtBase64, signature, }: BitcoinSignPsbtRequest): Promise<BitcoinSignPsbtResponse>;
|
|
36
|
+
signPsbts(requests: BitcoinSignPsbtRequest[]): Promise<string[] | undefined>;
|
|
37
|
+
createUiTransaction(from: string): Promise<IUITransaction>;
|
|
38
|
+
canConnectWithHardwareWallet(): boolean;
|
|
39
|
+
isInstalledOnBrowser(): boolean;
|
|
40
|
+
private apiGetBalance;
|
|
41
|
+
getBalance(address: string): Promise<string | undefined>;
|
|
42
|
+
getAccount(): string | undefined;
|
|
43
|
+
signMessage(messageToSign: string, options?: {
|
|
44
|
+
address?: string;
|
|
45
|
+
protocol?: BitcoinSignProtocol;
|
|
46
|
+
}): Promise<string | undefined>;
|
|
47
|
+
}
|