Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@blockstack/keychain

Package Overview
Dependencies
Maintainers
8
Versions
96
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@blockstack/keychain - npm Package Compare versions

Comparing version
0.12.5
to
0.12.6
+11
-0
CHANGELOG.md

@@ -6,2 +6,13 @@ # Change Log

## 0.12.6 (2020-10-05)
### Bug Fixes
* remaining broken sidecar urls, fixes [#615](https://github.com/blockstack/ux/issues/615) ([4c26fce](https://github.com/blockstack/ux/commit/4c26fcea34c1603e4ea63d1be7b576b9ccb45a42))
## 0.12.5 (2020-09-29)

@@ -8,0 +19,0 @@

+1
-1

@@ -1193,3 +1193,3 @@ 'use strict';

var network = new stacksTransactions.StacksTestnet();
network.coreApiUrl = 'https://sidecar.staging.blockstack.xyz';
network.coreApiUrl = 'https://stacks-node-api.blockstack.org';
return network;

@@ -1196,0 +1196,0 @@ };

@@ -1,2 +0,2 @@

"use strict";var e,r=require("bip39"),t=require("bitcoinjs-lib"),n=require("blockstack/lib/wallet"),i=require("blockstack/lib/keys"),o=require("blockstack/lib/encryption/sha2Hash"),s=require("zone-file"),a=require("blockstack/lib/auth/authMessages"),u=require("jsontokens"),c=require("blockstack/lib/encryption/cryptoRandom"),d=require("blockstack"),f=require("assert"),l=require("triplesec"),h=require("blockstack/lib/encryption/wallet"),v=require("@blockstack/stacks-transactions"),p=(e=require("bn.js"))&&"object"==typeof e&&"default"in e?e.default:e;function y(){return(y=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}var m=function(){function e(){}return e.prototype.then=function(r,t){var n=new e,i=this.s;if(i){var o=1&i?r:t;if(o){try{P(n,1,o(this.v))}catch(e){P(n,2,e)}return n}return this}return this.o=function(e){try{var i=e.v;1&e.s?P(n,1,r?r(i):i):t?P(n,1,t(i)):P(n,2,i)}catch(e){P(n,2,e)}},n},e}();function P(e,r,t){if(!e.s){if(t instanceof m){if(!t.s)return void(t.o=P.bind(null,e,r));1&r&&(r=t.s),t=t.v}if(t&&t.then)return void t.then(P.bind(null,e,r),P.bind(null,e,2));e.s=r,e.v=t;var n=e.o;n&&n(e)}}function g(e){return e instanceof m&&1&e.s}function b(e,r){try{var t=e()}catch(e){return r(e)}return t&&t.then?t.then(void 0,r):t}"undefined"!=typeof Symbol&&(Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator"))),"undefined"!=typeof Symbol&&(Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator")));var k,K,x=function(){function e(e,r){this.hdNode=e,this.salt=r}var r=e.prototype;return r.getNode=function(){return this.hdNode},r.getSalt=function(){return this.salt},r.getIdentityKey=function(){if(!this.hdNode.privateKey)throw new Error("Node does not have private key");return this.hdNode.privateKey.toString("hex")},r.getIdentityKeyID=function(){return this.hdNode.publicKey.toString("hex")},r.getAppsNode=function(){return this.hdNode.deriveHardened(0)},r.getAddress=function(){return B(this.hdNode)},r.getEncryptionNode=function(){return this.hdNode.deriveHardened(2)},r.getSigningNode=function(){return this.hdNode.deriveHardened(1)},r.getSTXNode=function(){return this.hdNode.deriveHardened(6)},r.getAppNode=function(e){return n.getLegacyAppNode(this.hdNode,this.salt,e)},r.getAppPrivateKey=function(e){var r=this.getAppNode(e);if(!r.privateKey)throw new Error("App node does not have private key");return r.privateKey.toString("hex")},r.getAppAddress=function(e){var r=this.getAppNode(e);return i.publicKeyToAddress(r.publicKey)},e}(),S=function(e,r,t){try{return Promise.resolve(fetch(t.server+"/store/"+t.address+"/"+e,{method:"POST",headers:{"Content-Type":"application/json",Authorization:"bearer "+t.token},body:r,referrer:"no-referrer",referrerPolicy:"no-referrer"})).then((function(e){return Promise.resolve(e.json()).then((function(e){return e.publicURL}))}))}catch(e){return Promise.reject(e)}},A=function(e){var r=e.gaiaHubUrl,t=e.filePath,n=e.identity,i=e.signedProfileTokenData,o=e.gaiaHubConfig;try{var s=function(e){return Promise.resolve(S(t,i,e))};return Promise.resolve(o?s(o):Promise.resolve(d.connectToGaiaHub(r,n.keyPair.key)).then(s))}catch(e){return Promise.reject(e)}},C={"@type":"Person","@context":"http://schema.org"};(K=exports.Subdomains||(exports.Subdomains={})).TEST="test-personal.id",K.BLOCKSTACK="id.blockstack";var N=((k={})[exports.Subdomains.TEST]={registerUrl:"https://test-registrar.blockstack.org/register",apiUrl:"https://test-registrar.blockstack.org/v1/names"},k[exports.Subdomains.BLOCKSTACK]={registerUrl:"https://registrar.blockstack.org/register",apiUrl:"https://registrar.blockstack.org/v1/names"},k);function w(e,r){var t=d.signProfileToken(e,r.key,{publicKey:r.keyID}),n=d.wrapProfileToken(t);return JSON.stringify([n],null,2)}var T=function(e){var r=e.profile,t=e.gaiaHubUrl,n=e.filePath,i=e.identity,o=e.gaiaHubConfig;try{var s=w(r,i.keyPair);return Promise.resolve(A({gaiaHubUrl:t,filePath:n,identity:i,signedProfileTokenData:s,gaiaHubConfig:o})).then((function(){}))}catch(e){return Promise.reject(e)}},U=function(e){var r=e.profileUrl;return Promise.resolve(b((function(){return Promise.resolve(fetch(r)).then((function(e){var r=!1;function t(t){if(r)return t;if(404===e.status)return null;throw new Error("Network error when fetching profile")}var n=function(){if(e.ok)return Promise.resolve(e.json()).then((function(e){var t;return r=!0,null===(t=e[0].decodedToken.payload)||void 0===t?void 0:t.claim}))}();return n&&n.then?n.then(t):t(n)}))}),(function(){return null})))},j=function(){function e(e){var r=e.address,t=e.usernames,n=e.defaultUsername,i=e.profile;this.keyPair=e.keyPair,this.address=r,this.usernames=t||[],this.defaultUsername=n,this.profile=i}var r=e.prototype;return r.makeAuthResponse=function(e){var r=e.appDomain,n=e.gaiaUrl,o=e.transitPublicKey,s=e.scopes,f=void 0===s?[]:s,l=e.stxAddress;try{var h=this,v=h.appPrivateKey(r);return Promise.resolve(function(e){try{return Promise.resolve(fetch(e+"/hub_info")).then((function(e){return Promise.resolve(e.json())}))}catch(e){return Promise.reject(e)}}(n)).then((function(e){return Promise.resolve(h.profileUrl(e.read_url_prefix)).then((function(s){return Promise.resolve(U({profileUrl:s})).then((function(p){function m(){h.profile=p;var e,r,t,d,f,m=(e=i.getPublicKeyFromPrivate(v.slice(0,64)),r=h.keyPair.key.slice(0,64),t=i.getPublicKeyFromPrivate(r),d=c.randomBytes(16).toString("hex"),f={childToAssociate:e,iss:t,exp:31536e3+(new Date).getTime()/1e3,iat:Date.now()/1e3,salt:d},new u.TokenSigner("ES256K",r).sign(f));return a.makeAuthResponse(h.keyPair.key,y({},h.profile||{},{stxAddress:l}),h.defaultUsername||"",{profileUrl:s},void 0,v,void 0,o,n,void 0,m)}var P=function(){if(f.includes("publish_data")){p.apps||(p.apps={});var o=t.ECPair.fromPrivateKey(Buffer.from(v,"hex")),a=d.ecPairToAddress(o),l=""+e.read_url_prefix+a+"/";p.apps[r]=l,p.appsMeta||(p.appsMeta={}),p.appsMeta[r]={storage:l,publicKey:o.publicKey.toString("hex")};var y=function(e){var r=e.hubInfo,t=e.privateKey,n=e.gaiaHubUrl,o=r.read_url_prefix,s=function(e){var r=e.privateKey,t=e.hubInfo.challenge_text,n={gaiaHubUrl:e.gaiaHubUrl,iss:i.getPublicKeyFromPrivate(r),salt:c.randomBytes(16).toString("hex")};return t&&(n.gaiaChallenge=t),"v1:"+new u.TokenSigner("ES256K",r).sign(n)}({hubInfo:r,privateKey:t,gaiaHubUrl:n});return{url_prefix:o,max_file_upload_size_megabytes:100,address:d.ecPairToAddress(d.hexStringToECPair(t+(64===t.length?"01":""))),token:s,server:n}}({hubInfo:e,privateKey:h.keyPair.key,gaiaHubUrl:n}),m=h.gaiaReadUrlToWritePath(s,e.read_url_prefix,a);return Promise.resolve(T({profile:p,identity:h,gaiaHubUrl:n,filePath:m,gaiaHubConfig:y})).then((function(){}))}}();return P&&P.then?P.then(m):m()}))}))}))}catch(e){return Promise.reject(e)}},r.appPrivateKey=function(e){var r=this.keyPair,n=r.salt;return new x(t.bip32.fromBase58(r.appsNodeKey),n).getAppPrivateKey(e)},r.profileUrl=function(e){try{var r=!1,t=this,n=function(){if(t.defaultUsername)return b((function(){return Promise.resolve(z(t.defaultUsername)).then((function(e){if(e)return r=!0,e}))}),(function(e){console.warn("Error fetching profile URL from zone file:",e)}))}();return Promise.resolve(n&&n.then?n.then((function(n){return r?n:""+e+t.address+"/profile.json"})):r?n:""+e+t.address+"/profile.json")}catch(e){return Promise.reject(e)}},r.gaiaReadUrlToWritePath=function(e,r,t){return e.substr((""+r+t+"/").length)},r.fetchNames=function(){try{return Promise.resolve(fetch("https://core.blockstack.org/v1/addresses/bitcoin/"+this.address)).then((function(e){return Promise.resolve(e.json()).then((function(e){return e.names}))}))}catch(e){return Promise.reject(e)}},r.refresh=function(e){var r=this;return void 0===e&&(e={gaiaUrl:"https://gaia.blockstack.org/hub/"}),Promise.resolve(b((function(){return Promise.resolve(r.fetchNames()).then((function(t){return t&&(t[0]&&!r.defaultUsername&&(r.defaultUsername=t[0]),t.forEach((function(e){-1===r.usernames.findIndex((function(r){return r===e}))&&r.usernames.push(e)}))),Promise.resolve(r.profileUrl(e.gaiaUrl)).then((function(e){return Promise.resolve(U({profileUrl:e})).then((function(e){e&&(r.profile=e)}))}))}))}),(function(){})))},e}(),I=function(e,r){try{var t=function(){return{identityPublicKeychain:o,bitcoinPublicKeychain:a,firstBitcoinAddress:u,identityAddresses:c,identityKeypairs:d,identities:f}},n=_(e),i=H(e),o=n.neutered().toBase58(),s=i.neutered(),a=s.toBase58(),u=B(M(s)),c=[],d=[],f=[],l=0,h=function(e,r,t){for(var n;;){var i=e();if(g(i)&&(i=i.v),!i)return o;if(i.then){n=0;break}var o=t();if(o&&o.then){if(!g(o)){n=1;break}o=o.s}if(r){var s=r();if(s&&s.then&&!g(s)){n=2;break}}}var a=new m,u=P.bind(null,a,2);return(0===n?i.then(d):1===n?o.then(c):s.then(f)).then(void 0,u),a;function c(n){o=n;do{if(r&&(s=r())&&s.then&&!g(s))return void s.then(f).then(void 0,u);if(!(i=e())||g(i)&&!i.v)return void P(a,1,o);if(i.then)return void i.then(d).then(void 0,u);g(o=t())&&(o=o.v)}while(!o||!o.then);o.then(c).then(void 0,u)}function d(e){e?(o=t())&&o.then?o.then(c).then(void 0,u):c(o):P(a,1,o)}function f(){(i=e())?i.then?i.then(d).then(void 0,u):d(i):P(a,1,o)}}((function(){return l<r}),(function(){return l++}),(function(){return Promise.resolve(F(e,l)).then((function(e){f.push(e),d.push(e.keyPair),c.push(e.address)}))}));return Promise.resolve(h&&h.then?h.then(t):t())}catch(e){return Promise.reject(e)}},E=function(e,r){void 0===r&&(r=0);try{if(e.isNeutered())throw new Error("You need the private key to generate identity addresses");var t=Buffer.from(e.publicKey.toString("hex"));return Promise.resolve(o.createSha2Hash()).then((function(n){return Promise.resolve(n.digest(t,"sha256")).then((function(t){var n=t.toString("hex");return new x(e.deriveHardened(r),n)}))}))}catch(e){return Promise.reject(e)}};function _(e){return e.deriveHardened(888).deriveHardened(0)}function H(e){return e.deriveHardened(44).deriveHardened(0).deriveHardened(0)}function M(e,r,t){void 0===r&&(r=0),void 0===t&&(t="EXTERNAL_ADDRESS");var n=null;if("EXTERNAL_ADDRESS"===t)n=0;else{if("CHANGE_ADDRESS"!==t)throw new Error("Invalid chain type");n=1}return e.derive(n).derive(r)}function B(e){return i.publicKeyToAddress(e.publicKey)}function L(e){var r=e.getAddress(),t=e.getIdentityKey(),n=e.getIdentityKeyID(),i=e.getAppsNode(),o=e.getSTXNode();return{key:t,keyID:n,address:r,appsNodeKey:i.toBase58(),stxNodeKey:o.toBase58(),salt:e.getSalt()}}var R,F=function(e,r){try{var t=_(e);return Promise.resolve(E(t,r)).then((function(e){var r=L(e);return new j({keyPair:r,address:r.address,usernames:[]})}))}catch(e){return Promise.reject(e)}};function D(e){if(!e)throw new f.AssertionError({expected:!0,actual:e})}(R=exports.IdentityNameValidityError||(exports.IdentityNameValidityError={})).MINIMUM_LENGTH="error_minimum_length",R.MAXIMUM_LENGTH="error_maximum_length",R.ILLEGAL_CHARACTER="error_illegal_character",R.UNAVAILABLE="error_name_unavailable";var O,q=function(e){var r=e.length;return r<8?exports.IdentityNameValidityError.MINIMUM_LENGTH:r>37?exports.IdentityNameValidityError.MAXIMUM_LENGTH:/^[a-z0-9_]+$/.test(e)?null:exports.IdentityNameValidityError.ILLEGAL_CHARACTER},X=function(e,r){try{void 0===r&&(r=exports.Subdomains.BLOCKSTACK);var t=N[r].apiUrl+"/"+e.toLowerCase()+"."+r;return Promise.resolve(fetch(t)).then((function(e){return Promise.resolve(e.json())}))}catch(e){return Promise.reject(e)}},V=function e(r){var t=r.rootNode,n=r.index,i=void 0===n?1:n,o=r.identities,s=void 0===o?[]:o;try{return Promise.resolve(F(t,i)).then((function(r){return Promise.resolve(r.refresh()).then((function(){return r.defaultUsername?(s.push(r),e({rootNode:t,index:i+1,identities:s})):s}))}))}catch(e){return Promise.reject(e)}},z=function(e){try{return Promise.resolve(fetch("https://core.blockstack.org/v1/names/"+e)).then((function(e){return function(){if(e.ok)return Promise.resolve(e.json()).then((function(e){return s.parseZoneFile(e.zonefile).uri[0].target}))}()}))}catch(e){return Promise.reject(e)}},G=function(e,r){try{return Promise.resolve(h.decryptMnemonic(e,r,l.decrypt))}catch(e){return Promise.reject(e)}},W=function(e,r){try{return Promise.resolve(h.encryptMnemonic(e,r))}catch(e){return Promise.reject(e)}},J=function(e,r){try{return Promise.resolve(d.encryptMnemonic(e,r)).then((function(e){var r=e.toString("hex");return{encryptedMnemonic:e,encryptedMnemonicHex:r}}))}catch(e){return Promise.reject(e)}},Z=function(e){try{return Promise.resolve(r.mnemonicToSeed(e)).then((function(e){return t.bip32.fromSeed(e)}))}catch(e){return Promise.reject(e)}},Y=function(e,n){try{var i=r.generateMnemonic(n,c.randomBytes);return Promise.resolve(r.mnemonicToSeed(i)).then((function(r){var n=t.bip32.fromSeed(r);return Promise.resolve(W(i,e)).then((function(e){var r=e.toString("hex");return{rootNode:n,encryptedMnemonicPhrase:r}}))}))}catch(e){return Promise.reject(e)}},$=((O={})[v.ChainID.Mainnet]="m/44'/5757'/0'/0/0",O[v.ChainID.Testnet]="m/44'/5757'/0'/0/0",O);function Q(e){return $[e]}function ee(e){return function(r){var n=r.derivePath(Q(e));if(!n.privateKey)throw new Error("Unable to derive private key from `rootNode`, bip32 master keychain");var i=t.ECPair.fromPrivateKey(n.privateKey),o=d.ecPairToHexString(i);return{childKey:n,address:v.getAddressFromPrivateKey(o,e===v.ChainID.Mainnet?v.TransactionVersion.Mainnet:v.TransactionVersion.Testnet),privateKey:o}}}var re=function(){function e(e){this.privateKey=e.privateKey}var r=e.prototype;return r.getSTXAddress=function(e){return v.getAddressFromPrivateKey(this.getSTXPrivateKey(),e)},r.getSTXPrivateKey=function(){var e=t.bip32.fromBase58(this.privateKey);return D(e.privateKey),e.privateKey},r.getNetwork=function(){var e=new v.StacksTestnet;return e.coreApiUrl="https://sidecar.staging.blockstack.xyz",e},r.fetchAccount=function(e){var r=e.version,t=e.rpcClient;try{var n=this.getSTXAddress(r);return Promise.resolve(t.fetchAccount(n))}catch(e){return Promise.reject(e)}},r.signContractCall=function(e){var r=e.contractName,t=e.contractAddress,n=e.functionName,i=e.functionArgs,o=e.nonce,s=e.postConditionMode,a=e.postConditions;try{return Promise.resolve(v.makeContractCall({contractAddress:t,contractName:r,functionName:n,functionArgs:i,senderKey:this.getSTXPrivateKey().toString("hex"),nonce:new p(o),network:this.getNetwork(),postConditionMode:s,postConditions:a}))}catch(e){return Promise.reject(e)}},r.signContractDeploy=function(e){var r=e.contractName,t=e.codeBody,n=e.nonce,i=e.postConditionMode,o=e.postConditions;try{return Promise.resolve(v.makeContractDeploy({contractName:r,codeBody:t,senderKey:this.getSTXPrivateKey().toString("hex"),network:this.getNetwork(),nonce:new p(n),postConditionMode:i,postConditions:o}))}catch(e){return Promise.reject(e)}},r.signSTXTransfer=function(e){var r=e.recipient,t=e.amount,n=e.memo,i=e.nonce,o=e.postConditionMode,s=e.postConditions;try{return Promise.resolve(v.makeSTXTokenTransfer({recipient:r,amount:new p(t),memo:n,senderKey:this.getSTXPrivateKey().toString("hex"),network:this.getNetwork(),nonce:new p(i),postConditionMode:o,postConditions:s}))}catch(e){return Promise.reject(e)}},e}(),te=function(){function e(e){var r=e.encryptedBackupPhrase,t=e.identityPublicKeychain,n=e.bitcoinPublicKeychain,i=e.firstBitcoinAddress,o=e.identityKeypairs,s=e.identityAddresses,a=e.identities,u=e.configPrivateKey,c=e.stacksPrivateKey,d=e.walletConfig;this.chain=e.chain,this.encryptedBackupPhrase=r,this.identityPublicKeychain=t,this.bitcoinPublicKeychain=n,this.firstBitcoinAddress=i,this.identityKeypairs=o,this.identityAddresses=s,this.identities=a.map((function(e){return new j(e)})),this.configPrivateKey=u,this.stacksPrivateKey=c,this.walletConfig=d}e.generateFactory=function(e){var r=this;return function(t,n){try{return Promise.resolve(Y(t,e)).then((function(e){return r.createAccount({encryptedBackupPhrase:e.encryptedMnemonicPhrase,rootNode:e.rootNode,chain:n})}))}catch(e){return Promise.reject(e)}}},e.generate=function(e,r){try{return Promise.resolve(this.generateFactory(128)(e,r))}catch(e){return Promise.reject(e)}},e.generateStrong=function(e,r){try{return Promise.resolve(this.generateFactory(256)(e,r))}catch(e){return Promise.reject(e)}},e.restore=function(r,t,n){try{return Promise.resolve(Z(t)).then((function(i){return Promise.resolve(J(t,r)).then((function(r){return Promise.resolve(e.createAccount({encryptedBackupPhrase:r.encryptedMnemonicHex,rootNode:i,chain:n})).then((function(e){return Promise.resolve(e.restoreIdentities({rootNode:i,gaiaReadURL:"https://gaia.blockstack.org/hub/"}))}))}))}))}catch(e){return Promise.reject(e)}},e.createAccount=function(r){var t=r.encryptedBackupPhrase,n=r.rootNode,i=r.chain,o=r.identitiesToGenerate,s=void 0===o?1:o;try{var a=n.deriveHardened(45).privateKey;if(!a)throw new TypeError("Unable to derive config key for wallet identities");var u=a.toString("hex"),c=ee(i)(n).childKey;return Promise.resolve(I(n,s)).then((function(r){return new e(y({},r,{chain:i,configPrivateKey:u,stacksPrivateKey:c.toBase58(),encryptedBackupPhrase:t}))}))}catch(e){return Promise.reject(e)}};var n=e.prototype;return n.restoreIdentities=function(e){var r,t,n=e.rootNode,i=e.gaiaReadURL;try{var o=this,s={url_prefix:(r={readURL:i,privateKey:o.configPrivateKey}).readURL,max_file_upload_size_megabytes:100,address:d.ecPairToAddress(d.hexStringToECPair((t=r.privateKey)+(64===t.length?"01":""))),token:"not_used",server:"not_used"};return Promise.resolve(o.fetchConfig(s)).then((function(){var e=!1;function r(r){return e?r:Promise.resolve(o.identities[0].refresh()).then((function(){return Promise.resolve(V({rootNode:n})).then((function(e){return o.identities=o.identities.concat(e),o}))}))}var t=function(){if(o.walletConfig){var r=o.walletConfig.identities.map((function(e,r){try{var t=function(){return e.username&&(i.usernames=[e.username],i.defaultUsername=e.username),i},i=o.identities[r],s=function(){if(!i)return Promise.resolve(F(n,r)).then((function(e){i=e}))}();return Promise.resolve(s&&s.then?s.then(t):t())}catch(e){return Promise.reject(e)}}));return Promise.resolve(Promise.all(r)).then((function(r){return o.identities=r,e=!0,o}))}}();return t&&t.then?t.then(r):r(t)}))}catch(e){return Promise.reject(e)}},n.createNewIdentity=function(e){try{var n=this;return Promise.resolve(G(Buffer.from(n.encryptedBackupPhrase,"hex"),e)).then((function(e){return Promise.resolve(r.mnemonicToSeed(e)).then((function(e){var r=t.bip32.fromSeed(e);return Promise.resolve(F(r,n.identities.length)).then((function(e){return n.identities.push(e),n.identityKeypairs.push(e.keyPair),n.identityAddresses.push(e.address),e}))}))}))}catch(e){return Promise.reject(e)}},n.createGaiaConfig=function(e){try{return Promise.resolve(d.connectToGaiaHub(e,this.configPrivateKey))}catch(e){return Promise.reject(e)}},n.fetchConfig=function(e){var r=this;return Promise.resolve(b((function(){return Promise.resolve(fetch(""+e.url_prefix+e.address+"/wallet-config.json")).then((function(e){return Promise.resolve(e.text()).then((function(e){return Promise.resolve(d.decryptContent(e,{privateKey:r.configPrivateKey})).then((function(e){var t=JSON.parse(e);return r.walletConfig=t,t}))}))}))}),(function(){return null})))},n.getOrCreateConfig=function(e){var r=e.gaiaConfig,t=e.skipUpload;try{var n=this;return n.walletConfig?Promise.resolve(n.walletConfig):Promise.resolve(n.fetchConfig(r)).then((function(e){if(e)return e;var i={identities:n.identities.map((function(e){return{username:e.defaultUsername,address:e.address,apps:{}}}))};n.walletConfig=i;var o=function(){if(!t)return Promise.resolve(n.updateConfig(r)).then((function(){}))}();return o&&o.then?o.then((function(){return i})):i}))}catch(e){return Promise.reject(e)}},n.updateConfig=function(e){try{var r=d.getPublicKeyFromPrivate(this.configPrivateKey);return Promise.resolve(d.encryptContent(JSON.stringify(this.walletConfig),{publicKey:r})).then((function(r){return Promise.resolve(S("wallet-config.json",r,e)).then((function(){}))}))}catch(e){return Promise.reject(e)}},n.updateConfigWithAuth=function(e){var r=e.identityIndex,t=e.app,n=e.gaiaConfig;try{var i=this,o=i.walletConfig;D(o),i.identities.forEach((function(e,r){var t,n=o.identities[r];n?(n.apps=n.apps||{},n.username=e.defaultUsername,n.address=e.address,o.identities[r]=n):null===(t=i.walletConfig)||void 0===t||t.identities.push({username:e.defaultUsername,address:e.address,apps:{}})}));var s=o.identities[r];return s.apps=s.apps||{},s.apps[t.origin]=t,o.identities[r]=s,i.walletConfig=o,Promise.resolve(i.updateConfig(n)).then((function(){}))}catch(e){return Promise.reject(e)}},n.updateConfigForReuseWarning=function(e){var r=e.gaiaConfig;try{return D(this.walletConfig),this.walletConfig.hideWarningForReusingIdentity=!0,Promise.resolve(this.updateConfig(r)).then((function(){}))}catch(e){return Promise.reject(e)}},n.getSigner=function(){return new re({privateKey:this.stacksPrivateKey})},e}(),ne={Wallet:te};exports.DEFAULT_PROFILE=C,exports.Identity=j,exports.Wallet=te,exports.WalletSigner=re,exports.assertIsTruthy=D,exports.decrypt=G,exports.default=ne,exports.derivationPaths=$,exports.deriveIdentityKeyPair=L,exports.deriveRootKeychainFromMnemonic=Z,exports.deriveStxAddressChain=ee,exports.encrypt=W,exports.encryptMnemonicFormatted=J,exports.fetchProfile=U,exports.generateEncryptedMnemonicRootKeychain=Y,exports.generateMnemonicRootKeychain=function(e){try{var n=r.generateMnemonic(e,c.randomBytes);return Promise.resolve(r.mnemonicToSeed(n)).then((function(e){return{rootNode:t.bip32.fromSeed(e),plaintextMnemonic:n}}))}catch(e){return Promise.reject(e)}},exports.getAddress=B,exports.getBitcoinAddressNode=M,exports.getBitcoinPrivateKeychain=H,exports.getBlockchainIdentities=I,exports.getDerivationPath=Q,exports.getIdentityOwnerAddressNode=E,exports.getIdentityPrivateKeychain=_,exports.getProfileURLFromZoneFile=z,exports.makeIdentity=F,exports.recursiveRestoreIdentities=V,exports.registerSubdomain=function(e){var r=e.identity,t=e.gaiaHubUrl,n=e.username,i=e.subdomain;try{var o=w(r.profile||C,r.keyPair);return Promise.resolve(A({gaiaHubUrl:t,filePath:"profile.json",identity:r,signedProfileTokenData:o})).then((function(e){var t=n+"."+i,o=d.makeProfileZoneFile(t,e);return Promise.resolve(function(e){var r=e.username,t=e.subdomain,n=e.zoneFile,i=e.identity;try{var o=N[t].registerUrl,s=JSON.stringify({name:r,owner_address:i.address,zonefile:n});return Promise.resolve(fetch(o,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json"},body:s})).then((function(e){return e.ok?e.json():Promise.reject({error:"Failed to register username",status:e.status})}))}catch(e){return Promise.reject(e)}}({username:n,subdomain:i,zoneFile:o,identity:r})).then((function(){return r.defaultUsername=t,r.usernames.push(t),r}))}))}catch(e){return Promise.reject(e)}},exports.registrars=N,exports.signAndUploadProfile=T,exports.signProfileForUpload=w,exports.uploadProfile=A,exports.validateSubdomain=function(e,r){try{var t=!1;void 0===r&&(r=exports.Subdomains.BLOCKSTACK);var n=q(e);if(n)return Promise.resolve(n);var i=b((function(){return Promise.resolve(X(e,r)).then((function(e){if("available"!==e.status)return t=!0,exports.IdentityNameValidityError.UNAVAILABLE}))}),(function(){return t=!0,exports.IdentityNameValidityError.UNAVAILABLE}));return Promise.resolve(i&&i.then?i.then((function(e){return t?e:null})):t?i:null)}catch(e){return Promise.reject(e)}},exports.validateSubdomainAvailability=X,exports.validateSubdomainFormat=q;
"use strict";var e,r=require("bip39"),t=require("bitcoinjs-lib"),n=require("blockstack/lib/wallet"),i=require("blockstack/lib/keys"),o=require("blockstack/lib/encryption/sha2Hash"),s=require("zone-file"),a=require("blockstack/lib/auth/authMessages"),u=require("jsontokens"),c=require("blockstack/lib/encryption/cryptoRandom"),d=require("blockstack"),f=require("assert"),l=require("triplesec"),h=require("blockstack/lib/encryption/wallet"),p=require("@blockstack/stacks-transactions"),v=(e=require("bn.js"))&&"object"==typeof e&&"default"in e?e.default:e;function y(){return(y=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}var m=function(){function e(){}return e.prototype.then=function(r,t){var n=new e,i=this.s;if(i){var o=1&i?r:t;if(o){try{P(n,1,o(this.v))}catch(e){P(n,2,e)}return n}return this}return this.o=function(e){try{var i=e.v;1&e.s?P(n,1,r?r(i):i):t?P(n,1,t(i)):P(n,2,i)}catch(e){P(n,2,e)}},n},e}();function P(e,r,t){if(!e.s){if(t instanceof m){if(!t.s)return void(t.o=P.bind(null,e,r));1&r&&(r=t.s),t=t.v}if(t&&t.then)return void t.then(P.bind(null,e,r),P.bind(null,e,2));e.s=r,e.v=t;var n=e.o;n&&n(e)}}function g(e){return e instanceof m&&1&e.s}function b(e,r){try{var t=e()}catch(e){return r(e)}return t&&t.then?t.then(void 0,r):t}"undefined"!=typeof Symbol&&(Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator"))),"undefined"!=typeof Symbol&&(Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator")));var k,K,x=function(){function e(e,r){this.hdNode=e,this.salt=r}var r=e.prototype;return r.getNode=function(){return this.hdNode},r.getSalt=function(){return this.salt},r.getIdentityKey=function(){if(!this.hdNode.privateKey)throw new Error("Node does not have private key");return this.hdNode.privateKey.toString("hex")},r.getIdentityKeyID=function(){return this.hdNode.publicKey.toString("hex")},r.getAppsNode=function(){return this.hdNode.deriveHardened(0)},r.getAddress=function(){return B(this.hdNode)},r.getEncryptionNode=function(){return this.hdNode.deriveHardened(2)},r.getSigningNode=function(){return this.hdNode.deriveHardened(1)},r.getSTXNode=function(){return this.hdNode.deriveHardened(6)},r.getAppNode=function(e){return n.getLegacyAppNode(this.hdNode,this.salt,e)},r.getAppPrivateKey=function(e){var r=this.getAppNode(e);if(!r.privateKey)throw new Error("App node does not have private key");return r.privateKey.toString("hex")},r.getAppAddress=function(e){var r=this.getAppNode(e);return i.publicKeyToAddress(r.publicKey)},e}(),S=function(e,r,t){try{return Promise.resolve(fetch(t.server+"/store/"+t.address+"/"+e,{method:"POST",headers:{"Content-Type":"application/json",Authorization:"bearer "+t.token},body:r,referrer:"no-referrer",referrerPolicy:"no-referrer"})).then((function(e){return Promise.resolve(e.json()).then((function(e){return e.publicURL}))}))}catch(e){return Promise.reject(e)}},A=function(e){var r=e.gaiaHubUrl,t=e.filePath,n=e.identity,i=e.signedProfileTokenData,o=e.gaiaHubConfig;try{var s=function(e){return Promise.resolve(S(t,i,e))};return Promise.resolve(o?s(o):Promise.resolve(d.connectToGaiaHub(r,n.keyPair.key)).then(s))}catch(e){return Promise.reject(e)}},C={"@type":"Person","@context":"http://schema.org"};(K=exports.Subdomains||(exports.Subdomains={})).TEST="test-personal.id",K.BLOCKSTACK="id.blockstack";var N=((k={})[exports.Subdomains.TEST]={registerUrl:"https://test-registrar.blockstack.org/register",apiUrl:"https://test-registrar.blockstack.org/v1/names"},k[exports.Subdomains.BLOCKSTACK]={registerUrl:"https://registrar.blockstack.org/register",apiUrl:"https://registrar.blockstack.org/v1/names"},k);function w(e,r){var t=d.signProfileToken(e,r.key,{publicKey:r.keyID}),n=d.wrapProfileToken(t);return JSON.stringify([n],null,2)}var T=function(e){var r=e.profile,t=e.gaiaHubUrl,n=e.filePath,i=e.identity,o=e.gaiaHubConfig;try{var s=w(r,i.keyPair);return Promise.resolve(A({gaiaHubUrl:t,filePath:n,identity:i,signedProfileTokenData:s,gaiaHubConfig:o})).then((function(){}))}catch(e){return Promise.reject(e)}},U=function(e){var r=e.profileUrl;return Promise.resolve(b((function(){return Promise.resolve(fetch(r)).then((function(e){var r=!1;function t(t){if(r)return t;if(404===e.status)return null;throw new Error("Network error when fetching profile")}var n=function(){if(e.ok)return Promise.resolve(e.json()).then((function(e){var t;return r=!0,null===(t=e[0].decodedToken.payload)||void 0===t?void 0:t.claim}))}();return n&&n.then?n.then(t):t(n)}))}),(function(){return null})))},j=function(){function e(e){var r=e.address,t=e.usernames,n=e.defaultUsername,i=e.profile;this.keyPair=e.keyPair,this.address=r,this.usernames=t||[],this.defaultUsername=n,this.profile=i}var r=e.prototype;return r.makeAuthResponse=function(e){var r=e.appDomain,n=e.gaiaUrl,o=e.transitPublicKey,s=e.scopes,f=void 0===s?[]:s,l=e.stxAddress;try{var h=this,p=h.appPrivateKey(r);return Promise.resolve(function(e){try{return Promise.resolve(fetch(e+"/hub_info")).then((function(e){return Promise.resolve(e.json())}))}catch(e){return Promise.reject(e)}}(n)).then((function(e){return Promise.resolve(h.profileUrl(e.read_url_prefix)).then((function(s){return Promise.resolve(U({profileUrl:s})).then((function(v){function m(){h.profile=v;var e,r,t,d,f,m=(e=i.getPublicKeyFromPrivate(p.slice(0,64)),r=h.keyPair.key.slice(0,64),t=i.getPublicKeyFromPrivate(r),d=c.randomBytes(16).toString("hex"),f={childToAssociate:e,iss:t,exp:31536e3+(new Date).getTime()/1e3,iat:Date.now()/1e3,salt:d},new u.TokenSigner("ES256K",r).sign(f));return a.makeAuthResponse(h.keyPair.key,y({},h.profile||{},{stxAddress:l}),h.defaultUsername||"",{profileUrl:s},void 0,p,void 0,o,n,void 0,m)}var P=function(){if(f.includes("publish_data")){v.apps||(v.apps={});var o=t.ECPair.fromPrivateKey(Buffer.from(p,"hex")),a=d.ecPairToAddress(o),l=""+e.read_url_prefix+a+"/";v.apps[r]=l,v.appsMeta||(v.appsMeta={}),v.appsMeta[r]={storage:l,publicKey:o.publicKey.toString("hex")};var y=function(e){var r=e.hubInfo,t=e.privateKey,n=e.gaiaHubUrl,o=r.read_url_prefix,s=function(e){var r=e.privateKey,t=e.hubInfo.challenge_text,n={gaiaHubUrl:e.gaiaHubUrl,iss:i.getPublicKeyFromPrivate(r),salt:c.randomBytes(16).toString("hex")};return t&&(n.gaiaChallenge=t),"v1:"+new u.TokenSigner("ES256K",r).sign(n)}({hubInfo:r,privateKey:t,gaiaHubUrl:n});return{url_prefix:o,max_file_upload_size_megabytes:100,address:d.ecPairToAddress(d.hexStringToECPair(t+(64===t.length?"01":""))),token:s,server:n}}({hubInfo:e,privateKey:h.keyPair.key,gaiaHubUrl:n}),m=h.gaiaReadUrlToWritePath(s,e.read_url_prefix,a);return Promise.resolve(T({profile:v,identity:h,gaiaHubUrl:n,filePath:m,gaiaHubConfig:y})).then((function(){}))}}();return P&&P.then?P.then(m):m()}))}))}))}catch(e){return Promise.reject(e)}},r.appPrivateKey=function(e){var r=this.keyPair,n=r.salt;return new x(t.bip32.fromBase58(r.appsNodeKey),n).getAppPrivateKey(e)},r.profileUrl=function(e){try{var r=!1,t=this,n=function(){if(t.defaultUsername)return b((function(){return Promise.resolve(G(t.defaultUsername)).then((function(e){if(e)return r=!0,e}))}),(function(e){console.warn("Error fetching profile URL from zone file:",e)}))}();return Promise.resolve(n&&n.then?n.then((function(n){return r?n:""+e+t.address+"/profile.json"})):r?n:""+e+t.address+"/profile.json")}catch(e){return Promise.reject(e)}},r.gaiaReadUrlToWritePath=function(e,r,t){return e.substr((""+r+t+"/").length)},r.fetchNames=function(){try{return Promise.resolve(fetch("https://core.blockstack.org/v1/addresses/bitcoin/"+this.address)).then((function(e){return Promise.resolve(e.json()).then((function(e){return e.names}))}))}catch(e){return Promise.reject(e)}},r.refresh=function(e){var r=this;return void 0===e&&(e={gaiaUrl:"https://gaia.blockstack.org/hub/"}),Promise.resolve(b((function(){return Promise.resolve(r.fetchNames()).then((function(t){return t&&(t[0]&&!r.defaultUsername&&(r.defaultUsername=t[0]),t.forEach((function(e){-1===r.usernames.findIndex((function(r){return r===e}))&&r.usernames.push(e)}))),Promise.resolve(r.profileUrl(e.gaiaUrl)).then((function(e){return Promise.resolve(U({profileUrl:e})).then((function(e){e&&(r.profile=e)}))}))}))}),(function(){})))},e}(),I=function(e,r){try{var t=function(){return{identityPublicKeychain:o,bitcoinPublicKeychain:a,firstBitcoinAddress:u,identityAddresses:c,identityKeypairs:d,identities:f}},n=_(e),i=H(e),o=n.neutered().toBase58(),s=i.neutered(),a=s.toBase58(),u=B(M(s)),c=[],d=[],f=[],l=0,h=function(e,r,t){for(var n;;){var i=e();if(g(i)&&(i=i.v),!i)return o;if(i.then){n=0;break}var o=t();if(o&&o.then){if(!g(o)){n=1;break}o=o.s}if(r){var s=r();if(s&&s.then&&!g(s)){n=2;break}}}var a=new m,u=P.bind(null,a,2);return(0===n?i.then(d):1===n?o.then(c):s.then(f)).then(void 0,u),a;function c(n){o=n;do{if(r&&(s=r())&&s.then&&!g(s))return void s.then(f).then(void 0,u);if(!(i=e())||g(i)&&!i.v)return void P(a,1,o);if(i.then)return void i.then(d).then(void 0,u);g(o=t())&&(o=o.v)}while(!o||!o.then);o.then(c).then(void 0,u)}function d(e){e?(o=t())&&o.then?o.then(c).then(void 0,u):c(o):P(a,1,o)}function f(){(i=e())?i.then?i.then(d).then(void 0,u):d(i):P(a,1,o)}}((function(){return l<r}),(function(){return l++}),(function(){return Promise.resolve(F(e,l)).then((function(e){f.push(e),d.push(e.keyPair),c.push(e.address)}))}));return Promise.resolve(h&&h.then?h.then(t):t())}catch(e){return Promise.reject(e)}},E=function(e,r){void 0===r&&(r=0);try{if(e.isNeutered())throw new Error("You need the private key to generate identity addresses");var t=Buffer.from(e.publicKey.toString("hex"));return Promise.resolve(o.createSha2Hash()).then((function(n){return Promise.resolve(n.digest(t,"sha256")).then((function(t){var n=t.toString("hex");return new x(e.deriveHardened(r),n)}))}))}catch(e){return Promise.reject(e)}};function _(e){return e.deriveHardened(888).deriveHardened(0)}function H(e){return e.deriveHardened(44).deriveHardened(0).deriveHardened(0)}function M(e,r,t){void 0===r&&(r=0),void 0===t&&(t="EXTERNAL_ADDRESS");var n=null;if("EXTERNAL_ADDRESS"===t)n=0;else{if("CHANGE_ADDRESS"!==t)throw new Error("Invalid chain type");n=1}return e.derive(n).derive(r)}function B(e){return i.publicKeyToAddress(e.publicKey)}function L(e){var r=e.getAddress(),t=e.getIdentityKey(),n=e.getIdentityKeyID(),i=e.getAppsNode(),o=e.getSTXNode();return{key:t,keyID:n,address:r,appsNodeKey:i.toBase58(),stxNodeKey:o.toBase58(),salt:e.getSalt()}}var R,F=function(e,r){try{var t=_(e);return Promise.resolve(E(t,r)).then((function(e){var r=L(e);return new j({keyPair:r,address:r.address,usernames:[]})}))}catch(e){return Promise.reject(e)}};function D(e){if(!e)throw new f.AssertionError({expected:!0,actual:e})}(R=exports.IdentityNameValidityError||(exports.IdentityNameValidityError={})).MINIMUM_LENGTH="error_minimum_length",R.MAXIMUM_LENGTH="error_maximum_length",R.ILLEGAL_CHARACTER="error_illegal_character",R.UNAVAILABLE="error_name_unavailable";var O,q=function(e){var r=e.length;return r<8?exports.IdentityNameValidityError.MINIMUM_LENGTH:r>37?exports.IdentityNameValidityError.MAXIMUM_LENGTH:/^[a-z0-9_]+$/.test(e)?null:exports.IdentityNameValidityError.ILLEGAL_CHARACTER},X=function(e,r){try{void 0===r&&(r=exports.Subdomains.BLOCKSTACK);var t=N[r].apiUrl+"/"+e.toLowerCase()+"."+r;return Promise.resolve(fetch(t)).then((function(e){return Promise.resolve(e.json())}))}catch(e){return Promise.reject(e)}},V=function e(r){var t=r.rootNode,n=r.index,i=void 0===n?1:n,o=r.identities,s=void 0===o?[]:o;try{return Promise.resolve(F(t,i)).then((function(r){return Promise.resolve(r.refresh()).then((function(){return r.defaultUsername?(s.push(r),e({rootNode:t,index:i+1,identities:s})):s}))}))}catch(e){return Promise.reject(e)}},G=function(e){try{return Promise.resolve(fetch("https://core.blockstack.org/v1/names/"+e)).then((function(e){return function(){if(e.ok)return Promise.resolve(e.json()).then((function(e){return s.parseZoneFile(e.zonefile).uri[0].target}))}()}))}catch(e){return Promise.reject(e)}},z=function(e,r){try{return Promise.resolve(h.decryptMnemonic(e,r,l.decrypt))}catch(e){return Promise.reject(e)}},W=function(e,r){try{return Promise.resolve(h.encryptMnemonic(e,r))}catch(e){return Promise.reject(e)}},J=function(e,r){try{return Promise.resolve(d.encryptMnemonic(e,r)).then((function(e){var r=e.toString("hex");return{encryptedMnemonic:e,encryptedMnemonicHex:r}}))}catch(e){return Promise.reject(e)}},Z=function(e){try{return Promise.resolve(r.mnemonicToSeed(e)).then((function(e){return t.bip32.fromSeed(e)}))}catch(e){return Promise.reject(e)}},Y=function(e,n){try{var i=r.generateMnemonic(n,c.randomBytes);return Promise.resolve(r.mnemonicToSeed(i)).then((function(r){var n=t.bip32.fromSeed(r);return Promise.resolve(W(i,e)).then((function(e){var r=e.toString("hex");return{rootNode:n,encryptedMnemonicPhrase:r}}))}))}catch(e){return Promise.reject(e)}},$=((O={})[p.ChainID.Mainnet]="m/44'/5757'/0'/0/0",O[p.ChainID.Testnet]="m/44'/5757'/0'/0/0",O);function Q(e){return $[e]}function ee(e){return function(r){var n=r.derivePath(Q(e));if(!n.privateKey)throw new Error("Unable to derive private key from `rootNode`, bip32 master keychain");var i=t.ECPair.fromPrivateKey(n.privateKey),o=d.ecPairToHexString(i);return{childKey:n,address:p.getAddressFromPrivateKey(o,e===p.ChainID.Mainnet?p.TransactionVersion.Mainnet:p.TransactionVersion.Testnet),privateKey:o}}}var re=function(){function e(e){this.privateKey=e.privateKey}var r=e.prototype;return r.getSTXAddress=function(e){return p.getAddressFromPrivateKey(this.getSTXPrivateKey(),e)},r.getSTXPrivateKey=function(){var e=t.bip32.fromBase58(this.privateKey);return D(e.privateKey),e.privateKey},r.getNetwork=function(){var e=new p.StacksTestnet;return e.coreApiUrl="https://stacks-node-api.blockstack.org",e},r.fetchAccount=function(e){var r=e.version,t=e.rpcClient;try{var n=this.getSTXAddress(r);return Promise.resolve(t.fetchAccount(n))}catch(e){return Promise.reject(e)}},r.signContractCall=function(e){var r=e.contractName,t=e.contractAddress,n=e.functionName,i=e.functionArgs,o=e.nonce,s=e.postConditionMode,a=e.postConditions;try{return Promise.resolve(p.makeContractCall({contractAddress:t,contractName:r,functionName:n,functionArgs:i,senderKey:this.getSTXPrivateKey().toString("hex"),nonce:new v(o),network:this.getNetwork(),postConditionMode:s,postConditions:a}))}catch(e){return Promise.reject(e)}},r.signContractDeploy=function(e){var r=e.contractName,t=e.codeBody,n=e.nonce,i=e.postConditionMode,o=e.postConditions;try{return Promise.resolve(p.makeContractDeploy({contractName:r,codeBody:t,senderKey:this.getSTXPrivateKey().toString("hex"),network:this.getNetwork(),nonce:new v(n),postConditionMode:i,postConditions:o}))}catch(e){return Promise.reject(e)}},r.signSTXTransfer=function(e){var r=e.recipient,t=e.amount,n=e.memo,i=e.nonce,o=e.postConditionMode,s=e.postConditions;try{return Promise.resolve(p.makeSTXTokenTransfer({recipient:r,amount:new v(t),memo:n,senderKey:this.getSTXPrivateKey().toString("hex"),network:this.getNetwork(),nonce:new v(i),postConditionMode:o,postConditions:s}))}catch(e){return Promise.reject(e)}},e}(),te=function(){function e(e){var r=e.encryptedBackupPhrase,t=e.identityPublicKeychain,n=e.bitcoinPublicKeychain,i=e.firstBitcoinAddress,o=e.identityKeypairs,s=e.identityAddresses,a=e.identities,u=e.configPrivateKey,c=e.stacksPrivateKey,d=e.walletConfig;this.chain=e.chain,this.encryptedBackupPhrase=r,this.identityPublicKeychain=t,this.bitcoinPublicKeychain=n,this.firstBitcoinAddress=i,this.identityKeypairs=o,this.identityAddresses=s,this.identities=a.map((function(e){return new j(e)})),this.configPrivateKey=u,this.stacksPrivateKey=c,this.walletConfig=d}e.generateFactory=function(e){var r=this;return function(t,n){try{return Promise.resolve(Y(t,e)).then((function(e){return r.createAccount({encryptedBackupPhrase:e.encryptedMnemonicPhrase,rootNode:e.rootNode,chain:n})}))}catch(e){return Promise.reject(e)}}},e.generate=function(e,r){try{return Promise.resolve(this.generateFactory(128)(e,r))}catch(e){return Promise.reject(e)}},e.generateStrong=function(e,r){try{return Promise.resolve(this.generateFactory(256)(e,r))}catch(e){return Promise.reject(e)}},e.restore=function(r,t,n){try{return Promise.resolve(Z(t)).then((function(i){return Promise.resolve(J(t,r)).then((function(r){return Promise.resolve(e.createAccount({encryptedBackupPhrase:r.encryptedMnemonicHex,rootNode:i,chain:n})).then((function(e){return Promise.resolve(e.restoreIdentities({rootNode:i,gaiaReadURL:"https://gaia.blockstack.org/hub/"}))}))}))}))}catch(e){return Promise.reject(e)}},e.createAccount=function(r){var t=r.encryptedBackupPhrase,n=r.rootNode,i=r.chain,o=r.identitiesToGenerate,s=void 0===o?1:o;try{var a=n.deriveHardened(45).privateKey;if(!a)throw new TypeError("Unable to derive config key for wallet identities");var u=a.toString("hex"),c=ee(i)(n).childKey;return Promise.resolve(I(n,s)).then((function(r){return new e(y({},r,{chain:i,configPrivateKey:u,stacksPrivateKey:c.toBase58(),encryptedBackupPhrase:t}))}))}catch(e){return Promise.reject(e)}};var n=e.prototype;return n.restoreIdentities=function(e){var r,t,n=e.rootNode,i=e.gaiaReadURL;try{var o=this,s={url_prefix:(r={readURL:i,privateKey:o.configPrivateKey}).readURL,max_file_upload_size_megabytes:100,address:d.ecPairToAddress(d.hexStringToECPair((t=r.privateKey)+(64===t.length?"01":""))),token:"not_used",server:"not_used"};return Promise.resolve(o.fetchConfig(s)).then((function(){var e=!1;function r(r){return e?r:Promise.resolve(o.identities[0].refresh()).then((function(){return Promise.resolve(V({rootNode:n})).then((function(e){return o.identities=o.identities.concat(e),o}))}))}var t=function(){if(o.walletConfig){var r=o.walletConfig.identities.map((function(e,r){try{var t=function(){return e.username&&(i.usernames=[e.username],i.defaultUsername=e.username),i},i=o.identities[r],s=function(){if(!i)return Promise.resolve(F(n,r)).then((function(e){i=e}))}();return Promise.resolve(s&&s.then?s.then(t):t())}catch(e){return Promise.reject(e)}}));return Promise.resolve(Promise.all(r)).then((function(r){return o.identities=r,e=!0,o}))}}();return t&&t.then?t.then(r):r(t)}))}catch(e){return Promise.reject(e)}},n.createNewIdentity=function(e){try{var n=this;return Promise.resolve(z(Buffer.from(n.encryptedBackupPhrase,"hex"),e)).then((function(e){return Promise.resolve(r.mnemonicToSeed(e)).then((function(e){var r=t.bip32.fromSeed(e);return Promise.resolve(F(r,n.identities.length)).then((function(e){return n.identities.push(e),n.identityKeypairs.push(e.keyPair),n.identityAddresses.push(e.address),e}))}))}))}catch(e){return Promise.reject(e)}},n.createGaiaConfig=function(e){try{return Promise.resolve(d.connectToGaiaHub(e,this.configPrivateKey))}catch(e){return Promise.reject(e)}},n.fetchConfig=function(e){var r=this;return Promise.resolve(b((function(){return Promise.resolve(fetch(""+e.url_prefix+e.address+"/wallet-config.json")).then((function(e){return Promise.resolve(e.text()).then((function(e){return Promise.resolve(d.decryptContent(e,{privateKey:r.configPrivateKey})).then((function(e){var t=JSON.parse(e);return r.walletConfig=t,t}))}))}))}),(function(){return null})))},n.getOrCreateConfig=function(e){var r=e.gaiaConfig,t=e.skipUpload;try{var n=this;return n.walletConfig?Promise.resolve(n.walletConfig):Promise.resolve(n.fetchConfig(r)).then((function(e){if(e)return e;var i={identities:n.identities.map((function(e){return{username:e.defaultUsername,address:e.address,apps:{}}}))};n.walletConfig=i;var o=function(){if(!t)return Promise.resolve(n.updateConfig(r)).then((function(){}))}();return o&&o.then?o.then((function(){return i})):i}))}catch(e){return Promise.reject(e)}},n.updateConfig=function(e){try{var r=d.getPublicKeyFromPrivate(this.configPrivateKey);return Promise.resolve(d.encryptContent(JSON.stringify(this.walletConfig),{publicKey:r})).then((function(r){return Promise.resolve(S("wallet-config.json",r,e)).then((function(){}))}))}catch(e){return Promise.reject(e)}},n.updateConfigWithAuth=function(e){var r=e.identityIndex,t=e.app,n=e.gaiaConfig;try{var i=this,o=i.walletConfig;D(o),i.identities.forEach((function(e,r){var t,n=o.identities[r];n?(n.apps=n.apps||{},n.username=e.defaultUsername,n.address=e.address,o.identities[r]=n):null===(t=i.walletConfig)||void 0===t||t.identities.push({username:e.defaultUsername,address:e.address,apps:{}})}));var s=o.identities[r];return s.apps=s.apps||{},s.apps[t.origin]=t,o.identities[r]=s,i.walletConfig=o,Promise.resolve(i.updateConfig(n)).then((function(){}))}catch(e){return Promise.reject(e)}},n.updateConfigForReuseWarning=function(e){var r=e.gaiaConfig;try{return D(this.walletConfig),this.walletConfig.hideWarningForReusingIdentity=!0,Promise.resolve(this.updateConfig(r)).then((function(){}))}catch(e){return Promise.reject(e)}},n.getSigner=function(){return new re({privateKey:this.stacksPrivateKey})},e}(),ne={Wallet:te};exports.DEFAULT_PROFILE=C,exports.Identity=j,exports.Wallet=te,exports.WalletSigner=re,exports.assertIsTruthy=D,exports.decrypt=z,exports.default=ne,exports.derivationPaths=$,exports.deriveIdentityKeyPair=L,exports.deriveRootKeychainFromMnemonic=Z,exports.deriveStxAddressChain=ee,exports.encrypt=W,exports.encryptMnemonicFormatted=J,exports.fetchProfile=U,exports.generateEncryptedMnemonicRootKeychain=Y,exports.generateMnemonicRootKeychain=function(e){try{var n=r.generateMnemonic(e,c.randomBytes);return Promise.resolve(r.mnemonicToSeed(n)).then((function(e){return{rootNode:t.bip32.fromSeed(e),plaintextMnemonic:n}}))}catch(e){return Promise.reject(e)}},exports.getAddress=B,exports.getBitcoinAddressNode=M,exports.getBitcoinPrivateKeychain=H,exports.getBlockchainIdentities=I,exports.getDerivationPath=Q,exports.getIdentityOwnerAddressNode=E,exports.getIdentityPrivateKeychain=_,exports.getProfileURLFromZoneFile=G,exports.makeIdentity=F,exports.recursiveRestoreIdentities=V,exports.registerSubdomain=function(e){var r=e.identity,t=e.gaiaHubUrl,n=e.username,i=e.subdomain;try{var o=w(r.profile||C,r.keyPair);return Promise.resolve(A({gaiaHubUrl:t,filePath:"profile.json",identity:r,signedProfileTokenData:o})).then((function(e){var t=n+"."+i,o=d.makeProfileZoneFile(t,e);return Promise.resolve(function(e){var r=e.username,t=e.subdomain,n=e.zoneFile,i=e.identity;try{var o=N[t].registerUrl,s=JSON.stringify({name:r,owner_address:i.address,zonefile:n});return Promise.resolve(fetch(o,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json"},body:s})).then((function(e){return e.ok?e.json():Promise.reject({error:"Failed to register username",status:e.status})}))}catch(e){return Promise.reject(e)}}({username:n,subdomain:i,zoneFile:o,identity:r})).then((function(){return r.defaultUsername=t,r.usernames.push(t),r}))}))}catch(e){return Promise.reject(e)}},exports.registrars=N,exports.signAndUploadProfile=T,exports.signProfileForUpload=w,exports.uploadProfile=A,exports.validateSubdomain=function(e,r){try{var t=!1;void 0===r&&(r=exports.Subdomains.BLOCKSTACK);var n=q(e);if(n)return Promise.resolve(n);var i=b((function(){return Promise.resolve(X(e,r)).then((function(e){if("available"!==e.status)return t=!0,exports.IdentityNameValidityError.UNAVAILABLE}))}),(function(){return t=!0,exports.IdentityNameValidityError.UNAVAILABLE}));return Promise.resolve(i&&i.then?i.then((function(e){return t?e:null})):t?i:null)}catch(e){return Promise.reject(e)}},exports.validateSubdomainAvailability=X,exports.validateSubdomainFormat=q;
//# sourceMappingURL=keychain.cjs.production.min.js.map

@@ -1191,3 +1191,3 @@ import { mnemonicToSeed, generateMnemonic } from 'bip39';

var network = new StacksTestnet();
network.coreApiUrl = 'https://sidecar.staging.blockstack.xyz';
network.coreApiUrl = 'https://stacks-node-api.blockstack.org';
return network;

@@ -1194,0 +1194,0 @@ };

@@ -1181,3 +1181,3 @@ (function (global, factory) {

var network = new stacksTransactions.StacksTestnet();
network.coreApiUrl = 'https://sidecar.staging.blockstack.xyz';
network.coreApiUrl = 'https://stacks-node-api.blockstack.org';
return network;

@@ -1184,0 +1184,0 @@ };

@@ -1,2 +0,2 @@

!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("bip39"),require("bitcoinjs-lib"),require("blockstack/lib/wallet"),require("blockstack/lib/keys"),require("blockstack/lib/encryption/sha2Hash"),require("zone-file"),require("blockstack/lib/auth/authMessages"),require("jsontokens"),require("blockstack/lib/encryption/cryptoRandom"),require("blockstack"),require("assert"),require("triplesec"),require("blockstack/lib/encryption/wallet"),require("@blockstack/stacks-transactions"),require("bn.js")):"function"==typeof define&&define.amd?define(["exports","bip39","bitcoinjs-lib","blockstack/lib/wallet","blockstack/lib/keys","blockstack/lib/encryption/sha2Hash","zone-file","blockstack/lib/auth/authMessages","jsontokens","blockstack/lib/encryption/cryptoRandom","blockstack","assert","triplesec","blockstack/lib/encryption/wallet","@blockstack/stacks-transactions","bn.js"],r):r((e=e||self)["@blockstack/keychain"]={},e.bip39,e.bitcoinjsLib,e.wallet,e.keys,e.sha2Hash,e.zoneFile,e.authMessages,e.jsontokens,e.cryptoRandom,e.blockstack,e.assert,e.triplesec,e.wallet$1,e.stacksTransactions,e.BN)}(this,(function(e,r,t,n,i,o,s,a,c,u,d,l,f,h,v,y){"use strict";function m(){return(m=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}y=y&&Object.prototype.hasOwnProperty.call(y,"default")?y.default:y;var p=function(){function e(){}return e.prototype.then=function(r,t){var n=new e,i=this.s;if(i){var o=1&i?r:t;if(o){try{g(n,1,o(this.v))}catch(e){g(n,2,e)}return n}return this}return this.o=function(e){try{var i=e.v;1&e.s?g(n,1,r?r(i):i):t?g(n,1,t(i)):g(n,2,i)}catch(e){g(n,2,e)}},n},e}();function g(e,r,t){if(!e.s){if(t instanceof p){if(!t.s)return void(t.o=g.bind(null,e,r));1&r&&(r=t.s),t=t.v}if(t&&t.then)return void t.then(g.bind(null,e,r),g.bind(null,e,2));e.s=r,e.v=t;var n=e.o;n&&n(e)}}function P(e){return e instanceof p&&1&e.s}function b(e,r){try{var t=e()}catch(e){return r(e)}return t&&t.then?t.then(void 0,r):t}"undefined"!=typeof Symbol&&(Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator"))),"undefined"!=typeof Symbol&&(Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator")));var k,K,S=function(){function e(e,r){this.hdNode=e,this.salt=r}var r=e.prototype;return r.getNode=function(){return this.hdNode},r.getSalt=function(){return this.salt},r.getIdentityKey=function(){if(!this.hdNode.privateKey)throw new Error("Node does not have private key");return this.hdNode.privateKey.toString("hex")},r.getIdentityKeyID=function(){return this.hdNode.publicKey.toString("hex")},r.getAppsNode=function(){return this.hdNode.deriveHardened(0)},r.getAddress=function(){return B(this.hdNode)},r.getEncryptionNode=function(){return this.hdNode.deriveHardened(2)},r.getSigningNode=function(){return this.hdNode.deriveHardened(1)},r.getSTXNode=function(){return this.hdNode.deriveHardened(6)},r.getAppNode=function(e){return n.getLegacyAppNode(this.hdNode,this.salt,e)},r.getAppPrivateKey=function(e){var r=this.getAppNode(e);if(!r.privateKey)throw new Error("App node does not have private key");return r.privateKey.toString("hex")},r.getAppAddress=function(e){var r=this.getAppNode(e);return i.publicKeyToAddress(r.publicKey)},e}(),A=function(e,r,t){try{return Promise.resolve(fetch(t.server+"/store/"+t.address+"/"+e,{method:"POST",headers:{"Content-Type":"application/json",Authorization:"bearer "+t.token},body:r,referrer:"no-referrer",referrerPolicy:"no-referrer"})).then((function(e){return Promise.resolve(e.json()).then((function(e){return e.publicURL}))}))}catch(e){return Promise.reject(e)}},C=function(e){var r=e.gaiaHubUrl,t=e.filePath,n=e.identity,i=e.signedProfileTokenData,o=e.gaiaHubConfig;try{var s=function(e){return Promise.resolve(A(t,i,e))};return Promise.resolve(o?s(o):Promise.resolve(d.connectToGaiaHub(r,n.keyPair.key)).then(s))}catch(e){return Promise.reject(e)}},N={"@type":"Person","@context":"http://schema.org"};(K=e.Subdomains||(e.Subdomains={})).TEST="test-personal.id",K.BLOCKSTACK="id.blockstack";var w=((k={})[e.Subdomains.TEST]={registerUrl:"https://test-registrar.blockstack.org/register",apiUrl:"https://test-registrar.blockstack.org/v1/names"},k[e.Subdomains.BLOCKSTACK]={registerUrl:"https://registrar.blockstack.org/register",apiUrl:"https://registrar.blockstack.org/v1/names"},k);function T(e,r){var t=d.signProfileToken(e,r.key,{publicKey:r.keyID}),n=d.wrapProfileToken(t);return JSON.stringify([n],null,2)}var j=function(e){var r=e.profile,t=e.gaiaHubUrl,n=e.filePath,i=e.identity,o=e.gaiaHubConfig;try{var s=T(r,i.keyPair);return Promise.resolve(C({gaiaHubUrl:t,filePath:n,identity:i,signedProfileTokenData:s,gaiaHubConfig:o})).then((function(){}))}catch(e){return Promise.reject(e)}},U=function(e){var r=e.profileUrl;return Promise.resolve(b((function(){return Promise.resolve(fetch(r)).then((function(e){var r=!1;function t(t){if(r)return t;if(404===e.status)return null;throw new Error("Network error when fetching profile")}var n=function(){if(e.ok)return Promise.resolve(e.json()).then((function(e){var t;return r=!0,null===(t=e[0].decodedToken.payload)||void 0===t?void 0:t.claim}))}();return n&&n.then?n.then(t):t(n)}))}),(function(){return null})))},I=function(){function e(e){var r=e.address,t=e.usernames,n=e.defaultUsername,i=e.profile;this.keyPair=e.keyPair,this.address=r,this.usernames=t||[],this.defaultUsername=n,this.profile=i}var r=e.prototype;return r.makeAuthResponse=function(e){var r=e.appDomain,n=e.gaiaUrl,o=e.transitPublicKey,s=e.scopes,l=void 0===s?[]:s,f=e.stxAddress;try{var h=this,v=h.appPrivateKey(r);return Promise.resolve(function(e){try{return Promise.resolve(fetch(e+"/hub_info")).then((function(e){return Promise.resolve(e.json())}))}catch(e){return Promise.reject(e)}}(n)).then((function(e){return Promise.resolve(h.profileUrl(e.read_url_prefix)).then((function(s){return Promise.resolve(U({profileUrl:s})).then((function(y){function p(){h.profile=y;var e,r,t,d,l,p=(e=i.getPublicKeyFromPrivate(v.slice(0,64)),r=h.keyPair.key.slice(0,64),t=i.getPublicKeyFromPrivate(r),d=u.randomBytes(16).toString("hex"),l={childToAssociate:e,iss:t,exp:31536e3+(new Date).getTime()/1e3,iat:Date.now()/1e3,salt:d},new c.TokenSigner("ES256K",r).sign(l));return a.makeAuthResponse(h.keyPair.key,m({},h.profile||{},{stxAddress:f}),h.defaultUsername||"",{profileUrl:s},void 0,v,void 0,o,n,void 0,p)}var g=function(){if(l.includes("publish_data")){y.apps||(y.apps={});var o=t.ECPair.fromPrivateKey(Buffer.from(v,"hex")),a=d.ecPairToAddress(o),f=""+e.read_url_prefix+a+"/";y.apps[r]=f,y.appsMeta||(y.appsMeta={}),y.appsMeta[r]={storage:f,publicKey:o.publicKey.toString("hex")};var m=function(e){var r=e.hubInfo,t=e.privateKey,n=e.gaiaHubUrl,o=r.read_url_prefix,s=function(e){var r=e.privateKey,t=e.hubInfo.challenge_text,n={gaiaHubUrl:e.gaiaHubUrl,iss:i.getPublicKeyFromPrivate(r),salt:u.randomBytes(16).toString("hex")};return t&&(n.gaiaChallenge=t),"v1:"+new c.TokenSigner("ES256K",r).sign(n)}({hubInfo:r,privateKey:t,gaiaHubUrl:n});return{url_prefix:o,max_file_upload_size_megabytes:100,address:d.ecPairToAddress(d.hexStringToECPair(t+(64===t.length?"01":""))),token:s,server:n}}({hubInfo:e,privateKey:h.keyPair.key,gaiaHubUrl:n}),p=h.gaiaReadUrlToWritePath(s,e.read_url_prefix,a);return Promise.resolve(j({profile:y,identity:h,gaiaHubUrl:n,filePath:p,gaiaHubConfig:m})).then((function(){}))}}();return g&&g.then?g.then(p):p()}))}))}))}catch(e){return Promise.reject(e)}},r.appPrivateKey=function(e){var r=this.keyPair,n=r.salt;return new S(t.bip32.fromBase58(r.appsNodeKey),n).getAppPrivateKey(e)},r.profileUrl=function(e){try{var r=!1,t=this,n=function(){if(t.defaultUsername)return b((function(){return Promise.resolve(V(t.defaultUsername)).then((function(e){if(e)return r=!0,e}))}),(function(e){console.warn("Error fetching profile URL from zone file:",e)}))}();return Promise.resolve(n&&n.then?n.then((function(n){return r?n:""+e+t.address+"/profile.json"})):r?n:""+e+t.address+"/profile.json")}catch(e){return Promise.reject(e)}},r.gaiaReadUrlToWritePath=function(e,r,t){return e.substr((""+r+t+"/").length)},r.fetchNames=function(){try{return Promise.resolve(fetch("https://core.blockstack.org/v1/addresses/bitcoin/"+this.address)).then((function(e){return Promise.resolve(e.json()).then((function(e){return e.names}))}))}catch(e){return Promise.reject(e)}},r.refresh=function(e){var r=this;return void 0===e&&(e={gaiaUrl:"https://gaia.blockstack.org/hub/"}),Promise.resolve(b((function(){return Promise.resolve(r.fetchNames()).then((function(t){return t&&(t[0]&&!r.defaultUsername&&(r.defaultUsername=t[0]),t.forEach((function(e){-1===r.usernames.findIndex((function(r){return r===e}))&&r.usernames.push(e)}))),Promise.resolve(r.profileUrl(e.gaiaUrl)).then((function(e){return Promise.resolve(U({profileUrl:e})).then((function(e){e&&(r.profile=e)}))}))}))}),(function(){})))},e}(),x=function(e,r){try{var t=function(){return{identityPublicKeychain:o,bitcoinPublicKeychain:a,firstBitcoinAddress:c,identityAddresses:u,identityKeypairs:d,identities:l}},n=_(e),i=H(e),o=n.neutered().toBase58(),s=i.neutered(),a=s.toBase58(),c=B(M(s)),u=[],d=[],l=[],f=0,h=function(e,r,t){for(var n;;){var i=e();if(P(i)&&(i=i.v),!i)return o;if(i.then){n=0;break}var o=t();if(o&&o.then){if(!P(o)){n=1;break}o=o.s}if(r){var s=r();if(s&&s.then&&!P(s)){n=2;break}}}var a=new p,c=g.bind(null,a,2);return(0===n?i.then(d):1===n?o.then(u):s.then(l)).then(void 0,c),a;function u(n){o=n;do{if(r&&(s=r())&&s.then&&!P(s))return void s.then(l).then(void 0,c);if(!(i=e())||P(i)&&!i.v)return void g(a,1,o);if(i.then)return void i.then(d).then(void 0,c);P(o=t())&&(o=o.v)}while(!o||!o.then);o.then(u).then(void 0,c)}function d(e){e?(o=t())&&o.then?o.then(u).then(void 0,c):u(o):g(a,1,o)}function l(){(i=e())?i.then?i.then(d).then(void 0,c):d(i):g(a,1,o)}}((function(){return f<r}),(function(){return f++}),(function(){return Promise.resolve(F(e,f)).then((function(e){l.push(e),d.push(e.keyPair),u.push(e.address)}))}));return Promise.resolve(h&&h.then?h.then(t):t())}catch(e){return Promise.reject(e)}},E=function(e,r){void 0===r&&(r=0);try{if(e.isNeutered())throw new Error("You need the private key to generate identity addresses");var t=Buffer.from(e.publicKey.toString("hex"));return Promise.resolve(o.createSha2Hash()).then((function(n){return Promise.resolve(n.digest(t,"sha256")).then((function(t){var n=t.toString("hex");return new S(e.deriveHardened(r),n)}))}))}catch(e){return Promise.reject(e)}};function _(e){return e.deriveHardened(888).deriveHardened(0)}function H(e){return e.deriveHardened(44).deriveHardened(0).deriveHardened(0)}function M(e,r,t){void 0===r&&(r=0),void 0===t&&(t="EXTERNAL_ADDRESS");var n=null;if("EXTERNAL_ADDRESS"===t)n=0;else{if("CHANGE_ADDRESS"!==t)throw new Error("Invalid chain type");n=1}return e.derive(n).derive(r)}function B(e){return i.publicKeyToAddress(e.publicKey)}function L(e){var r=e.getAddress(),t=e.getIdentityKey(),n=e.getIdentityKeyID(),i=e.getAppsNode(),o=e.getSTXNode();return{key:t,keyID:n,address:r,appsNodeKey:i.toBase58(),stxNodeKey:o.toBase58(),salt:e.getSalt()}}var R,F=function(e,r){try{var t=_(e);return Promise.resolve(E(t,r)).then((function(e){var r=L(e);return new I({keyPair:r,address:r.address,usernames:[]})}))}catch(e){return Promise.reject(e)}};function D(e){if(!e)throw new l.AssertionError({expected:!0,actual:e})}(R=e.IdentityNameValidityError||(e.IdentityNameValidityError={})).MINIMUM_LENGTH="error_minimum_length",R.MAXIMUM_LENGTH="error_maximum_length",R.ILLEGAL_CHARACTER="error_illegal_character",R.UNAVAILABLE="error_name_unavailable";var O,q=function(r){var t=r.length;return t<8?e.IdentityNameValidityError.MINIMUM_LENGTH:t>37?e.IdentityNameValidityError.MAXIMUM_LENGTH:/^[a-z0-9_]+$/.test(r)?null:e.IdentityNameValidityError.ILLEGAL_CHARACTER},X=function(r,t){try{void 0===t&&(t=e.Subdomains.BLOCKSTACK);var n=w[t].apiUrl+"/"+r.toLowerCase()+"."+t;return Promise.resolve(fetch(n)).then((function(e){return Promise.resolve(e.json())}))}catch(e){return Promise.reject(e)}},z=function e(r){var t=r.rootNode,n=r.index,i=void 0===n?1:n,o=r.identities,s=void 0===o?[]:o;try{return Promise.resolve(F(t,i)).then((function(r){return Promise.resolve(r.refresh()).then((function(){return r.defaultUsername?(s.push(r),e({rootNode:t,index:i+1,identities:s})):s}))}))}catch(e){return Promise.reject(e)}},V=function(e){try{return Promise.resolve(fetch("https://core.blockstack.org/v1/names/"+e)).then((function(e){return function(){if(e.ok)return Promise.resolve(e.json()).then((function(e){return s.parseZoneFile(e.zonefile).uri[0].target}))}()}))}catch(e){return Promise.reject(e)}},G=function(e,r){try{return Promise.resolve(h.decryptMnemonic(e,r,f.decrypt))}catch(e){return Promise.reject(e)}},W=function(e,r){try{return Promise.resolve(h.encryptMnemonic(e,r))}catch(e){return Promise.reject(e)}},J=function(e,r){try{return Promise.resolve(d.encryptMnemonic(e,r)).then((function(e){var r=e.toString("hex");return{encryptedMnemonic:e,encryptedMnemonicHex:r}}))}catch(e){return Promise.reject(e)}},Z=function(e){try{return Promise.resolve(r.mnemonicToSeed(e)).then((function(e){return t.bip32.fromSeed(e)}))}catch(e){return Promise.reject(e)}},$=function(e,n){try{var i=r.generateMnemonic(n,u.randomBytes);return Promise.resolve(r.mnemonicToSeed(i)).then((function(r){var n=t.bip32.fromSeed(r);return Promise.resolve(W(i,e)).then((function(e){var r=e.toString("hex");return{rootNode:n,encryptedMnemonicPhrase:r}}))}))}catch(e){return Promise.reject(e)}},Y=((O={})[v.ChainID.Mainnet]="m/44'/5757'/0'/0/0",O[v.ChainID.Testnet]="m/44'/5757'/0'/0/0",O);function Q(e){return Y[e]}function ee(e){return function(r){var n=r.derivePath(Q(e));if(!n.privateKey)throw new Error("Unable to derive private key from `rootNode`, bip32 master keychain");var i=t.ECPair.fromPrivateKey(n.privateKey),o=d.ecPairToHexString(i);return{childKey:n,address:v.getAddressFromPrivateKey(o,e===v.ChainID.Mainnet?v.TransactionVersion.Mainnet:v.TransactionVersion.Testnet),privateKey:o}}}var re=function(){function e(e){this.privateKey=e.privateKey}var r=e.prototype;return r.getSTXAddress=function(e){return v.getAddressFromPrivateKey(this.getSTXPrivateKey(),e)},r.getSTXPrivateKey=function(){var e=t.bip32.fromBase58(this.privateKey);return D(e.privateKey),e.privateKey},r.getNetwork=function(){var e=new v.StacksTestnet;return e.coreApiUrl="https://sidecar.staging.blockstack.xyz",e},r.fetchAccount=function(e){var r=e.version,t=e.rpcClient;try{var n=this.getSTXAddress(r);return Promise.resolve(t.fetchAccount(n))}catch(e){return Promise.reject(e)}},r.signContractCall=function(e){var r=e.contractName,t=e.contractAddress,n=e.functionName,i=e.functionArgs,o=e.nonce,s=e.postConditionMode,a=e.postConditions;try{return Promise.resolve(v.makeContractCall({contractAddress:t,contractName:r,functionName:n,functionArgs:i,senderKey:this.getSTXPrivateKey().toString("hex"),nonce:new y(o),network:this.getNetwork(),postConditionMode:s,postConditions:a}))}catch(e){return Promise.reject(e)}},r.signContractDeploy=function(e){var r=e.contractName,t=e.codeBody,n=e.nonce,i=e.postConditionMode,o=e.postConditions;try{return Promise.resolve(v.makeContractDeploy({contractName:r,codeBody:t,senderKey:this.getSTXPrivateKey().toString("hex"),network:this.getNetwork(),nonce:new y(n),postConditionMode:i,postConditions:o}))}catch(e){return Promise.reject(e)}},r.signSTXTransfer=function(e){var r=e.recipient,t=e.amount,n=e.memo,i=e.nonce,o=e.postConditionMode,s=e.postConditions;try{return Promise.resolve(v.makeSTXTokenTransfer({recipient:r,amount:new y(t),memo:n,senderKey:this.getSTXPrivateKey().toString("hex"),network:this.getNetwork(),nonce:new y(i),postConditionMode:o,postConditions:s}))}catch(e){return Promise.reject(e)}},e}(),te=function(){function e(e){var r=e.encryptedBackupPhrase,t=e.identityPublicKeychain,n=e.bitcoinPublicKeychain,i=e.firstBitcoinAddress,o=e.identityKeypairs,s=e.identityAddresses,a=e.identities,c=e.configPrivateKey,u=e.stacksPrivateKey,d=e.walletConfig;this.chain=e.chain,this.encryptedBackupPhrase=r,this.identityPublicKeychain=t,this.bitcoinPublicKeychain=n,this.firstBitcoinAddress=i,this.identityKeypairs=o,this.identityAddresses=s,this.identities=a.map((function(e){return new I(e)})),this.configPrivateKey=c,this.stacksPrivateKey=u,this.walletConfig=d}e.generateFactory=function(e){var r=this;return function(t,n){try{return Promise.resolve($(t,e)).then((function(e){return r.createAccount({encryptedBackupPhrase:e.encryptedMnemonicPhrase,rootNode:e.rootNode,chain:n})}))}catch(e){return Promise.reject(e)}}},e.generate=function(e,r){try{return Promise.resolve(this.generateFactory(128)(e,r))}catch(e){return Promise.reject(e)}},e.generateStrong=function(e,r){try{return Promise.resolve(this.generateFactory(256)(e,r))}catch(e){return Promise.reject(e)}},e.restore=function(r,t,n){try{return Promise.resolve(Z(t)).then((function(i){return Promise.resolve(J(t,r)).then((function(r){return Promise.resolve(e.createAccount({encryptedBackupPhrase:r.encryptedMnemonicHex,rootNode:i,chain:n})).then((function(e){return Promise.resolve(e.restoreIdentities({rootNode:i,gaiaReadURL:"https://gaia.blockstack.org/hub/"}))}))}))}))}catch(e){return Promise.reject(e)}},e.createAccount=function(r){var t=r.encryptedBackupPhrase,n=r.rootNode,i=r.chain,o=r.identitiesToGenerate,s=void 0===o?1:o;try{var a=n.deriveHardened(45).privateKey;if(!a)throw new TypeError("Unable to derive config key for wallet identities");var c=a.toString("hex"),u=ee(i)(n).childKey;return Promise.resolve(x(n,s)).then((function(r){return new e(m({},r,{chain:i,configPrivateKey:c,stacksPrivateKey:u.toBase58(),encryptedBackupPhrase:t}))}))}catch(e){return Promise.reject(e)}};var n=e.prototype;return n.restoreIdentities=function(e){var r,t,n=e.rootNode,i=e.gaiaReadURL;try{var o=this,s={url_prefix:(r={readURL:i,privateKey:o.configPrivateKey}).readURL,max_file_upload_size_megabytes:100,address:d.ecPairToAddress(d.hexStringToECPair((t=r.privateKey)+(64===t.length?"01":""))),token:"not_used",server:"not_used"};return Promise.resolve(o.fetchConfig(s)).then((function(){var e=!1;function r(r){return e?r:Promise.resolve(o.identities[0].refresh()).then((function(){return Promise.resolve(z({rootNode:n})).then((function(e){return o.identities=o.identities.concat(e),o}))}))}var t=function(){if(o.walletConfig){var r=o.walletConfig.identities.map((function(e,r){try{var t=function(){return e.username&&(i.usernames=[e.username],i.defaultUsername=e.username),i},i=o.identities[r],s=function(){if(!i)return Promise.resolve(F(n,r)).then((function(e){i=e}))}();return Promise.resolve(s&&s.then?s.then(t):t())}catch(e){return Promise.reject(e)}}));return Promise.resolve(Promise.all(r)).then((function(r){return o.identities=r,e=!0,o}))}}();return t&&t.then?t.then(r):r(t)}))}catch(e){return Promise.reject(e)}},n.createNewIdentity=function(e){try{var n=this;return Promise.resolve(G(Buffer.from(n.encryptedBackupPhrase,"hex"),e)).then((function(e){return Promise.resolve(r.mnemonicToSeed(e)).then((function(e){var r=t.bip32.fromSeed(e);return Promise.resolve(F(r,n.identities.length)).then((function(e){return n.identities.push(e),n.identityKeypairs.push(e.keyPair),n.identityAddresses.push(e.address),e}))}))}))}catch(e){return Promise.reject(e)}},n.createGaiaConfig=function(e){try{return Promise.resolve(d.connectToGaiaHub(e,this.configPrivateKey))}catch(e){return Promise.reject(e)}},n.fetchConfig=function(e){var r=this;return Promise.resolve(b((function(){return Promise.resolve(fetch(""+e.url_prefix+e.address+"/wallet-config.json")).then((function(e){return Promise.resolve(e.text()).then((function(e){return Promise.resolve(d.decryptContent(e,{privateKey:r.configPrivateKey})).then((function(e){var t=JSON.parse(e);return r.walletConfig=t,t}))}))}))}),(function(){return null})))},n.getOrCreateConfig=function(e){var r=e.gaiaConfig,t=e.skipUpload;try{var n=this;return n.walletConfig?Promise.resolve(n.walletConfig):Promise.resolve(n.fetchConfig(r)).then((function(e){if(e)return e;var i={identities:n.identities.map((function(e){return{username:e.defaultUsername,address:e.address,apps:{}}}))};n.walletConfig=i;var o=function(){if(!t)return Promise.resolve(n.updateConfig(r)).then((function(){}))}();return o&&o.then?o.then((function(){return i})):i}))}catch(e){return Promise.reject(e)}},n.updateConfig=function(e){try{var r=d.getPublicKeyFromPrivate(this.configPrivateKey);return Promise.resolve(d.encryptContent(JSON.stringify(this.walletConfig),{publicKey:r})).then((function(r){return Promise.resolve(A("wallet-config.json",r,e)).then((function(){}))}))}catch(e){return Promise.reject(e)}},n.updateConfigWithAuth=function(e){var r=e.identityIndex,t=e.app,n=e.gaiaConfig;try{var i=this,o=i.walletConfig;D(o),i.identities.forEach((function(e,r){var t,n=o.identities[r];n?(n.apps=n.apps||{},n.username=e.defaultUsername,n.address=e.address,o.identities[r]=n):null===(t=i.walletConfig)||void 0===t||t.identities.push({username:e.defaultUsername,address:e.address,apps:{}})}));var s=o.identities[r];return s.apps=s.apps||{},s.apps[t.origin]=t,o.identities[r]=s,i.walletConfig=o,Promise.resolve(i.updateConfig(n)).then((function(){}))}catch(e){return Promise.reject(e)}},n.updateConfigForReuseWarning=function(e){var r=e.gaiaConfig;try{return D(this.walletConfig),this.walletConfig.hideWarningForReusingIdentity=!0,Promise.resolve(this.updateConfig(r)).then((function(){}))}catch(e){return Promise.reject(e)}},n.getSigner=function(){return new re({privateKey:this.stacksPrivateKey})},e}(),ne={Wallet:te};e.DEFAULT_PROFILE=N,e.Identity=I,e.Wallet=te,e.WalletSigner=re,e.assertIsTruthy=D,e.decrypt=G,e.default=ne,e.derivationPaths=Y,e.deriveIdentityKeyPair=L,e.deriveRootKeychainFromMnemonic=Z,e.deriveStxAddressChain=ee,e.encrypt=W,e.encryptMnemonicFormatted=J,e.fetchProfile=U,e.generateEncryptedMnemonicRootKeychain=$,e.generateMnemonicRootKeychain=function(e){try{var n=r.generateMnemonic(e,u.randomBytes);return Promise.resolve(r.mnemonicToSeed(n)).then((function(e){return{rootNode:t.bip32.fromSeed(e),plaintextMnemonic:n}}))}catch(e){return Promise.reject(e)}},e.getAddress=B,e.getBitcoinAddressNode=M,e.getBitcoinPrivateKeychain=H,e.getBlockchainIdentities=x,e.getDerivationPath=Q,e.getIdentityOwnerAddressNode=E,e.getIdentityPrivateKeychain=_,e.getProfileURLFromZoneFile=V,e.makeIdentity=F,e.recursiveRestoreIdentities=z,e.registerSubdomain=function(e){var r=e.identity,t=e.gaiaHubUrl,n=e.username,i=e.subdomain;try{var o=T(r.profile||N,r.keyPair);return Promise.resolve(C({gaiaHubUrl:t,filePath:"profile.json",identity:r,signedProfileTokenData:o})).then((function(e){var t=n+"."+i,o=d.makeProfileZoneFile(t,e);return Promise.resolve(function(e){var r=e.username,t=e.subdomain,n=e.zoneFile,i=e.identity;try{var o=w[t].registerUrl,s=JSON.stringify({name:r,owner_address:i.address,zonefile:n});return Promise.resolve(fetch(o,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json"},body:s})).then((function(e){return e.ok?e.json():Promise.reject({error:"Failed to register username",status:e.status})}))}catch(e){return Promise.reject(e)}}({username:n,subdomain:i,zoneFile:o,identity:r})).then((function(){return r.defaultUsername=t,r.usernames.push(t),r}))}))}catch(e){return Promise.reject(e)}},e.registrars=w,e.signAndUploadProfile=j,e.signProfileForUpload=T,e.uploadProfile=C,e.validateSubdomain=function(r,t){try{var n=!1;void 0===t&&(t=e.Subdomains.BLOCKSTACK);var i=q(r);if(i)return Promise.resolve(i);var o=b((function(){return Promise.resolve(X(r,t)).then((function(r){if("available"!==r.status)return n=!0,e.IdentityNameValidityError.UNAVAILABLE}))}),(function(){return n=!0,e.IdentityNameValidityError.UNAVAILABLE}));return Promise.resolve(o&&o.then?o.then((function(e){return n?e:null})):n?o:null)}catch(e){return Promise.reject(e)}},e.validateSubdomainAvailability=X,e.validateSubdomainFormat=q}));
!function(e,r){"object"==typeof exports&&"undefined"!=typeof module?r(exports,require("bip39"),require("bitcoinjs-lib"),require("blockstack/lib/wallet"),require("blockstack/lib/keys"),require("blockstack/lib/encryption/sha2Hash"),require("zone-file"),require("blockstack/lib/auth/authMessages"),require("jsontokens"),require("blockstack/lib/encryption/cryptoRandom"),require("blockstack"),require("assert"),require("triplesec"),require("blockstack/lib/encryption/wallet"),require("@blockstack/stacks-transactions"),require("bn.js")):"function"==typeof define&&define.amd?define(["exports","bip39","bitcoinjs-lib","blockstack/lib/wallet","blockstack/lib/keys","blockstack/lib/encryption/sha2Hash","zone-file","blockstack/lib/auth/authMessages","jsontokens","blockstack/lib/encryption/cryptoRandom","blockstack","assert","triplesec","blockstack/lib/encryption/wallet","@blockstack/stacks-transactions","bn.js"],r):r((e=e||self)["@blockstack/keychain"]={},e.bip39,e.bitcoinjsLib,e.wallet,e.keys,e.sha2Hash,e.zoneFile,e.authMessages,e.jsontokens,e.cryptoRandom,e.blockstack,e.assert,e.triplesec,e.wallet$1,e.stacksTransactions,e.BN)}(this,(function(e,r,t,n,i,o,s,a,c,u,d,l,f,h,v,y){"use strict";function m(){return(m=Object.assign||function(e){for(var r=1;r<arguments.length;r++){var t=arguments[r];for(var n in t)Object.prototype.hasOwnProperty.call(t,n)&&(e[n]=t[n])}return e}).apply(this,arguments)}y=y&&Object.prototype.hasOwnProperty.call(y,"default")?y.default:y;var p=function(){function e(){}return e.prototype.then=function(r,t){var n=new e,i=this.s;if(i){var o=1&i?r:t;if(o){try{P(n,1,o(this.v))}catch(e){P(n,2,e)}return n}return this}return this.o=function(e){try{var i=e.v;1&e.s?P(n,1,r?r(i):i):t?P(n,1,t(i)):P(n,2,i)}catch(e){P(n,2,e)}},n},e}();function P(e,r,t){if(!e.s){if(t instanceof p){if(!t.s)return void(t.o=P.bind(null,e,r));1&r&&(r=t.s),t=t.v}if(t&&t.then)return void t.then(P.bind(null,e,r),P.bind(null,e,2));e.s=r,e.v=t;var n=e.o;n&&n(e)}}function g(e){return e instanceof p&&1&e.s}function b(e,r){try{var t=e()}catch(e){return r(e)}return t&&t.then?t.then(void 0,r):t}"undefined"!=typeof Symbol&&(Symbol.iterator||(Symbol.iterator=Symbol("Symbol.iterator"))),"undefined"!=typeof Symbol&&(Symbol.asyncIterator||(Symbol.asyncIterator=Symbol("Symbol.asyncIterator")));var k,K,S=function(){function e(e,r){this.hdNode=e,this.salt=r}var r=e.prototype;return r.getNode=function(){return this.hdNode},r.getSalt=function(){return this.salt},r.getIdentityKey=function(){if(!this.hdNode.privateKey)throw new Error("Node does not have private key");return this.hdNode.privateKey.toString("hex")},r.getIdentityKeyID=function(){return this.hdNode.publicKey.toString("hex")},r.getAppsNode=function(){return this.hdNode.deriveHardened(0)},r.getAddress=function(){return B(this.hdNode)},r.getEncryptionNode=function(){return this.hdNode.deriveHardened(2)},r.getSigningNode=function(){return this.hdNode.deriveHardened(1)},r.getSTXNode=function(){return this.hdNode.deriveHardened(6)},r.getAppNode=function(e){return n.getLegacyAppNode(this.hdNode,this.salt,e)},r.getAppPrivateKey=function(e){var r=this.getAppNode(e);if(!r.privateKey)throw new Error("App node does not have private key");return r.privateKey.toString("hex")},r.getAppAddress=function(e){var r=this.getAppNode(e);return i.publicKeyToAddress(r.publicKey)},e}(),A=function(e,r,t){try{return Promise.resolve(fetch(t.server+"/store/"+t.address+"/"+e,{method:"POST",headers:{"Content-Type":"application/json",Authorization:"bearer "+t.token},body:r,referrer:"no-referrer",referrerPolicy:"no-referrer"})).then((function(e){return Promise.resolve(e.json()).then((function(e){return e.publicURL}))}))}catch(e){return Promise.reject(e)}},C=function(e){var r=e.gaiaHubUrl,t=e.filePath,n=e.identity,i=e.signedProfileTokenData,o=e.gaiaHubConfig;try{var s=function(e){return Promise.resolve(A(t,i,e))};return Promise.resolve(o?s(o):Promise.resolve(d.connectToGaiaHub(r,n.keyPair.key)).then(s))}catch(e){return Promise.reject(e)}},N={"@type":"Person","@context":"http://schema.org"};(K=e.Subdomains||(e.Subdomains={})).TEST="test-personal.id",K.BLOCKSTACK="id.blockstack";var w=((k={})[e.Subdomains.TEST]={registerUrl:"https://test-registrar.blockstack.org/register",apiUrl:"https://test-registrar.blockstack.org/v1/names"},k[e.Subdomains.BLOCKSTACK]={registerUrl:"https://registrar.blockstack.org/register",apiUrl:"https://registrar.blockstack.org/v1/names"},k);function T(e,r){var t=d.signProfileToken(e,r.key,{publicKey:r.keyID}),n=d.wrapProfileToken(t);return JSON.stringify([n],null,2)}var j=function(e){var r=e.profile,t=e.gaiaHubUrl,n=e.filePath,i=e.identity,o=e.gaiaHubConfig;try{var s=T(r,i.keyPair);return Promise.resolve(C({gaiaHubUrl:t,filePath:n,identity:i,signedProfileTokenData:s,gaiaHubConfig:o})).then((function(){}))}catch(e){return Promise.reject(e)}},U=function(e){var r=e.profileUrl;return Promise.resolve(b((function(){return Promise.resolve(fetch(r)).then((function(e){var r=!1;function t(t){if(r)return t;if(404===e.status)return null;throw new Error("Network error when fetching profile")}var n=function(){if(e.ok)return Promise.resolve(e.json()).then((function(e){var t;return r=!0,null===(t=e[0].decodedToken.payload)||void 0===t?void 0:t.claim}))}();return n&&n.then?n.then(t):t(n)}))}),(function(){return null})))},I=function(){function e(e){var r=e.address,t=e.usernames,n=e.defaultUsername,i=e.profile;this.keyPair=e.keyPair,this.address=r,this.usernames=t||[],this.defaultUsername=n,this.profile=i}var r=e.prototype;return r.makeAuthResponse=function(e){var r=e.appDomain,n=e.gaiaUrl,o=e.transitPublicKey,s=e.scopes,l=void 0===s?[]:s,f=e.stxAddress;try{var h=this,v=h.appPrivateKey(r);return Promise.resolve(function(e){try{return Promise.resolve(fetch(e+"/hub_info")).then((function(e){return Promise.resolve(e.json())}))}catch(e){return Promise.reject(e)}}(n)).then((function(e){return Promise.resolve(h.profileUrl(e.read_url_prefix)).then((function(s){return Promise.resolve(U({profileUrl:s})).then((function(y){function p(){h.profile=y;var e,r,t,d,l,p=(e=i.getPublicKeyFromPrivate(v.slice(0,64)),r=h.keyPair.key.slice(0,64),t=i.getPublicKeyFromPrivate(r),d=u.randomBytes(16).toString("hex"),l={childToAssociate:e,iss:t,exp:31536e3+(new Date).getTime()/1e3,iat:Date.now()/1e3,salt:d},new c.TokenSigner("ES256K",r).sign(l));return a.makeAuthResponse(h.keyPair.key,m({},h.profile||{},{stxAddress:f}),h.defaultUsername||"",{profileUrl:s},void 0,v,void 0,o,n,void 0,p)}var P=function(){if(l.includes("publish_data")){y.apps||(y.apps={});var o=t.ECPair.fromPrivateKey(Buffer.from(v,"hex")),a=d.ecPairToAddress(o),f=""+e.read_url_prefix+a+"/";y.apps[r]=f,y.appsMeta||(y.appsMeta={}),y.appsMeta[r]={storage:f,publicKey:o.publicKey.toString("hex")};var m=function(e){var r=e.hubInfo,t=e.privateKey,n=e.gaiaHubUrl,o=r.read_url_prefix,s=function(e){var r=e.privateKey,t=e.hubInfo.challenge_text,n={gaiaHubUrl:e.gaiaHubUrl,iss:i.getPublicKeyFromPrivate(r),salt:u.randomBytes(16).toString("hex")};return t&&(n.gaiaChallenge=t),"v1:"+new c.TokenSigner("ES256K",r).sign(n)}({hubInfo:r,privateKey:t,gaiaHubUrl:n});return{url_prefix:o,max_file_upload_size_megabytes:100,address:d.ecPairToAddress(d.hexStringToECPair(t+(64===t.length?"01":""))),token:s,server:n}}({hubInfo:e,privateKey:h.keyPair.key,gaiaHubUrl:n}),p=h.gaiaReadUrlToWritePath(s,e.read_url_prefix,a);return Promise.resolve(j({profile:y,identity:h,gaiaHubUrl:n,filePath:p,gaiaHubConfig:m})).then((function(){}))}}();return P&&P.then?P.then(p):p()}))}))}))}catch(e){return Promise.reject(e)}},r.appPrivateKey=function(e){var r=this.keyPair,n=r.salt;return new S(t.bip32.fromBase58(r.appsNodeKey),n).getAppPrivateKey(e)},r.profileUrl=function(e){try{var r=!1,t=this,n=function(){if(t.defaultUsername)return b((function(){return Promise.resolve(V(t.defaultUsername)).then((function(e){if(e)return r=!0,e}))}),(function(e){console.warn("Error fetching profile URL from zone file:",e)}))}();return Promise.resolve(n&&n.then?n.then((function(n){return r?n:""+e+t.address+"/profile.json"})):r?n:""+e+t.address+"/profile.json")}catch(e){return Promise.reject(e)}},r.gaiaReadUrlToWritePath=function(e,r,t){return e.substr((""+r+t+"/").length)},r.fetchNames=function(){try{return Promise.resolve(fetch("https://core.blockstack.org/v1/addresses/bitcoin/"+this.address)).then((function(e){return Promise.resolve(e.json()).then((function(e){return e.names}))}))}catch(e){return Promise.reject(e)}},r.refresh=function(e){var r=this;return void 0===e&&(e={gaiaUrl:"https://gaia.blockstack.org/hub/"}),Promise.resolve(b((function(){return Promise.resolve(r.fetchNames()).then((function(t){return t&&(t[0]&&!r.defaultUsername&&(r.defaultUsername=t[0]),t.forEach((function(e){-1===r.usernames.findIndex((function(r){return r===e}))&&r.usernames.push(e)}))),Promise.resolve(r.profileUrl(e.gaiaUrl)).then((function(e){return Promise.resolve(U({profileUrl:e})).then((function(e){e&&(r.profile=e)}))}))}))}),(function(){})))},e}(),E=function(e,r){try{var t=function(){return{identityPublicKeychain:o,bitcoinPublicKeychain:a,firstBitcoinAddress:c,identityAddresses:u,identityKeypairs:d,identities:l}},n=_(e),i=H(e),o=n.neutered().toBase58(),s=i.neutered(),a=s.toBase58(),c=B(M(s)),u=[],d=[],l=[],f=0,h=function(e,r,t){for(var n;;){var i=e();if(g(i)&&(i=i.v),!i)return o;if(i.then){n=0;break}var o=t();if(o&&o.then){if(!g(o)){n=1;break}o=o.s}if(r){var s=r();if(s&&s.then&&!g(s)){n=2;break}}}var a=new p,c=P.bind(null,a,2);return(0===n?i.then(d):1===n?o.then(u):s.then(l)).then(void 0,c),a;function u(n){o=n;do{if(r&&(s=r())&&s.then&&!g(s))return void s.then(l).then(void 0,c);if(!(i=e())||g(i)&&!i.v)return void P(a,1,o);if(i.then)return void i.then(d).then(void 0,c);g(o=t())&&(o=o.v)}while(!o||!o.then);o.then(u).then(void 0,c)}function d(e){e?(o=t())&&o.then?o.then(u).then(void 0,c):u(o):P(a,1,o)}function l(){(i=e())?i.then?i.then(d).then(void 0,c):d(i):P(a,1,o)}}((function(){return f<r}),(function(){return f++}),(function(){return Promise.resolve(F(e,f)).then((function(e){l.push(e),d.push(e.keyPair),u.push(e.address)}))}));return Promise.resolve(h&&h.then?h.then(t):t())}catch(e){return Promise.reject(e)}},x=function(e,r){void 0===r&&(r=0);try{if(e.isNeutered())throw new Error("You need the private key to generate identity addresses");var t=Buffer.from(e.publicKey.toString("hex"));return Promise.resolve(o.createSha2Hash()).then((function(n){return Promise.resolve(n.digest(t,"sha256")).then((function(t){var n=t.toString("hex");return new S(e.deriveHardened(r),n)}))}))}catch(e){return Promise.reject(e)}};function _(e){return e.deriveHardened(888).deriveHardened(0)}function H(e){return e.deriveHardened(44).deriveHardened(0).deriveHardened(0)}function M(e,r,t){void 0===r&&(r=0),void 0===t&&(t="EXTERNAL_ADDRESS");var n=null;if("EXTERNAL_ADDRESS"===t)n=0;else{if("CHANGE_ADDRESS"!==t)throw new Error("Invalid chain type");n=1}return e.derive(n).derive(r)}function B(e){return i.publicKeyToAddress(e.publicKey)}function L(e){var r=e.getAddress(),t=e.getIdentityKey(),n=e.getIdentityKeyID(),i=e.getAppsNode(),o=e.getSTXNode();return{key:t,keyID:n,address:r,appsNodeKey:i.toBase58(),stxNodeKey:o.toBase58(),salt:e.getSalt()}}var R,F=function(e,r){try{var t=_(e);return Promise.resolve(x(t,r)).then((function(e){var r=L(e);return new I({keyPair:r,address:r.address,usernames:[]})}))}catch(e){return Promise.reject(e)}};function D(e){if(!e)throw new l.AssertionError({expected:!0,actual:e})}(R=e.IdentityNameValidityError||(e.IdentityNameValidityError={})).MINIMUM_LENGTH="error_minimum_length",R.MAXIMUM_LENGTH="error_maximum_length",R.ILLEGAL_CHARACTER="error_illegal_character",R.UNAVAILABLE="error_name_unavailable";var O,q=function(r){var t=r.length;return t<8?e.IdentityNameValidityError.MINIMUM_LENGTH:t>37?e.IdentityNameValidityError.MAXIMUM_LENGTH:/^[a-z0-9_]+$/.test(r)?null:e.IdentityNameValidityError.ILLEGAL_CHARACTER},X=function(r,t){try{void 0===t&&(t=e.Subdomains.BLOCKSTACK);var n=w[t].apiUrl+"/"+r.toLowerCase()+"."+t;return Promise.resolve(fetch(n)).then((function(e){return Promise.resolve(e.json())}))}catch(e){return Promise.reject(e)}},z=function e(r){var t=r.rootNode,n=r.index,i=void 0===n?1:n,o=r.identities,s=void 0===o?[]:o;try{return Promise.resolve(F(t,i)).then((function(r){return Promise.resolve(r.refresh()).then((function(){return r.defaultUsername?(s.push(r),e({rootNode:t,index:i+1,identities:s})):s}))}))}catch(e){return Promise.reject(e)}},V=function(e){try{return Promise.resolve(fetch("https://core.blockstack.org/v1/names/"+e)).then((function(e){return function(){if(e.ok)return Promise.resolve(e.json()).then((function(e){return s.parseZoneFile(e.zonefile).uri[0].target}))}()}))}catch(e){return Promise.reject(e)}},G=function(e,r){try{return Promise.resolve(h.decryptMnemonic(e,r,f.decrypt))}catch(e){return Promise.reject(e)}},W=function(e,r){try{return Promise.resolve(h.encryptMnemonic(e,r))}catch(e){return Promise.reject(e)}},J=function(e,r){try{return Promise.resolve(d.encryptMnemonic(e,r)).then((function(e){var r=e.toString("hex");return{encryptedMnemonic:e,encryptedMnemonicHex:r}}))}catch(e){return Promise.reject(e)}},Z=function(e){try{return Promise.resolve(r.mnemonicToSeed(e)).then((function(e){return t.bip32.fromSeed(e)}))}catch(e){return Promise.reject(e)}},$=function(e,n){try{var i=r.generateMnemonic(n,u.randomBytes);return Promise.resolve(r.mnemonicToSeed(i)).then((function(r){var n=t.bip32.fromSeed(r);return Promise.resolve(W(i,e)).then((function(e){var r=e.toString("hex");return{rootNode:n,encryptedMnemonicPhrase:r}}))}))}catch(e){return Promise.reject(e)}},Y=((O={})[v.ChainID.Mainnet]="m/44'/5757'/0'/0/0",O[v.ChainID.Testnet]="m/44'/5757'/0'/0/0",O);function Q(e){return Y[e]}function ee(e){return function(r){var n=r.derivePath(Q(e));if(!n.privateKey)throw new Error("Unable to derive private key from `rootNode`, bip32 master keychain");var i=t.ECPair.fromPrivateKey(n.privateKey),o=d.ecPairToHexString(i);return{childKey:n,address:v.getAddressFromPrivateKey(o,e===v.ChainID.Mainnet?v.TransactionVersion.Mainnet:v.TransactionVersion.Testnet),privateKey:o}}}var re=function(){function e(e){this.privateKey=e.privateKey}var r=e.prototype;return r.getSTXAddress=function(e){return v.getAddressFromPrivateKey(this.getSTXPrivateKey(),e)},r.getSTXPrivateKey=function(){var e=t.bip32.fromBase58(this.privateKey);return D(e.privateKey),e.privateKey},r.getNetwork=function(){var e=new v.StacksTestnet;return e.coreApiUrl="https://stacks-node-api.blockstack.org",e},r.fetchAccount=function(e){var r=e.version,t=e.rpcClient;try{var n=this.getSTXAddress(r);return Promise.resolve(t.fetchAccount(n))}catch(e){return Promise.reject(e)}},r.signContractCall=function(e){var r=e.contractName,t=e.contractAddress,n=e.functionName,i=e.functionArgs,o=e.nonce,s=e.postConditionMode,a=e.postConditions;try{return Promise.resolve(v.makeContractCall({contractAddress:t,contractName:r,functionName:n,functionArgs:i,senderKey:this.getSTXPrivateKey().toString("hex"),nonce:new y(o),network:this.getNetwork(),postConditionMode:s,postConditions:a}))}catch(e){return Promise.reject(e)}},r.signContractDeploy=function(e){var r=e.contractName,t=e.codeBody,n=e.nonce,i=e.postConditionMode,o=e.postConditions;try{return Promise.resolve(v.makeContractDeploy({contractName:r,codeBody:t,senderKey:this.getSTXPrivateKey().toString("hex"),network:this.getNetwork(),nonce:new y(n),postConditionMode:i,postConditions:o}))}catch(e){return Promise.reject(e)}},r.signSTXTransfer=function(e){var r=e.recipient,t=e.amount,n=e.memo,i=e.nonce,o=e.postConditionMode,s=e.postConditions;try{return Promise.resolve(v.makeSTXTokenTransfer({recipient:r,amount:new y(t),memo:n,senderKey:this.getSTXPrivateKey().toString("hex"),network:this.getNetwork(),nonce:new y(i),postConditionMode:o,postConditions:s}))}catch(e){return Promise.reject(e)}},e}(),te=function(){function e(e){var r=e.encryptedBackupPhrase,t=e.identityPublicKeychain,n=e.bitcoinPublicKeychain,i=e.firstBitcoinAddress,o=e.identityKeypairs,s=e.identityAddresses,a=e.identities,c=e.configPrivateKey,u=e.stacksPrivateKey,d=e.walletConfig;this.chain=e.chain,this.encryptedBackupPhrase=r,this.identityPublicKeychain=t,this.bitcoinPublicKeychain=n,this.firstBitcoinAddress=i,this.identityKeypairs=o,this.identityAddresses=s,this.identities=a.map((function(e){return new I(e)})),this.configPrivateKey=c,this.stacksPrivateKey=u,this.walletConfig=d}e.generateFactory=function(e){var r=this;return function(t,n){try{return Promise.resolve($(t,e)).then((function(e){return r.createAccount({encryptedBackupPhrase:e.encryptedMnemonicPhrase,rootNode:e.rootNode,chain:n})}))}catch(e){return Promise.reject(e)}}},e.generate=function(e,r){try{return Promise.resolve(this.generateFactory(128)(e,r))}catch(e){return Promise.reject(e)}},e.generateStrong=function(e,r){try{return Promise.resolve(this.generateFactory(256)(e,r))}catch(e){return Promise.reject(e)}},e.restore=function(r,t,n){try{return Promise.resolve(Z(t)).then((function(i){return Promise.resolve(J(t,r)).then((function(r){return Promise.resolve(e.createAccount({encryptedBackupPhrase:r.encryptedMnemonicHex,rootNode:i,chain:n})).then((function(e){return Promise.resolve(e.restoreIdentities({rootNode:i,gaiaReadURL:"https://gaia.blockstack.org/hub/"}))}))}))}))}catch(e){return Promise.reject(e)}},e.createAccount=function(r){var t=r.encryptedBackupPhrase,n=r.rootNode,i=r.chain,o=r.identitiesToGenerate,s=void 0===o?1:o;try{var a=n.deriveHardened(45).privateKey;if(!a)throw new TypeError("Unable to derive config key for wallet identities");var c=a.toString("hex"),u=ee(i)(n).childKey;return Promise.resolve(E(n,s)).then((function(r){return new e(m({},r,{chain:i,configPrivateKey:c,stacksPrivateKey:u.toBase58(),encryptedBackupPhrase:t}))}))}catch(e){return Promise.reject(e)}};var n=e.prototype;return n.restoreIdentities=function(e){var r,t,n=e.rootNode,i=e.gaiaReadURL;try{var o=this,s={url_prefix:(r={readURL:i,privateKey:o.configPrivateKey}).readURL,max_file_upload_size_megabytes:100,address:d.ecPairToAddress(d.hexStringToECPair((t=r.privateKey)+(64===t.length?"01":""))),token:"not_used",server:"not_used"};return Promise.resolve(o.fetchConfig(s)).then((function(){var e=!1;function r(r){return e?r:Promise.resolve(o.identities[0].refresh()).then((function(){return Promise.resolve(z({rootNode:n})).then((function(e){return o.identities=o.identities.concat(e),o}))}))}var t=function(){if(o.walletConfig){var r=o.walletConfig.identities.map((function(e,r){try{var t=function(){return e.username&&(i.usernames=[e.username],i.defaultUsername=e.username),i},i=o.identities[r],s=function(){if(!i)return Promise.resolve(F(n,r)).then((function(e){i=e}))}();return Promise.resolve(s&&s.then?s.then(t):t())}catch(e){return Promise.reject(e)}}));return Promise.resolve(Promise.all(r)).then((function(r){return o.identities=r,e=!0,o}))}}();return t&&t.then?t.then(r):r(t)}))}catch(e){return Promise.reject(e)}},n.createNewIdentity=function(e){try{var n=this;return Promise.resolve(G(Buffer.from(n.encryptedBackupPhrase,"hex"),e)).then((function(e){return Promise.resolve(r.mnemonicToSeed(e)).then((function(e){var r=t.bip32.fromSeed(e);return Promise.resolve(F(r,n.identities.length)).then((function(e){return n.identities.push(e),n.identityKeypairs.push(e.keyPair),n.identityAddresses.push(e.address),e}))}))}))}catch(e){return Promise.reject(e)}},n.createGaiaConfig=function(e){try{return Promise.resolve(d.connectToGaiaHub(e,this.configPrivateKey))}catch(e){return Promise.reject(e)}},n.fetchConfig=function(e){var r=this;return Promise.resolve(b((function(){return Promise.resolve(fetch(""+e.url_prefix+e.address+"/wallet-config.json")).then((function(e){return Promise.resolve(e.text()).then((function(e){return Promise.resolve(d.decryptContent(e,{privateKey:r.configPrivateKey})).then((function(e){var t=JSON.parse(e);return r.walletConfig=t,t}))}))}))}),(function(){return null})))},n.getOrCreateConfig=function(e){var r=e.gaiaConfig,t=e.skipUpload;try{var n=this;return n.walletConfig?Promise.resolve(n.walletConfig):Promise.resolve(n.fetchConfig(r)).then((function(e){if(e)return e;var i={identities:n.identities.map((function(e){return{username:e.defaultUsername,address:e.address,apps:{}}}))};n.walletConfig=i;var o=function(){if(!t)return Promise.resolve(n.updateConfig(r)).then((function(){}))}();return o&&o.then?o.then((function(){return i})):i}))}catch(e){return Promise.reject(e)}},n.updateConfig=function(e){try{var r=d.getPublicKeyFromPrivate(this.configPrivateKey);return Promise.resolve(d.encryptContent(JSON.stringify(this.walletConfig),{publicKey:r})).then((function(r){return Promise.resolve(A("wallet-config.json",r,e)).then((function(){}))}))}catch(e){return Promise.reject(e)}},n.updateConfigWithAuth=function(e){var r=e.identityIndex,t=e.app,n=e.gaiaConfig;try{var i=this,o=i.walletConfig;D(o),i.identities.forEach((function(e,r){var t,n=o.identities[r];n?(n.apps=n.apps||{},n.username=e.defaultUsername,n.address=e.address,o.identities[r]=n):null===(t=i.walletConfig)||void 0===t||t.identities.push({username:e.defaultUsername,address:e.address,apps:{}})}));var s=o.identities[r];return s.apps=s.apps||{},s.apps[t.origin]=t,o.identities[r]=s,i.walletConfig=o,Promise.resolve(i.updateConfig(n)).then((function(){}))}catch(e){return Promise.reject(e)}},n.updateConfigForReuseWarning=function(e){var r=e.gaiaConfig;try{return D(this.walletConfig),this.walletConfig.hideWarningForReusingIdentity=!0,Promise.resolve(this.updateConfig(r)).then((function(){}))}catch(e){return Promise.reject(e)}},n.getSigner=function(){return new re({privateKey:this.stacksPrivateKey})},e}(),ne={Wallet:te};e.DEFAULT_PROFILE=N,e.Identity=I,e.Wallet=te,e.WalletSigner=re,e.assertIsTruthy=D,e.decrypt=G,e.default=ne,e.derivationPaths=Y,e.deriveIdentityKeyPair=L,e.deriveRootKeychainFromMnemonic=Z,e.deriveStxAddressChain=ee,e.encrypt=W,e.encryptMnemonicFormatted=J,e.fetchProfile=U,e.generateEncryptedMnemonicRootKeychain=$,e.generateMnemonicRootKeychain=function(e){try{var n=r.generateMnemonic(e,u.randomBytes);return Promise.resolve(r.mnemonicToSeed(n)).then((function(e){return{rootNode:t.bip32.fromSeed(e),plaintextMnemonic:n}}))}catch(e){return Promise.reject(e)}},e.getAddress=B,e.getBitcoinAddressNode=M,e.getBitcoinPrivateKeychain=H,e.getBlockchainIdentities=E,e.getDerivationPath=Q,e.getIdentityOwnerAddressNode=x,e.getIdentityPrivateKeychain=_,e.getProfileURLFromZoneFile=V,e.makeIdentity=F,e.recursiveRestoreIdentities=z,e.registerSubdomain=function(e){var r=e.identity,t=e.gaiaHubUrl,n=e.username,i=e.subdomain;try{var o=T(r.profile||N,r.keyPair);return Promise.resolve(C({gaiaHubUrl:t,filePath:"profile.json",identity:r,signedProfileTokenData:o})).then((function(e){var t=n+"."+i,o=d.makeProfileZoneFile(t,e);return Promise.resolve(function(e){var r=e.username,t=e.subdomain,n=e.zoneFile,i=e.identity;try{var o=w[t].registerUrl,s=JSON.stringify({name:r,owner_address:i.address,zonefile:n});return Promise.resolve(fetch(o,{method:"POST",headers:{Accept:"application/json","Content-Type":"application/json"},body:s})).then((function(e){return e.ok?e.json():Promise.reject({error:"Failed to register username",status:e.status})}))}catch(e){return Promise.reject(e)}}({username:n,subdomain:i,zoneFile:o,identity:r})).then((function(){return r.defaultUsername=t,r.usernames.push(t),r}))}))}catch(e){return Promise.reject(e)}},e.registrars=w,e.signAndUploadProfile=j,e.signProfileForUpload=T,e.uploadProfile=C,e.validateSubdomain=function(r,t){try{var n=!1;void 0===t&&(t=e.Subdomains.BLOCKSTACK);var i=q(r);if(i)return Promise.resolve(i);var o=b((function(){return Promise.resolve(X(r,t)).then((function(r){if("available"!==r.status)return n=!0,e.IdentityNameValidityError.UNAVAILABLE}))}),(function(){return n=!0,e.IdentityNameValidityError.UNAVAILABLE}));return Promise.resolve(o&&o.then?o.then((function(e){return n?e:null})):n?o:null)}catch(e){return Promise.reject(e)}},e.validateSubdomainAvailability=X,e.validateSubdomainFormat=q}));
//# sourceMappingURL=keychain.umd.production.min.js.map
{
"name": "@blockstack/keychain",
"version": "0.12.5",
"version": "0.12.6",
"description": "A package for managing Blockstack keychains",

@@ -58,3 +58,3 @@ "main": "./dist/index.js",

"dependencies": {
"@blockstack/rpc-client": "^0.3.0-alpha.19",
"@blockstack/rpc-client": "^0.3.0-alpha.20",
"@blockstack/stacks-transactions": "0.5.1",

@@ -74,3 +74,3 @@ "bip39": "^3.0.2",

},
"gitHead": "9c56937b260db89770d8bc34d2cc3576154927ee"
"gitHead": "528a38057acec8ba19a7d1ccc979facdd0e02503"
}

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display

Sorry, the diff of this file is too big to display