emblem-vault-sdk 1.9.0 → 1.9.2
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/bundle.js +85583 -28615
- package/dist/derive.js +110 -1834
- package/dist/index.js +121 -1
- package/dist/utils.js +3 -3
- package/docs/DeGods.html +3 -1
- package/docs/bitcoinjs-lib.js +11820 -0
- package/docs/bundle.js +85583 -28615
- package/docs/index.html +1 -0
- package/package.json +6 -1
- package/src/derive.ts +103 -2007
- package/src/index.ts +152 -1
- package/src/utils.ts +3 -3
- package/tests/EmblemVaultSdk.test.ts +3 -2
- package/types/derive.d.ts +14 -1
- package/types/index.d.ts +15 -0
package/dist/index.js
CHANGED
|
@@ -34,7 +34,9 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
34
34
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
35
35
|
const bignumber_1 = require("@ethersproject/bignumber");
|
|
36
36
|
const utils_1 = require("./utils");
|
|
37
|
-
const
|
|
37
|
+
const sats_connect_1 = require("sats-connect");
|
|
38
|
+
const derive_1 = require("./derive");
|
|
39
|
+
const SDK_VERSION = '1.9.2';
|
|
38
40
|
class EmblemVaultSDK {
|
|
39
41
|
constructor(apiKey, baseUrl) {
|
|
40
42
|
this.apiKey = apiKey;
|
|
@@ -104,6 +106,7 @@ class EmblemVaultSDK {
|
|
|
104
106
|
createCuratedVault(template, callback = null) {
|
|
105
107
|
return __awaiter(this, void 0, void 0, function* () {
|
|
106
108
|
(0, utils_1.templateGuard)(template);
|
|
109
|
+
template.chainId == 1 ? delete template.targetContract[5] : delete template.targetContract[1];
|
|
107
110
|
let url = `${this.baseUrl}/create-curated`;
|
|
108
111
|
if (callback) {
|
|
109
112
|
callback(`creating Vault for user`, template.toAddress);
|
|
@@ -460,6 +463,123 @@ class EmblemVaultSDK {
|
|
|
460
463
|
}));
|
|
461
464
|
});
|
|
462
465
|
}
|
|
466
|
+
// BTC
|
|
467
|
+
getSatsConnectAddress() {
|
|
468
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
469
|
+
return new Promise((resolve, reject) => {
|
|
470
|
+
(0, sats_connect_1.getAddress)({
|
|
471
|
+
payload: {
|
|
472
|
+
purposes: [
|
|
473
|
+
sats_connect_1.AddressPurpose.Ordinals,
|
|
474
|
+
sats_connect_1.AddressPurpose.Payment,
|
|
475
|
+
],
|
|
476
|
+
message: "My App's Name",
|
|
477
|
+
network: {
|
|
478
|
+
type: sats_connect_1.BitcoinNetworkType.Mainnet,
|
|
479
|
+
},
|
|
480
|
+
},
|
|
481
|
+
onFinish: (response) => {
|
|
482
|
+
const paymentAddressItem = response.addresses.find((address) => address.purpose === sats_connect_1.AddressPurpose.Payment);
|
|
483
|
+
const ordinalsAddressItem = response.addresses.find((address) => address.purpose === sats_connect_1.AddressPurpose.Ordinals);
|
|
484
|
+
resolve({
|
|
485
|
+
paymentAddress: (paymentAddressItem === null || paymentAddressItem === void 0 ? void 0 : paymentAddressItem.address) || "",
|
|
486
|
+
paymentPublicKey: (paymentAddressItem === null || paymentAddressItem === void 0 ? void 0 : paymentAddressItem.publicKey) || "",
|
|
487
|
+
ordinalsAddress: (ordinalsAddressItem === null || ordinalsAddressItem === void 0 ? void 0 : ordinalsAddressItem.address) || ""
|
|
488
|
+
});
|
|
489
|
+
},
|
|
490
|
+
onCancel: () => {
|
|
491
|
+
reject("Request canceled");
|
|
492
|
+
},
|
|
493
|
+
});
|
|
494
|
+
});
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
generatePSBT(phrase) {
|
|
498
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
499
|
+
const desiredFeeRate = 2; // sats per byte -> mainnet will be much higher
|
|
500
|
+
const { paymentAddress, paymentPublicKey, ordinalsAddress } = yield this.getSatsConnectAddress();
|
|
501
|
+
// change this to mainnet
|
|
502
|
+
if (window.bitcoin) {
|
|
503
|
+
let bitcoin = window.bitcoin;
|
|
504
|
+
var network = bitcoin.networks.mainnet;
|
|
505
|
+
// generate taproot address
|
|
506
|
+
const { p2tr, pubKey, tweakedSigner } = yield (0, derive_1.generateTaprootAddressFromMnemonic)(phrase);
|
|
507
|
+
const taprootAddress = p2tr.address;
|
|
508
|
+
// build payment definition for payments address
|
|
509
|
+
const p2wpkh = bitcoin.payments.p2wpkh({
|
|
510
|
+
pubkey: Buffer.from(paymentPublicKey, "hex"),
|
|
511
|
+
network,
|
|
512
|
+
});
|
|
513
|
+
const p2sh = bitcoin.payments.p2sh({ redeem: p2wpkh, network });
|
|
514
|
+
console.log(taprootAddress);
|
|
515
|
+
const getAddressUtxos = (address) => __awaiter(this, void 0, void 0, function* () {
|
|
516
|
+
const response = yield fetch(`https://mempool.space/api/address/${address}/utxo`);
|
|
517
|
+
const utxos = yield response.json();
|
|
518
|
+
return utxos;
|
|
519
|
+
});
|
|
520
|
+
const taprootUtxos = yield getAddressUtxos(taprootAddress);
|
|
521
|
+
const paymentUtxos = yield getAddressUtxos(paymentAddress);
|
|
522
|
+
// there should only be 1 utxo in this vault address
|
|
523
|
+
const taprootUtxo = taprootUtxos[0];
|
|
524
|
+
// construct PSBT
|
|
525
|
+
const psbt = new bitcoin.Psbt({ network });
|
|
526
|
+
// add input from taproot
|
|
527
|
+
psbt.addInput({
|
|
528
|
+
hash: taprootUtxo.txid,
|
|
529
|
+
index: taprootUtxo.vout,
|
|
530
|
+
witnessUtxo: {
|
|
531
|
+
script: p2tr.output,
|
|
532
|
+
value: taprootUtxo.value,
|
|
533
|
+
},
|
|
534
|
+
tapInternalKey: pubKey,
|
|
535
|
+
});
|
|
536
|
+
// output to ordinalsAddress
|
|
537
|
+
psbt.addOutput({
|
|
538
|
+
address: ordinalsAddress,
|
|
539
|
+
value: taprootUtxo.value,
|
|
540
|
+
});
|
|
541
|
+
// add inputs for fees from paymentAddress
|
|
542
|
+
let totalFeeInput = 0;
|
|
543
|
+
let size = 0;
|
|
544
|
+
for (const utxo of paymentUtxos) {
|
|
545
|
+
psbt.addInput({
|
|
546
|
+
hash: utxo.txid,
|
|
547
|
+
index: utxo.vout,
|
|
548
|
+
witnessUtxo: {
|
|
549
|
+
script: p2sh.output,
|
|
550
|
+
value: utxo.value,
|
|
551
|
+
},
|
|
552
|
+
redeemScript: p2sh.redeem.output,
|
|
553
|
+
});
|
|
554
|
+
totalFeeInput += utxo.value;
|
|
555
|
+
size = (0, derive_1.getPsbtTxnSize)(phrase, psbt.toBase64());
|
|
556
|
+
if (totalFeeInput >= desiredFeeRate * size) {
|
|
557
|
+
break;
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
if (totalFeeInput < desiredFeeRate * size) {
|
|
561
|
+
throw new Error("Insufficient funds at desired fee rate");
|
|
562
|
+
}
|
|
563
|
+
// maybe add output for change if change is greater than 1000 sats (dust)
|
|
564
|
+
if (desiredFeeRate * size > 1000) {
|
|
565
|
+
psbt.addOutput({
|
|
566
|
+
address: paymentAddress,
|
|
567
|
+
value: totalFeeInput - Math.ceil(desiredFeeRate * size),
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
// sign
|
|
571
|
+
psbt.signInput(0, tweakedSigner);
|
|
572
|
+
// send this to wallet to sign all indexes except the first one
|
|
573
|
+
const psbtBase64 = psbt.toBase64();
|
|
574
|
+
console.log(psbtBase64);
|
|
575
|
+
}
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
getTaprootAddressFromMnemonic(phrase) {
|
|
579
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
580
|
+
return (0, derive_1.generateTaprootAddressFromMnemonic)(phrase);
|
|
581
|
+
});
|
|
582
|
+
}
|
|
463
583
|
}
|
|
464
584
|
if (typeof window !== 'undefined') {
|
|
465
585
|
window.EmblemVaultSDK = EmblemVaultSDK;
|
package/dist/utils.js
CHANGED
|
@@ -16,7 +16,7 @@ exports.decryptKeys = exports.getTorusKeys = exports.COIN_TO_NETWORK = exports.c
|
|
|
16
16
|
const crypto_js_1 = __importDefault(require("crypto-js"));
|
|
17
17
|
const metadata_json_1 = __importDefault(require("./curated/metadata.json"));
|
|
18
18
|
const abi_json_1 = __importDefault(require("./abi/abi.json"));
|
|
19
|
-
|
|
19
|
+
// import { phrasePathToKey } from './derive'
|
|
20
20
|
exports.NFT_DATA = metadata_json_1.default;
|
|
21
21
|
// PROJECTS_DATA is list of projects i.e. curated collection names
|
|
22
22
|
// that are present in metadataJson file
|
|
@@ -478,7 +478,7 @@ function generateTemplate(record) {
|
|
|
478
478
|
let template = {
|
|
479
479
|
fromAddress: { type: "user-provided" },
|
|
480
480
|
toAddress: { type: "user-provided" },
|
|
481
|
-
chainId:
|
|
481
|
+
chainId: { type: "user-provided" },
|
|
482
482
|
experimental: true,
|
|
483
483
|
targetContract: Object.assign(Object.assign({}, _this.contracts), { name: _this.name, description: _this.loadTypes.includes('detailed') ? null : _this.description }),
|
|
484
484
|
targetAsset: {
|
|
@@ -696,7 +696,7 @@ function decryptKeys(vaultCiphertextV2, keys, addresses) {
|
|
|
696
696
|
try {
|
|
697
697
|
var bytes = crypto_js_1.default.AES.decrypt(vaultCiphertextV2, keys.privateKey.privKey);
|
|
698
698
|
let payload = JSON.parse(bytes.toString(crypto_js_1.default.enc.Utf8));
|
|
699
|
-
let key =
|
|
699
|
+
// let key = phrasePathToKey(payload.phrase, 0)
|
|
700
700
|
return payload; //key
|
|
701
701
|
// addresses.forEach(async address=>{
|
|
702
702
|
// let key = phraseToKey(payload.phrase, 0)
|
package/docs/DeGods.html
CHANGED
|
@@ -27,6 +27,7 @@
|
|
|
27
27
|
"experimental":true,
|
|
28
28
|
"targetContract":{
|
|
29
29
|
"1":"0x345eF9d7E75aEEb979053AA41BB6330683353B7b",
|
|
30
|
+
"5":"0x582699d2c58A38056Cf02875540705137f0bbbF7",
|
|
30
31
|
"name":"Bitcoin DeGods",
|
|
31
32
|
"description":"Bitcoin DeGods is a collection of 535 Bitcoin Ordinals inscribed in the 77236 to 77770 range. This collection is curated by Emblem Vault. "
|
|
32
33
|
},
|
|
@@ -50,10 +51,11 @@
|
|
|
50
51
|
defaultAccount = await web3.eth.getAccounts().then(accounts => accounts[0]).catch(err=>console) // Minter's address
|
|
51
52
|
}
|
|
52
53
|
if (!vaultData) {
|
|
54
|
+
let chainId = Number(await web3.eth.net.getId())
|
|
53
55
|
// populate the template
|
|
54
56
|
contractTemplate.toAddress = defaultAccount;
|
|
55
57
|
contractTemplate.fromAddress = defaultAccount;
|
|
56
|
-
|
|
58
|
+
contractTemplate.chainId = chainId;
|
|
57
59
|
// Create a vault
|
|
58
60
|
vaultData = await sdk.createCuratedVault(contractTemplate, updateLogCallback).catch(err=>console.log(err));
|
|
59
61
|
if (!vaultData || vaultData.err) { // Temporary fix while I fix a race condition
|