@wangjinbao/wallet-connect 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/cjs/index.js CHANGED
@@ -1,11 +1,2 @@
1
- "use strict";var e,t=require("react"),n={exports:{}},r={};var a,o={};
2
- /**
3
- * @license React
4
- * react-jsx-runtime.production.min.js
5
- *
6
- * Copyright (c) Facebook, Inc. and its affiliates.
7
- *
8
- * This source code is licensed under the MIT license found in the
9
- * LICENSE file in the root directory of this source tree.
10
- */"production"===process.env.NODE_ENV?n.exports=function(){if(a)return o;a=1;var e=t,n=Symbol.for("react.element"),r=Symbol.for("react.fragment"),s=Object.prototype.hasOwnProperty,i=e.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner,c={key:!0,ref:!0,__self:!0,__source:!0};function l(e,t,r){var a,o={},l=null,u=null;for(a in void 0!==r&&(l=""+r),void 0!==t.key&&(l=""+t.key),void 0!==t.ref&&(u=t.ref),t)s.call(t,a)&&!c.hasOwnProperty(a)&&(o[a]=t[a]);if(e&&e.defaultProps)for(a in t=e.defaultProps)void 0===o[a]&&(o[a]=t[a]);return{$$typeof:n,type:e,key:l,ref:u,props:o,_owner:i.current}}return o.Fragment=r,o.jsx=l,o.jsxs=l,o}():n.exports=(e||(e=1,"production"!==process.env.NODE_ENV&&function(){var e,n=t,a=Symbol.for("react.element"),o=Symbol.for("react.portal"),s=Symbol.for("react.fragment"),i=Symbol.for("react.strict_mode"),c=Symbol.for("react.profiler"),l=Symbol.for("react.provider"),u=Symbol.for("react.context"),d=Symbol.for("react.forward_ref"),f=Symbol.for("react.suspense"),h=Symbol.for("react.suspense_list"),p=Symbol.for("react.memo"),m=Symbol.for("react.lazy"),y=Symbol.for("react.offscreen"),g=Symbol.iterator,b=n.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED;function w(e){for(var t=arguments.length,n=new Array(t>1?t-1:0),r=1;r<t;r++)n[r-1]=arguments[r];!function(e,t,n){var r=b.ReactDebugCurrentFrame.getStackAddendum();""!==r&&(t+="%s",n=n.concat([r]));var a=n.map(function(e){return String(e)});a.unshift("Warning: "+t),Function.prototype.apply.call(console[e],console,a)}("error",e,n)}function v(e){return e.displayName||"Context"}function x(e){if(null==e)return null;if("number"==typeof e.tag&&w("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."),"function"==typeof e)return e.displayName||e.name||null;if("string"==typeof e)return e;switch(e){case s:return"Fragment";case o:return"Portal";case c:return"Profiler";case i:return"StrictMode";case f:return"Suspense";case h:return"SuspenseList"}if("object"==typeof e)switch(e.$$typeof){case u:return v(e)+".Consumer";case l:return v(e._context)+".Provider";case d:return function(e,t,n){var r=e.displayName;if(r)return r;var a=t.displayName||t.name||"";return""!==a?n+"("+a+")":n}(e,e.render,"ForwardRef");case p:var t=e.displayName||null;return null!==t?t:x(e.type)||"Memo";case m:var n=e,r=n._payload,a=n._init;try{return x(a(r))}catch(e){return null}}return null}e=Symbol.for("react.module.reference");var j,N,C,E,k,I,_,O=Object.assign,S=0;function R(){}R.__reactDisabledLog=!0;var T,D=b.ReactCurrentDispatcher;function P(e,t,n){if(void 0===T)try{throw Error()}catch(e){var r=e.stack.trim().match(/\n( *(at )?)/);T=r&&r[1]||""}return"\n"+T+e}var L,U=!1,$="function"==typeof WeakMap?WeakMap:Map;function A(e,t){if(!e||U)return"";var n,r=L.get(e);if(void 0!==r)return r;U=!0;var a,o=Error.prepareStackTrace;Error.prepareStackTrace=void 0,a=D.current,D.current=null,function(){if(0===S){j=console.log,N=console.info,C=console.warn,E=console.error,k=console.group,I=console.groupCollapsed,_=console.groupEnd;var e={configurable:!0,enumerable:!0,value:R,writable:!0};Object.defineProperties(console,{info:e,log:e,warn:e,error:e,group:e,groupCollapsed:e,groupEnd:e})}S++}();try{if(t){var s=function(){throw Error()};if(Object.defineProperty(s.prototype,"props",{set:function(){throw Error()}}),"object"==typeof Reflect&&Reflect.construct){try{Reflect.construct(s,[])}catch(e){n=e}Reflect.construct(e,[],s)}else{try{s.call()}catch(e){n=e}e.call(s.prototype)}}else{try{throw Error()}catch(e){n=e}e()}}catch(t){if(t&&n&&"string"==typeof t.stack){for(var i=t.stack.split("\n"),c=n.stack.split("\n"),l=i.length-1,u=c.length-1;l>=1&&u>=0&&i[l]!==c[u];)u--;for(;l>=1&&u>=0;l--,u--)if(i[l]!==c[u]){if(1!==l||1!==u)do{if(l--,--u<0||i[l]!==c[u]){var d="\n"+i[l].replace(" at new "," at ");return e.displayName&&d.includes("<anonymous>")&&(d=d.replace("<anonymous>",e.displayName)),"function"==typeof e&&L.set(e,d),d}}while(l>=1&&u>=0);break}}}finally{U=!1,D.current=a,function(){if(0===--S){var e={configurable:!0,enumerable:!0,writable:!0};Object.defineProperties(console,{log:O({},e,{value:j}),info:O({},e,{value:N}),warn:O({},e,{value:C}),error:O({},e,{value:E}),group:O({},e,{value:k}),groupCollapsed:O({},e,{value:I}),groupEnd:O({},e,{value:_})})}S<0&&w("disabledDepth fell below zero. This is a bug in React. Please file an issue.")}(),Error.prepareStackTrace=o}var f=e?e.displayName||e.name:"",h=f?P(f):"";return"function"==typeof e&&L.set(e,h),h}function W(e,t,n){if(null==e)return"";if("function"==typeof e)return A(e,!(!(r=e.prototype)||!r.isReactComponent));var r;if("string"==typeof e)return P(e);switch(e){case f:return P("Suspense");case h:return P("SuspenseList")}if("object"==typeof e)switch(e.$$typeof){case d:return A(e.render,!1);case p:return W(e.type,t,n);case m:var a=e,o=a._payload,s=a._init;try{return W(s(o),t,n)}catch(e){}}return""}L=new $;var F=Object.prototype.hasOwnProperty,B={},M=b.ReactDebugCurrentFrame;function H(e){if(e){var t=e._owner,n=W(e.type,e._source,t?t.type:null);M.setExtraStackFrame(n)}else M.setExtraStackFrame(null)}var q=Array.isArray;function V(e){return q(e)}function z(e){return""+e}function Y(e){if(function(e){try{return z(e),!1}catch(e){return!0}}(e))return w("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.",function(e){return"function"==typeof Symbol&&Symbol.toStringTag&&e[Symbol.toStringTag]||e.constructor.name||"Object"}(e)),z(e)}var Z,J,X,G=b.ReactCurrentOwner,K={key:!0,ref:!0,__self:!0,__source:!0};function Q(e,t,n,r,o){var s,i={},c=null,l=null;for(s in void 0!==n&&(Y(n),c=""+n),function(e){if(F.call(e,"key")){var t=Object.getOwnPropertyDescriptor(e,"key").get;if(t&&t.isReactWarning)return!1}return void 0!==e.key}(t)&&(Y(t.key),c=""+t.key),function(e){if(F.call(e,"ref")){var t=Object.getOwnPropertyDescriptor(e,"ref").get;if(t&&t.isReactWarning)return!1}return void 0!==e.ref}(t)&&(l=t.ref,function(e,t){if("string"==typeof e.ref&&G.current&&t&&G.current.stateNode!==t){var n=x(G.current.type);X[n]||(w('Component "%s" contains the string ref "%s". Support for string refs will be removed in a future major release. This case cannot be automatically converted to an arrow function. We ask you to manually fix this case by using useRef() or createRef() instead. Learn more about using refs safely here: https://reactjs.org/link/strict-mode-string-ref',x(G.current.type),e.ref),X[n]=!0)}}(t,o)),t)F.call(t,s)&&!K.hasOwnProperty(s)&&(i[s]=t[s]);if(e&&e.defaultProps){var u=e.defaultProps;for(s in u)void 0===i[s]&&(i[s]=u[s])}if(c||l){var d="function"==typeof e?e.displayName||e.name||"Unknown":e;c&&function(e,t){var n=function(){Z||(Z=!0,w("%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)",t))};n.isReactWarning=!0,Object.defineProperty(e,"key",{get:n,configurable:!0})}(i,d),l&&function(e,t){var n=function(){J||(J=!0,w("%s: `ref` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://reactjs.org/link/special-props)",t))};n.isReactWarning=!0,Object.defineProperty(e,"ref",{get:n,configurable:!0})}(i,d)}return function(e,t,n,r,o,s,i){var c={$$typeof:a,type:e,key:t,ref:n,props:i,_owner:s,_store:{}};return Object.defineProperty(c._store,"validated",{configurable:!1,enumerable:!1,writable:!0,value:!1}),Object.defineProperty(c,"_self",{configurable:!1,enumerable:!1,writable:!1,value:r}),Object.defineProperty(c,"_source",{configurable:!1,enumerable:!1,writable:!1,value:o}),Object.freeze&&(Object.freeze(c.props),Object.freeze(c)),c}(e,c,l,o,r,G.current,i)}X={};var ee,te=b.ReactCurrentOwner,ne=b.ReactDebugCurrentFrame;function re(e){if(e){var t=e._owner,n=W(e.type,e._source,t?t.type:null);ne.setExtraStackFrame(n)}else ne.setExtraStackFrame(null)}function ae(e){return"object"==typeof e&&null!==e&&e.$$typeof===a}function oe(){if(te.current){var e=x(te.current.type);if(e)return"\n\nCheck the render method of `"+e+"`."}return""}ee=!1;var se={};function ie(e,t){if(e._store&&!e._store.validated&&null==e.key){e._store.validated=!0;var n=function(e){var t=oe();if(!t){var n="string"==typeof e?e:e.displayName||e.name;n&&(t="\n\nCheck the top-level render call using <"+n+">.")}return t}(t);if(!se[n]){se[n]=!0;var r="";e&&e._owner&&e._owner!==te.current&&(r=" It was passed a child from "+x(e._owner.type)+"."),re(e),w('Each child in a list should have a unique "key" prop.%s%s See https://reactjs.org/link/warning-keys for more information.',n,r),re(null)}}}function ce(e,t){if("object"==typeof e)if(V(e))for(var n=0;n<e.length;n++){var r=e[n];ae(r)&&ie(r,t)}else if(ae(e))e._store&&(e._store.validated=!0);else if(e){var a=function(e){if(null===e||"object"!=typeof e)return null;var t=g&&e[g]||e["@@iterator"];return"function"==typeof t?t:null}(e);if("function"==typeof a&&a!==e.entries)for(var o,s=a.call(e);!(o=s.next()).done;)ae(o.value)&&ie(o.value,t)}}function le(e){var t,n=e.type;if(null!=n&&"string"!=typeof n){if("function"==typeof n)t=n.propTypes;else{if("object"!=typeof n||n.$$typeof!==d&&n.$$typeof!==p)return;t=n.propTypes}if(t){var r=x(n);!function(e,t,n,r,a){var o=Function.call.bind(F);for(var s in e)if(o(e,s)){var i=void 0;try{if("function"!=typeof e[s]){var c=Error((r||"React class")+": "+n+" type `"+s+"` is invalid; it must be a function, usually from the `prop-types` package, but received `"+typeof e[s]+"`.This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`.");throw c.name="Invariant Violation",c}i=e[s](t,s,r,n,null,"SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED")}catch(e){i=e}!i||i instanceof Error||(H(a),w("%s: type specification of %s `%s` is invalid; the type checker function must return `null` or an `Error` but returned a %s. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).",r||"React class",n,s,typeof i),H(null)),i instanceof Error&&!(i.message in B)&&(B[i.message]=!0,H(a),w("Failed %s type: %s",n,i.message),H(null))}}(t,e.props,"prop",r,e)}else void 0===n.PropTypes||ee||(ee=!0,w("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?",x(n)||"Unknown"));"function"!=typeof n.getDefaultProps||n.getDefaultProps.isReactClassApproved||w("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead.")}}function ue(t,n,r,o,g,b){var v=function(t){return"string"==typeof t||"function"==typeof t||t===s||t===c||t===i||t===f||t===h||t===y||"object"==typeof t&&null!==t&&(t.$$typeof===m||t.$$typeof===p||t.$$typeof===l||t.$$typeof===u||t.$$typeof===d||t.$$typeof===e||void 0!==t.getModuleId)}(t);if(!v){var j="";(void 0===t||"object"==typeof t&&null!==t&&0===Object.keys(t).length)&&(j+=" You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.");var N,C=function(e){return void 0!==e?"\n\nCheck your code at "+e.fileName.replace(/^.*[\\\/]/,"")+":"+e.lineNumber+".":""}(g);j+=C||oe(),null===t?N="null":V(t)?N="array":void 0!==t&&t.$$typeof===a?(N="<"+(x(t.type)||"Unknown")+" />",j=" Did you accidentally export a JSX literal instead of a component?"):N=typeof t,w("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s",N,j)}var E=Q(t,n,r,g,b);if(null==E)return E;if(v){var k=n.children;if(void 0!==k)if(o)if(V(k)){for(var I=0;I<k.length;I++)ce(k[I],t);Object.freeze&&Object.freeze(k)}else w("React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead.");else ce(k,t)}return t===s?function(e){for(var t=Object.keys(e.props),n=0;n<t.length;n++){var r=t[n];if("children"!==r&&"key"!==r){re(e),w("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.",r),re(null);break}}null!==e.ref&&(re(e),w("Invalid attribute `ref` supplied to `React.Fragment`."),re(null))}(E):le(E),E}var de=function(e,t,n){return ue(e,t,n,!1)},fe=function(e,t,n){return ue(e,t,n,!0)};r.Fragment=s,r.jsx=de,r.jsxs=fe}()),r);var s=n.exports;const i=t.createContext(null);class c extends Error{constructor(e,t,n){super(e),Object.defineProperty(this,"code",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"data",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.name="WalletError",this.code=t,this.data=n}}const l={USER_REJECTED:4001,UNAUTHORIZED:4100,UNSUPPORTED_METHOD:4200,DISCONNECTED:4900,CHAIN_DISCONNECTED:4901,UNRECOGNIZED_CHAIN:4902,INVALID_PARAMS:-32602,INTERNAL_ERROR:-32603,PROVIDER_NOT_FOUND:-32e3,WALLET_NOT_INSTALLED:-32001,ALREADY_CONNECTED:-32002};function u(e,t,n){return new c(e,t,n)}function d(e){if(e instanceof c)return e;if("object"==typeof e&&null!==e){const t=e;return u(t.message||"未知错误",t.code||l.INTERNAL_ERROR,t.data)}return u("string"==typeof e?e:"发生未知错误",l.INTERNAL_ERROR)}class f{constructor(){Object.defineProperty(this,"provider",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"eventListeners",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"handleAccountsChanged",{enumerable:!0,configurable:!0,writable:!0,value:e=>{this.emit("accountsChanged",e)}}),Object.defineProperty(this,"handleChainChanged",{enumerable:!0,configurable:!0,writable:!0,value:e=>{this.emit("chainChanged",e)}}),Object.defineProperty(this,"handleDisconnect",{enumerable:!0,configurable:!0,writable:!0,value:()=>{this.emit("disconnect",null),this.cleanup()}})}async connect(){if(!this.isInstalled())throw u(`${this.getInfo().name} 未安装`,l.WALLET_NOT_INSTALLED);const e=this.getProvider();if(!e)throw u("未找到提供者",l.PROVIDER_NOT_FOUND);this.provider=e,this.setupEventListeners();try{const e=await this.request({method:"eth_requestAccounts"});if(!e||0===e.length)throw u("未返回账户",l.UNAUTHORIZED);return e}catch(e){throw this.cleanup(),d(e)}}async disconnect(){this.cleanup()}async getAccounts(){if(!this.provider)return[];try{return await this.request({method:"eth_accounts"})||[]}catch(e){return console.error("获取账户失败:",e),[]}}async getBalance(e){if(!this.provider)return"0x0";try{return await this.request({method:"eth_getBalance",params:[e,"latest"]})}catch(e){return console.error("获取余额失败:",e),"0x0"}}async getChainId(){if(!this.provider)throw u("提供者未连接",l.DISCONNECTED);try{return await this.request({method:"eth_chainId"})}catch(e){throw d(e)}}async switchChain(e){if(!this.provider)throw u("提供者未连接",l.DISCONNECTED);try{await this.request({method:"wallet_switchEthereumChain",params:[{chainId:e.chainId}]})}catch(t){if(!this.isUnrecognizedChainError(t))throw d(t);try{await this.request({method:"wallet_addEthereumChain",params:[{chainId:e.chainId,chainName:e.chainName,nativeCurrency:e.nativeCurrency,rpcUrls:e.rpcUrls,blockExplorerUrls:e.blockExplorerUrls,iconUrls:e.iconUrls}]})}catch(e){throw d(e)}}}on(e,t){this.eventListeners.has(e)||this.eventListeners.set(e,new Set),this.eventListeners.get(e).add(t)}off(e,t){const n=this.eventListeners.get(e);n&&n.delete(t)}async request(e){if(!this.provider)throw u("提供者未连接",l.DISCONNECTED);try{return await this.provider.request(e)}catch(e){throw d(e)}}setupEventListeners(){this.provider&&(this.provider.on("accountsChanged",this.handleAccountsChanged),this.provider.on("chainChanged",this.handleChainChanged),this.provider.on("disconnect",this.handleDisconnect))}cleanup(){this.provider&&(this.provider.removeListener("accountsChanged",this.handleAccountsChanged),this.provider.removeListener("chainChanged",this.handleChainChanged),this.provider.removeListener("disconnect",this.handleDisconnect)),this.provider=null,this.eventListeners.clear()}emit(e,t){const n=this.eventListeners.get(e);n&&n.forEach(n=>{try{n(t)}catch(t){console.error(`${e} 监听器错误:`,t)}})}isUnrecognizedChainError(e){return"object"==typeof e&&null!==e&&"code"in e&&e.code===l.UNRECOGNIZED_CHAIN}}class h extends f{getInfo(){return{id:"metamask",name:"MetaMask",downloadUrl:"https://metamask.io/download/"}}isInstalled(){return"undefined"!=typeof window&&Boolean(window.ethereum?.isMetaMask)}getProvider(){return"undefined"==typeof window?null:window.ethereum?.isMetaMask?window.ethereum:null}}class p extends f{getInfo(){return{id:"okx",name:"OKX Wallet",downloadUrl:"https://www.okx.com/web3"}}isInstalled(){return"undefined"!=typeof window&&Boolean(window.okxwallet)}getProvider(){return"undefined"==typeof window?null:window.okxwallet||null}}class m extends f{getInfo(){return{id:"phantom",name:"Phantom",downloadUrl:"https://phantom.app/"}}isInstalled(){return"undefined"!=typeof window&&Boolean(window.phantom?.ethereum)}getProvider(){return"undefined"==typeof window?null:window.phantom?.ethereum||null}}class y extends f{getInfo(){return{id:"coinbase",name:"Coinbase Wallet",downloadUrl:"https://www.coinbase.com/wallet"}}isInstalled(){return"undefined"!=typeof window&&Boolean(window.coinbaseWalletExtension)}getProvider(){return"undefined"==typeof window?null:window.coinbaseWalletExtension||null}}const g=[new h,new p,new m,new y];function b(e){return g.find(t=>t.getInfo().id===e)}const w={chainId:"0x1",chainName:"Ethereum Mainnet",nativeCurrency:{name:"Ether",symbol:"ETH",decimals:18},rpcUrls:["https://eth.llamarpc.com"],blockExplorerUrls:["https://etherscan.io"]},v={chainId:"0xaa36a7",chainName:"Sepolia",nativeCurrency:{name:"Sepolia Ether",symbol:"ETH",decimals:18},rpcUrls:["https://rpc.sepolia.org"],blockExplorerUrls:["https://sepolia.etherscan.io"]},x=[w,v];const j="wjb_wallet_connect";function N(){const e=t.useContext(i);if(!e)throw new Error("useWallet 必须在 WalletProvider 中使用");return e}function C({onClose:e}){const{connect:n,availableWallets:r}=N(),[a,o]=t.useState(!1),[i,c]=t.useState(null),l=e=>"metamask"===e?s.jsx("div",{className:"w-8 h-8 rounded-lg bg-orange-100 flex items-center justify-center text-orange-500",children:"🦊"}):"okx"===e?s.jsx("div",{className:"w-8 h-8 rounded-lg bg-black flex items-center justify-center text-white",children:"X"}):"phantom"===e?s.jsx("div",{className:"w-8 h-8 rounded-lg bg-purple-100 flex items-center justify-center text-purple-500",children:"P"}):"coinbase"===e?s.jsx("div",{className:"w-8 h-8 rounded-lg bg-blue-100 flex items-center justify-center text-blue-500",children:"C"}):s.jsx("div",{className:"w-8 h-8 rounded-lg bg-gray-200"});return s.jsx("div",{className:"wjb-modal-overlay",onClick:t=>{t.target===t.currentTarget&&e()},children:s.jsxs("div",{className:"wjb-wallet-modal-content",children:[s.jsxs("div",{className:"wjb-modal-left",children:[s.jsx("div",{className:"wjb-modal-header",children:s.jsx("h2",{className:"wjb-modal-title",children:"连接钱包"})}),s.jsxs("div",{className:"wjb-wallet-list",children:[r.map(t=>s.jsxs("button",{onClick:()=>(async t=>{o(!0),c(null);try{await n(t),e()}catch(e){c(e instanceof Error?e.message:"连接失败")}finally{o(!1)}})(t.id),disabled:a,className:"wjb-wallet-option",children:[l(t.id),s.jsx("span",{className:"flex-1",children:t.name}),a&&s.jsx("span",{className:"text-xs text-blue-500 animate-pulse",children:"连接中..."})]},t.id)),g.filter(e=>!e.isInstalled()).map(e=>{const t=e.getInfo();return s.jsxs("a",{href:t.downloadUrl,target:"_blank",rel:"noopener noreferrer",className:"wjb-wallet-option opacity-60 hover:opacity-100",children:[l(t.id),s.jsx("span",{className:"flex-1",children:t.name}),s.jsx("span",{className:"text-xs font-bold text-blue-500 bg-blue-50 px-2 py-1 rounded",children:"安装"})]},t.id)})]}),i&&s.jsx("div",{className:"mt-4 p-3 bg-red-50 text-red-500 text-sm rounded-xl",children:i})]}),s.jsxs("div",{className:"wjb-modal-right hidden md:flex",children:[s.jsxs("div",{className:"grid grid-cols-2 gap-4 mb-6 opacity-80",children:[s.jsx("div",{className:"w-12 h-12 bg-blue-500 rounded-xl shadow-lg -rotate-6"}),s.jsx("div",{className:"w-12 h-12 bg-purple-500 rounded-xl shadow-lg rotate-12"}),s.jsx("div",{className:"w-12 h-12 bg-orange-500 rounded-xl shadow-lg rotate-6"}),s.jsx("div",{className:"w-12 h-12 bg-green-500 rounded-xl shadow-lg -rotate-12"})]}),s.jsx("h3",{className:"text-lg font-bold text-gray-900 dark:text-white mb-2",children:"什么是钱包?"}),s.jsxs("ul",{className:"text-sm text-gray-500 dark:text-gray-400 space-y-3 text-left max-w-xs",children:[s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx("span",{className:"mt-1",children:"🏠"}),s.jsxs("span",{children:["您的数字资产之家",s.jsx("br",{}),"发送、接收和存储资产。"]})]}),s.jsxs("li",{className:"flex items-start gap-2",children:[s.jsx("span",{className:"mt-1",children:"🔐"}),s.jsxs("span",{children:["一种新的登录方式",s.jsx("br",{}),"无需创建账户和密码。"]})]})]}),s.jsx("button",{className:"mt-6 px-4 py-2 bg-gray-200 dark:bg-gray-700 rounded-lg text-sm font-bold text-gray-700 dark:text-white hover:bg-gray-300 transition-colors",children:"了解更多"})]})]})})}function E({onClose:e}){const{supportedChains:t,switchChain:n,state:r}=N();return s.jsx("div",{className:"wjb-modal-overlay",onClick:t=>{t.target===t.currentTarget&&e()},children:s.jsxs("div",{className:"wjb-chain-switcher-content",children:[s.jsxs("div",{className:"p-4 border-b border-gray-100 dark:border-gray-800 flex justify-between items-center",children:[s.jsx("h3",{className:"font-bold text-gray-900 dark:text-white",children:"切换网络"}),s.jsx("button",{onClick:e,className:"p-1 hover:bg-gray-100 rounded-full text-gray-400",children:"✕"})]}),s.jsx("div",{className:"p-2 space-y-1",children:t.map(t=>s.jsxs("button",{onClick:()=>(async t=>{r.chainId!==t&&await n(t),e()})(t.chainId),className:"wjb-chain-option "+(r.chainId===t.chainId?"active":""),children:[s.jsxs("div",{className:"flex items-center",children:[s.jsx("div",{className:"wjb-chain-icon flex items-center justify-center text-white text-xs "+("0x1"===t.chainId?"bg-blue-500":"bg-purple-500"),children:t.chainName[0]}),s.jsx("span",{children:t.chainName})]}),r.chainId===t.chainId&&s.jsx("span",{className:"text-sm font-bold text-green-500",children:"已连接"})]},t.chainId))})]})})}function k(e){if(!e||"0x0"===e)return"0 ETH";try{const t=e.startsWith("0x")?e:`0x${e}`,n=BigInt(t),r=1000000000000000000n,a=n/r,o=(n%r).toString().padStart(18,"0").slice(0,4).replace(/0+$/,"");return o?`${a}.${o} ETH`:`${a} ETH`}catch(e){return console.error("Error formatting balance:",e),"0 ETH"}}function I(e){return e?`${e.slice(0,6)}...${e.slice(-4)}`:""}function _({onClose:e}){const{state:t,disconnect:n}=N(),{account:r,balance:a}=t;return s.jsx("div",{className:"wjb-modal-overlay",onClick:t=>{t.target===t.currentTarget&&e()},children:s.jsxs("div",{className:"wjb-account-modal-content",children:[s.jsx("button",{onClick:e,className:"wjb-account-modal-close",children:"✕"}),s.jsx("div",{className:"wjb-account-avatar-large"}),s.jsx("div",{className:"wjb-account-address-large",children:I(r)}),s.jsx("div",{className:"wjb-account-balance-large",children:t.balance||"0 ETH"}),s.jsxs("div",{className:"wjb-account-actions",children:[s.jsxs("button",{onClick:async()=>{r&&await navigator.clipboard.writeText(r)},className:"wjb-action-button",children:[s.jsxs("svg",{width:"24",height:"24",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[s.jsx("rect",{x:"9",y:"9",width:"13",height:"13",rx:"2",ry:"2"}),s.jsx("path",{d:"M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"})]}),"复制地址"]}),s.jsxs("button",{onClick:async()=>{await n(),e()},className:"wjb-action-button text-red-500 hover:text-red-600",children:[s.jsxs("svg",{width:"24",height:"24",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[s.jsx("path",{d:"M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"}),s.jsx("polyline",{points:"16 17 21 12 16 7"}),s.jsx("line",{x1:"21",y1:"12",x2:"9",y2:"12"})]}),"断开连接"]})]})]})})}exports.BaseWallet=f,exports.CoinbaseWallet=y,exports.ConnectButton=function(){const{state:e}=N(),[n,r]=t.useState(!1),[a,o]=t.useState(!1),[i,c]=t.useState(!1);return e.isConnected?s.jsxs(s.Fragment,{children:[s.jsx("div",{className:"wjb-wallet-connected",children:s.jsxs("div",{className:"wjb-wallet-info-group",children:[s.jsxs("button",{onClick:()=>o(!0),className:"wjb-chain-button",children:[s.jsx("div",{className:`w-5 h-5 rounded-full ${"0x1"===e.chainId?"bg-blue-500":"0xaa36a7"===e.chainId?"bg-purple-500":"bg-gray-500"} flex items-center justify-center text-white text-[10px]`,children:"0x1"===e.chainId?"E":"0xaa36a7"===e.chainId?"S":"?"}),s.jsxs("span",{children:["0x1"===e.chainId&&"Ethereum","0xaa36a7"===e.chainId&&"Sepolia",e.chainId&&"0x1"!==e.chainId&&"0xaa36a7"!==e.chainId&&"Unknown"]}),s.jsx("svg",{width:"12",height:"12",viewBox:"0 0 12 12",fill:"none",className:"text-gray-400",children:s.jsx("path",{d:"M2.5 4.5L6 8L9.5 4.5",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round"})})]}),s.jsx("div",{className:"wjb-balance",children:e.balance?k(e.balance):"0 ETH"}),s.jsxs("button",{onClick:()=>c(!0),className:"wjb-account-button",children:[s.jsx("span",{children:e.account&&I(e.account)}),s.jsx("div",{className:"wjb-account-avatar"}),s.jsx("svg",{width:"12",height:"12",viewBox:"0 0 12 12",fill:"none",className:"text-gray-400 ml-1",children:s.jsx("path",{d:"M2.5 4.5L6 8L9.5 4.5",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round"})})]})]})}),a&&s.jsx(E,{onClose:()=>o(!1)}),i&&s.jsx(_,{onClose:()=>c(!1)})]}):s.jsxs(s.Fragment,{children:[s.jsx("button",{onClick:()=>r(!0),className:"wjb-connect-button wjb-connect-button-primary",children:"连接钱包"}),n&&s.jsx(C,{onClose:()=>r(!1)})]})},exports.ErrorCode=l,exports.MetaMaskWallet=h,exports.OKXWallet=p,exports.PhantomWallet=m,exports.WalletProvider=function({children:e,config:n}){const{chains:r=x,autoConnect:a=!0}=n,[o,c]=t.useState({isConnected:!1,account:null,chainId:null,balance:null,wallet:null}),[d,f]=t.useState(null),h=t.useMemo(()=>g.filter(e=>e.isInstalled()).map(e=>e.getInfo()),[]);t.useCallback(async(e,t)=>{try{const n=await e.getBalance(t);c(e=>({...e,balance:n}))}catch(e){console.error("更新余额失败:",e)}},[]);const p=t.useCallback(async e=>{try{if(o.isConnected&&o.wallet?.id===e)throw u("钱包已连接",l.ALREADY_CONNECTED);d&&await d.disconnect();const t=b(e);if(!t)throw u(`未找到钱包 ${e}`,l.PROVIDER_NOT_FOUND);const n=await t.connect(),r=await t.getChainId(),a=await t.getBalance(n[0]),s=t.getInfo();t.on("accountsChanged",async e=>{const n=e;if(0===n.length)m();else{const e=n[0],r=await t.getBalance(e);c(t=>({...t,account:e,balance:r}))}}),t.on("chainChanged",async e=>{const n=(await t.getAccounts())[0];let r="0x0";n&&(r=await t.getBalance(n)),c(t=>({...t,chainId:e,balance:r}))}),t.on("disconnect",()=>{m()}),f(t),c({isConnected:!0,account:n[0],chainId:r,balance:a,wallet:s}),"undefined"!=typeof window&&localStorage.setItem(j,JSON.stringify({walletId:e,account:n[0]}))}catch(e){throw console.error("连接钱包失败:",e),e}},[o.isConnected,o.wallet,d]),m=t.useCallback(async()=>{d&&await d.disconnect(),f(null),c({isConnected:!1,account:null,chainId:null,balance:null,wallet:null}),"undefined"!=typeof window&&localStorage.removeItem(j)},[d]),y=t.useCallback(async e=>{if(!d)throw u("未连接钱包",l.DISCONNECTED);const t=function(e,t){return t.find(t=>t.chainId.toLowerCase()===e.toLowerCase())}(e,r);if(!t)throw u(`不支持链 ${e}`,l.UNRECOGNIZED_CHAIN);await d.switchChain(t)},[d,r]);t.useEffect(()=>{if(!a)return;(async()=>{if("undefined"==typeof window)return;const e=localStorage.getItem(j);if(e)try{const{walletId:t}=JSON.parse(e),n=b(t);if(!n||!n.isInstalled())return void localStorage.removeItem(j);const r=await n.getAccounts();if(r.length>0){const e=await n.getChainId(),t=await n.getBalance(r[0]),a=n.getInfo();n.on("accountsChanged",async e=>{const t=e;if(0===t.length)m();else{const e=t[0],r=await n.getBalance(e);c(t=>({...t,account:e,balance:r}))}}),n.on("chainChanged",async e=>{const t=(await n.getAccounts())[0];let r="0x0";t&&(r=await n.getBalance(t)),c(t=>({...t,chainId:e,balance:r}))}),n.on("disconnect",()=>{m()}),f(n),c({isConnected:!0,account:r[0],chainId:e,balance:t,wallet:a})}else localStorage.removeItem(j)}catch(e){console.error("自动连接失败:",e),localStorage.removeItem(j)}})()},[a]);const w=t.useMemo(()=>({state:o,connect:p,disconnect:m,switchChain:y,availableWallets:h,supportedChains:r}),[o,p,m,y,h,r]);return s.jsx(i.Provider,{value:w,children:e})},exports.defaultChains=x,exports.mainnet=w,exports.sepolia=v,exports.useWallet=N;
1
+ "use strict";var e=require("react/jsx-runtime"),t=require("react");const n=t.createContext(null);class a extends Error{constructor(e,t,n){super(e),Object.defineProperty(this,"code",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),Object.defineProperty(this,"data",{enumerable:!0,configurable:!0,writable:!0,value:void 0}),this.name="WalletError",this.code=t,this.data=n}}const s={USER_REJECTED:4001,UNAUTHORIZED:4100,UNSUPPORTED_METHOD:4200,DISCONNECTED:4900,CHAIN_DISCONNECTED:4901,UNRECOGNIZED_CHAIN:4902,INVALID_PARAMS:-32602,INTERNAL_ERROR:-32603,PROVIDER_NOT_FOUND:-32e3,WALLET_NOT_INSTALLED:-32001,ALREADY_CONNECTED:-32002};function r(e,t,n){return new a(e,t,n)}function i(e){if(e instanceof a)return e;if("object"==typeof e&&null!==e){const t=e;return r(t.message||"未知错误",t.code||s.INTERNAL_ERROR,t.data)}return r("string"==typeof e?e:"发生未知错误",s.INTERNAL_ERROR)}class c{constructor(){Object.defineProperty(this,"provider",{enumerable:!0,configurable:!0,writable:!0,value:null}),Object.defineProperty(this,"eventListeners",{enumerable:!0,configurable:!0,writable:!0,value:new Map}),Object.defineProperty(this,"handleAccountsChanged",{enumerable:!0,configurable:!0,writable:!0,value:e=>{this.emit("accountsChanged",e)}}),Object.defineProperty(this,"handleChainChanged",{enumerable:!0,configurable:!0,writable:!0,value:e=>{this.emit("chainChanged",e)}}),Object.defineProperty(this,"handleDisconnect",{enumerable:!0,configurable:!0,writable:!0,value:()=>{this.emit("disconnect",null),this.cleanup()}})}async connect(){if(!this.isInstalled())throw r(`${this.getInfo().name} 未安装`,s.WALLET_NOT_INSTALLED);const e=this.getProvider();if(!e)throw r("未找到提供者",s.PROVIDER_NOT_FOUND);this.provider=e,this.setupEventListeners();try{const e=await this.request({method:"eth_requestAccounts"});if(!e||0===e.length)throw r("未返回账户",s.UNAUTHORIZED);return e}catch(e){throw this.cleanup(),i(e)}}async disconnect(){this.cleanup()}async getAccounts(){if(!this.provider)return[];try{return await this.request({method:"eth_accounts"})||[]}catch(e){return console.error("获取账户失败:",e),[]}}async getBalance(e){if(!this.provider)return"0x0";try{return await this.request({method:"eth_getBalance",params:[e,"latest"]})}catch(e){return console.error("获取余额失败:",e),"0x0"}}async getChainId(){if(!this.provider)throw r("提供者未连接",s.DISCONNECTED);try{return await this.request({method:"eth_chainId"})}catch(e){throw i(e)}}async switchChain(e){if(!this.provider)throw r("提供者未连接",s.DISCONNECTED);try{await this.request({method:"wallet_switchEthereumChain",params:[{chainId:e.chainId}]})}catch(t){if(!this.isUnrecognizedChainError(t))throw i(t);try{await this.request({method:"wallet_addEthereumChain",params:[{chainId:e.chainId,chainName:e.chainName,nativeCurrency:e.nativeCurrency,rpcUrls:e.rpcUrls,blockExplorerUrls:e.blockExplorerUrls,iconUrls:e.iconUrls}]})}catch(e){throw i(e)}}}on(e,t){this.eventListeners.has(e)||this.eventListeners.set(e,new Set),this.eventListeners.get(e).add(t)}off(e,t){const n=this.eventListeners.get(e);n&&n.delete(t)}async request(e){if(!this.provider)throw r("提供者未连接",s.DISCONNECTED);try{return await this.provider.request(e)}catch(e){throw i(e)}}setupEventListeners(){this.provider&&(this.provider.on("accountsChanged",this.handleAccountsChanged),this.provider.on("chainChanged",this.handleChainChanged),this.provider.on("disconnect",this.handleDisconnect))}cleanup(){this.provider&&(this.provider.removeListener("accountsChanged",this.handleAccountsChanged),this.provider.removeListener("chainChanged",this.handleChainChanged),this.provider.removeListener("disconnect",this.handleDisconnect)),this.provider=null,this.eventListeners.clear()}emit(e,t){const n=this.eventListeners.get(e);n&&n.forEach(n=>{try{n(t)}catch(t){console.error(`${e} 监听器错误:`,t)}})}isUnrecognizedChainError(e){return"object"==typeof e&&null!==e&&"code"in e&&e.code===s.UNRECOGNIZED_CHAIN}}class l extends c{getInfo(){return{id:"metamask",name:"MetaMask",downloadUrl:"https://metamask.io/download/"}}isInstalled(){return"undefined"!=typeof window&&Boolean(window.ethereum?.isMetaMask)}getProvider(){return"undefined"==typeof window?null:window.ethereum?.isMetaMask?window.ethereum:null}}class o extends c{getInfo(){return{id:"okx",name:"OKX Wallet",downloadUrl:"https://www.okx.com/web3"}}isInstalled(){return"undefined"!=typeof window&&Boolean(window.okxwallet)}getProvider(){return"undefined"==typeof window?null:window.okxwallet||null}}class d extends c{getInfo(){return{id:"phantom",name:"Phantom",downloadUrl:"https://phantom.app/"}}isInstalled(){return"undefined"!=typeof window&&Boolean(window.phantom?.ethereum)}getProvider(){return"undefined"==typeof window?null:window.phantom?.ethereum||null}}class h extends c{getInfo(){return{id:"coinbase",name:"Coinbase Wallet",downloadUrl:"https://www.coinbase.com/wallet"}}isInstalled(){return"undefined"!=typeof window&&Boolean(window.coinbaseWalletExtension)}getProvider(){return"undefined"==typeof window?null:window.coinbaseWalletExtension||null}}const u=[new l,new o,new d,new h];function w(e){return u.find(t=>t.getInfo().id===e)}const x={chainId:"0x1",chainName:"Ethereum Mainnet",nativeCurrency:{name:"Ether",symbol:"ETH",decimals:18},rpcUrls:["https://eth.llamarpc.com"],blockExplorerUrls:["https://etherscan.io"]},m={chainId:"0xaa36a7",chainName:"Sepolia",nativeCurrency:{name:"Sepolia Ether",symbol:"ETH",decimals:18},rpcUrls:["https://rpc.sepolia.org"],blockExplorerUrls:["https://sepolia.etherscan.io"]},g=[x,m];const b="wjb_wallet_connect";function p(){const e=t.useContext(n);if(!e)throw new Error("useWallet 必须在 WalletProvider 中使用");return e}function f({onClose:n}){const{connect:a,availableWallets:s}=p(),[r,i]=t.useState(!1),[c,l]=t.useState(null),o=t=>"metamask"===t?e.jsx("div",{className:"w-8 h-8 rounded-lg bg-orange-100 flex items-center justify-center text-orange-500",children:"🦊"}):"okx"===t?e.jsx("div",{className:"w-8 h-8 rounded-lg bg-black flex items-center justify-center text-white",children:"X"}):"phantom"===t?e.jsx("div",{className:"w-8 h-8 rounded-lg bg-purple-100 flex items-center justify-center text-purple-500",children:"P"}):"coinbase"===t?e.jsx("div",{className:"w-8 h-8 rounded-lg bg-blue-100 flex items-center justify-center text-blue-500",children:"C"}):e.jsx("div",{className:"w-8 h-8 rounded-lg bg-gray-200"});return e.jsx("div",{className:"wjb-modal-overlay",onClick:e=>{e.target===e.currentTarget&&n()},children:e.jsxs("div",{className:"wjb-wallet-modal-content",children:[e.jsxs("div",{className:"wjb-modal-left",children:[e.jsx("div",{className:"wjb-modal-header",children:e.jsx("h2",{className:"wjb-modal-title",children:"连接钱包"})}),e.jsxs("div",{className:"wjb-wallet-list",children:[s.map(t=>e.jsxs("button",{onClick:()=>(async e=>{i(!0),l(null);try{await a(e),n()}catch(e){l(e instanceof Error?e.message:"连接失败")}finally{i(!1)}})(t.id),disabled:r,className:"wjb-wallet-option",children:[o(t.id),e.jsx("span",{className:"flex-1",children:t.name}),r&&e.jsx("span",{className:"text-xs text-blue-500 animate-pulse",children:"连接中..."})]},t.id)),u.filter(e=>!e.isInstalled()).map(t=>{const n=t.getInfo();return e.jsxs("a",{href:n.downloadUrl,target:"_blank",rel:"noopener noreferrer",className:"wjb-wallet-option opacity-60 hover:opacity-100",children:[o(n.id),e.jsx("span",{className:"flex-1",children:n.name}),e.jsx("span",{className:"text-xs font-bold text-blue-500 bg-blue-50 px-2 py-1 rounded",children:"安装"})]},n.id)})]}),c&&e.jsx("div",{className:"mt-4 p-3 bg-red-50 text-red-500 text-sm rounded-xl",children:c})]}),e.jsxs("div",{className:"wjb-modal-right hidden md:flex",children:[e.jsxs("div",{className:"grid grid-cols-2 gap-4 mb-6 opacity-80",children:[e.jsx("div",{className:"w-12 h-12 bg-blue-500 rounded-xl shadow-lg -rotate-6"}),e.jsx("div",{className:"w-12 h-12 bg-purple-500 rounded-xl shadow-lg rotate-12"}),e.jsx("div",{className:"w-12 h-12 bg-orange-500 rounded-xl shadow-lg rotate-6"}),e.jsx("div",{className:"w-12 h-12 bg-green-500 rounded-xl shadow-lg -rotate-12"})]}),e.jsx("h3",{className:"text-lg font-bold text-gray-900 dark:text-white mb-2",children:"什么是钱包?"}),e.jsxs("ul",{className:"text-sm text-gray-500 dark:text-gray-400 space-y-3 text-left max-w-xs",children:[e.jsxs("li",{className:"flex items-start gap-2",children:[e.jsx("span",{className:"mt-1",children:"🏠"}),e.jsxs("span",{children:["您的数字资产之家",e.jsx("br",{}),"发送、接收和存储资产。"]})]}),e.jsxs("li",{className:"flex items-start gap-2",children:[e.jsx("span",{className:"mt-1",children:"🔐"}),e.jsxs("span",{children:["一种新的登录方式",e.jsx("br",{}),"无需创建账户和密码。"]})]})]}),e.jsx("button",{className:"mt-6 px-4 py-2 bg-gray-200 dark:bg-gray-700 rounded-lg text-sm font-bold text-gray-700 dark:text-white hover:bg-gray-300 transition-colors",children:"了解更多"})]})]})})}function j({onClose:t}){const{supportedChains:n,switchChain:a,state:s}=p();return e.jsx("div",{className:"wjb-modal-overlay",onClick:e=>{e.target===e.currentTarget&&t()},children:e.jsxs("div",{className:"wjb-chain-switcher-content",children:[e.jsxs("div",{className:"p-4 border-b border-gray-100 dark:border-gray-800 flex justify-between items-center",children:[e.jsx("h3",{className:"font-bold text-gray-900 dark:text-white",children:"切换网络"}),e.jsx("button",{onClick:t,className:"p-1 hover:bg-gray-100 rounded-full text-gray-400",children:"✕"})]}),e.jsx("div",{className:"p-2 space-y-1",children:n.map(n=>e.jsxs("button",{onClick:()=>(async e=>{s.chainId!==e&&await a(e),t()})(n.chainId),className:"wjb-chain-option "+(s.chainId===n.chainId?"active":""),children:[e.jsxs("div",{className:"flex items-center",children:[e.jsx("div",{className:"wjb-chain-icon flex items-center justify-center text-white text-xs "+("0x1"===n.chainId?"bg-blue-500":"bg-purple-500"),children:n.chainName[0]}),e.jsx("span",{children:n.chainName})]}),s.chainId===n.chainId&&e.jsx("span",{className:"text-sm font-bold text-green-500",children:"已连接"})]},n.chainId))})]})})}function N(e){if(!e||"0x0"===e)return"0 ETH";try{const t=e.startsWith("0x")?e:`0x${e}`,n=BigInt(t),a=1000000000000000000n,s=n/a,r=(n%a).toString().padStart(18,"0").slice(0,4).replace(/0+$/,"");return r?`${s}.${r} ETH`:`${s} ETH`}catch(e){return console.error("Error formatting balance:",e),"0 ETH"}}function v(e){return e?`${e.slice(0,6)}...${e.slice(-4)}`:""}function C({onClose:t}){const{state:n,disconnect:a}=p(),{account:s,balance:r}=n;return e.jsx("div",{className:"wjb-modal-overlay",onClick:e=>{e.target===e.currentTarget&&t()},children:e.jsxs("div",{className:"wjb-account-modal-content",children:[e.jsx("button",{onClick:t,className:"wjb-account-modal-close",children:"✕"}),e.jsx("div",{className:"wjb-account-avatar-large"}),e.jsx("div",{className:"wjb-account-address-large",children:v(s)}),e.jsx("div",{className:"wjb-account-balance-large",children:n.balance||"0 ETH"}),e.jsxs("div",{className:"wjb-account-actions",children:[e.jsxs("button",{onClick:async()=>{s&&await navigator.clipboard.writeText(s)},className:"wjb-action-button",children:[e.jsxs("svg",{width:"24",height:"24",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("rect",{x:"9",y:"9",width:"13",height:"13",rx:"2",ry:"2"}),e.jsx("path",{d:"M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"})]}),"复制地址"]}),e.jsxs("button",{onClick:async()=>{await a(),t()},className:"wjb-action-button text-red-500 hover:text-red-600",children:[e.jsxs("svg",{width:"24",height:"24",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",children:[e.jsx("path",{d:"M9 21H5a2 2 0 0 1-2-2V5a2 2 0 0 1 2-2h4"}),e.jsx("polyline",{points:"16 17 21 12 16 7"}),e.jsx("line",{x1:"21",y1:"12",x2:"9",y2:"12"})]}),"断开连接"]})]})]})})}exports.BaseWallet=c,exports.CoinbaseWallet=h,exports.ConnectButton=function(){const{state:n}=p(),[a,s]=t.useState(!1),[r,i]=t.useState(!1),[c,l]=t.useState(!1);return n.isConnected?e.jsxs(e.Fragment,{children:[e.jsx("div",{className:"wjb-wallet-connected",children:e.jsxs("div",{className:"wjb-wallet-info-group",children:[e.jsxs("button",{onClick:()=>i(!0),className:"wjb-chain-button",children:[e.jsx("div",{className:`w-5 h-5 rounded-full ${"0x1"===n.chainId?"bg-blue-500":"0xaa36a7"===n.chainId?"bg-purple-500":"bg-gray-500"} flex items-center justify-center text-white text-[10px]`,children:"0x1"===n.chainId?"E":"0xaa36a7"===n.chainId?"S":"?"}),e.jsxs("span",{children:["0x1"===n.chainId&&"Ethereum","0xaa36a7"===n.chainId&&"Sepolia",n.chainId&&"0x1"!==n.chainId&&"0xaa36a7"!==n.chainId&&"Unknown"]}),e.jsx("svg",{width:"12",height:"12",viewBox:"0 0 12 12",fill:"none",className:"text-gray-400",children:e.jsx("path",{d:"M2.5 4.5L6 8L9.5 4.5",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round"})})]}),e.jsx("div",{className:"wjb-balance",children:n.balance?N(n.balance):"0 ETH"}),e.jsxs("button",{onClick:()=>l(!0),className:"wjb-account-button",children:[e.jsx("span",{children:n.account&&v(n.account)}),e.jsx("div",{className:"wjb-account-avatar"}),e.jsx("svg",{width:"12",height:"12",viewBox:"0 0 12 12",fill:"none",className:"text-gray-400 ml-1",children:e.jsx("path",{d:"M2.5 4.5L6 8L9.5 4.5",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round"})})]})]})}),r&&e.jsx(j,{onClose:()=>i(!1)}),c&&e.jsx(C,{onClose:()=>l(!1)})]}):e.jsxs(e.Fragment,{children:[e.jsx("button",{onClick:()=>s(!0),className:"wjb-connect-button wjb-connect-button-primary",children:"连接钱包"}),a&&e.jsx(f,{onClose:()=>s(!1)})]})},exports.ErrorCode=s,exports.MetaMaskWallet=l,exports.OKXWallet=o,exports.PhantomWallet=d,exports.WalletProvider=function({children:a,config:i}){const{chains:c=g,autoConnect:l=!0}=i,[o,d]=t.useState({isConnected:!1,account:null,chainId:null,balance:null,wallet:null}),[h,x]=t.useState(null),m=t.useMemo(()=>u.filter(e=>e.isInstalled()).map(e=>e.getInfo()),[]);t.useCallback(async(e,t)=>{try{const n=await e.getBalance(t);d(e=>({...e,balance:n}))}catch(e){console.error("更新余额失败:",e)}},[]);const p=t.useCallback(async e=>{try{if(o.isConnected&&o.wallet?.id===e)throw r("钱包已连接",s.ALREADY_CONNECTED);h&&await h.disconnect();const t=w(e);if(!t)throw r(`未找到钱包 ${e}`,s.PROVIDER_NOT_FOUND);const n=await t.connect(),a=await t.getChainId(),i=await t.getBalance(n[0]),c=t.getInfo();t.on("accountsChanged",async e=>{const n=e;if(0===n.length)f();else{const e=n[0],a=await t.getBalance(e);d(t=>({...t,account:e,balance:a}))}}),t.on("chainChanged",async e=>{const n=(await t.getAccounts())[0];let a="0x0";n&&(a=await t.getBalance(n)),d(t=>({...t,chainId:e,balance:a}))}),t.on("disconnect",()=>{f()}),x(t),d({isConnected:!0,account:n[0],chainId:a,balance:i,wallet:c}),"undefined"!=typeof window&&localStorage.setItem(b,JSON.stringify({walletId:e,account:n[0]}))}catch(e){throw console.error("连接钱包失败:",e),e}},[o.isConnected,o.wallet,h]),f=t.useCallback(async()=>{h&&await h.disconnect(),x(null),d({isConnected:!1,account:null,chainId:null,balance:null,wallet:null}),"undefined"!=typeof window&&localStorage.removeItem(b)},[h]),j=t.useCallback(async e=>{if(!h)throw r("未连接钱包",s.DISCONNECTED);const t=function(e,t){return t.find(t=>t.chainId.toLowerCase()===e.toLowerCase())}(e,c);if(!t)throw r(`不支持链 ${e}`,s.UNRECOGNIZED_CHAIN);await h.switchChain(t)},[h,c]);t.useEffect(()=>{if(!l)return;(async()=>{if("undefined"==typeof window)return;const e=localStorage.getItem(b);if(e)try{const{walletId:t}=JSON.parse(e),n=w(t);if(!n||!n.isInstalled())return void localStorage.removeItem(b);const a=await n.getAccounts();if(a.length>0){const e=await n.getChainId(),t=await n.getBalance(a[0]),s=n.getInfo();n.on("accountsChanged",async e=>{const t=e;if(0===t.length)f();else{const e=t[0],a=await n.getBalance(e);d(t=>({...t,account:e,balance:a}))}}),n.on("chainChanged",async e=>{const t=(await n.getAccounts())[0];let a="0x0";t&&(a=await n.getBalance(t)),d(t=>({...t,chainId:e,balance:a}))}),n.on("disconnect",()=>{f()}),x(n),d({isConnected:!0,account:a[0],chainId:e,balance:t,wallet:s})}else localStorage.removeItem(b)}catch(e){console.error("自动连接失败:",e),localStorage.removeItem(b)}})()},[l]);const N=t.useMemo(()=>({state:o,connect:p,disconnect:f,switchChain:j,availableWallets:m,supportedChains:c}),[o,p,f,j,m,c]);return e.jsx(n.Provider,{value:N,children:a})},exports.defaultChains=g,exports.mainnet=x,exports.sepolia=m,exports.useWallet=p;
11
2
  //# sourceMappingURL=index.js.map