From 761fb5c73cca5578213cddfa8e0e11552f54a40e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eduard=20Bardaj=C3=AD=20Puig?= Date: Thu, 8 Aug 2024 09:45:29 +0200 Subject: [PATCH] Implement addListener example --- example/package-lock.json | 2 +- example/src/App.tsx | 11 ++++++++- package-lock.json | 50 ++++++++++++++++++++++++++++----------- package.json | 4 ++-- src/index.ts | 12 ++++++++++ 5 files changed, 61 insertions(+), 18 deletions(-) diff --git a/example/package-lock.json b/example/package-lock.json index 1f05c59..eca505b 100644 --- a/example/package-lock.json +++ b/example/package-lock.json @@ -35,7 +35,7 @@ "version": "2.5.0", "license": "ISC", "dependencies": { - "@sats-connect/core": "0.1.2", + "@sats-connect/core": "0.1.2-b66f8be", "@sats-connect/make-default-provider-config": "0.0.5", "@sats-connect/ui": "0.0.6" }, diff --git a/example/src/App.tsx b/example/src/App.tsx index fb836c4..8d5b8e6 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -8,7 +8,7 @@ import { SendStx, } from './components'; import { useLocalStorage } from './hooks'; -import { useCallback } from 'react'; +import { useCallback, useEffect } from 'react'; import GetBtcBalance from './components/GetBtcBalance'; import GetRunesBalance from './components/GetRunesBalance'; import { Container, ConnectButtonsContainer, Header, Logo, Body, Button } from './App.styles'; @@ -34,6 +34,15 @@ function AppWithProviders() { const isConnected = btcAddressInfo.length + stxAddressInfo.length + legacyAddressInfo.length > 0; + useEffect(() => { + if (btcAddressInfo.length < 1) return; + + const removeListener = Wallet.addListener('accountChange', (ev) => { + console.log('The account has changed.', ev); + }); + + return removeListener; + }); const onConnectLegacy = useCallback(() => { (async () => { const response = await Wallet.request('getAccounts', { diff --git a/package-lock.json b/package-lock.json index 8a52f6f..2e1ff2c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,15 @@ { "name": "sats-connect", - "version": "2.6.0", + "version": "2.7.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "sats-connect", - "version": "2.6.0", + "version": "2.7.0", "license": "ISC", "dependencies": { - "@sats-connect/core": "0.1.2", + "@sats-connect/core": "0.1.2-b66f8be", "@sats-connect/make-default-provider-config": "0.0.5", "@sats-connect/ui": "0.0.6" }, @@ -1443,6 +1443,7 @@ "version": "1.4.0", "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.4.0.tgz", "integrity": "sha512-V1JJ1WTRUqHHrOSh597hURcMqVKVGL/ea3kv0gSnEdsEZ0/+VyPghM1lMNGc00z7CIQorSvbKpuJkxvuHbvdbg==", + "license": "MIT", "engines": { "node": ">= 16" }, @@ -1459,7 +1460,8 @@ "type": "individual", "url": "https://paulmillr.com/funding/" } - ] + ], + "license": "MIT" }, "node_modules/@nodelib/fs.scandir": { "version": "2.1.5", @@ -1715,9 +1717,10 @@ ] }, "node_modules/@sats-connect/core": { - "version": "0.1.2", - "resolved": "https://registry.npmjs.org/@sats-connect/core/-/core-0.1.2.tgz", - "integrity": "sha512-SM8U7Bcp5PdtAXw0CNFP3QloU0/xMSupH0FOw65YzWGSklFRoQBq+YCrCZ/shB7fIg1wPFgEFvY5MLQjANl+gw==", + "version": "0.1.2-b66f8be", + "resolved": "https://registry.npmjs.org/@sats-connect/core/-/core-0.1.2-b66f8be.tgz", + "integrity": "sha512-BoWtgY6KbDMjXmpfV8/OTOkO3WE06DZnZLUFPycnST+QwbEc9vD8Q0wcxKPEpgviQZuzr4DpYuP/AE0Mrg0JLw==", + "license": "ISC", "dependencies": { "axios": "1.6.8", "bitcoin-address-validation": "2.2.3", @@ -1991,12 +1994,14 @@ "node_modules/asynckit": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz", - "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==" + "integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==", + "license": "MIT" }, "node_modules/axios": { "version": "1.6.8", "resolved": "https://registry.npmjs.org/axios/-/axios-1.6.8.tgz", "integrity": "sha512-v/ZHtJDU39mDpyBoFVkETcd/uNdxrWRrg3bKpOKzXFA6Bvqopts6ALSMU3y6ijYxbw2B+wPrIv46egTzJXCLGQ==", + "license": "MIT", "dependencies": { "follow-redirects": "^1.15.6", "form-data": "^4.0.0", @@ -2126,6 +2131,7 @@ "version": "1.0.5", "resolved": "https://registry.npmjs.org/base58-js/-/base58-js-1.0.5.tgz", "integrity": "sha512-LkkAPP8Zu+c0SVNRTRVDyMfKVORThX+rCViget00xdgLRrKkClCTz1T7cIrpr69ShwV5XJuuoZvMvJ43yURwkA==", + "license": "MIT", "engines": { "node": ">= 8" } @@ -2147,12 +2153,14 @@ "type": "consulting", "url": "https://feross.org/support" } - ] + ], + "license": "MIT" }, "node_modules/bech32": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/bech32/-/bech32-2.0.0.tgz", - "integrity": "sha512-LcknSilhIGatDAsY1ak2I8VtGaHNhgMSYVxFrGLXv+xLHytaKZKcaUJJUE7qmBr7h33o5YQwP55pMI0xmkpJwg==" + "integrity": "sha512-LcknSilhIGatDAsY1ak2I8VtGaHNhgMSYVxFrGLXv+xLHytaKZKcaUJJUE7qmBr7h33o5YQwP55pMI0xmkpJwg==", + "license": "MIT" }, "node_modules/binary-extensions": { "version": "2.3.0", @@ -2170,6 +2178,7 @@ "version": "2.2.3", "resolved": "https://registry.npmjs.org/bitcoin-address-validation/-/bitcoin-address-validation-2.2.3.tgz", "integrity": "sha512-1uGCGl26Ye8JG5qcExtFLQfuib6qEZWNDo1ZlLlwp/z7ygUFby3IxolgEfgMGaC+LG9csbVASLcH8fRLv7DIOg==", + "license": "MIT", "dependencies": { "base58-js": "^1.0.0", "bech32": "^2.0.0", @@ -2276,6 +2285,7 @@ "url": "https://feross.org/support" } ], + "license": "MIT", "dependencies": { "base64-js": "^1.3.1", "ieee754": "^1.2.1" @@ -2555,6 +2565,7 @@ "version": "1.0.8", "resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz", "integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==", + "license": "MIT", "dependencies": { "delayed-stream": "~1.0.0" }, @@ -2666,6 +2677,7 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz", "integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==", + "license": "MIT", "engines": { "node": ">=0.4.0" } @@ -2943,6 +2955,7 @@ "url": "https://github.com/sponsors/RubenVerborgh" } ], + "license": "MIT", "engines": { "node": ">=4.0" }, @@ -2984,6 +2997,7 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz", "integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==", + "license": "MIT", "dependencies": { "asynckit": "^0.4.0", "combined-stream": "^1.0.8", @@ -3203,7 +3217,8 @@ "type": "consulting", "url": "https://feross.org/support" } - ] + ], + "license": "BSD-3-Clause" }, "node_modules/ignore": { "version": "5.3.1", @@ -4134,6 +4149,7 @@ "version": "4.0.1", "resolved": "https://registry.npmjs.org/jsontokens/-/jsontokens-4.0.1.tgz", "integrity": "sha512-+MO415LEN6M+3FGsRz4wU20g7N2JA+2j9d9+pGaNJHviG4L8N0qzavGyENw6fJqsq9CcrHOIL6iWX5yeTZ86+Q==", + "license": "MIT", "dependencies": { "@noble/hashes": "^1.1.2", "@noble/secp256k1": "^1.6.3", @@ -4444,7 +4460,8 @@ "node_modules/lodash.omit": { "version": "4.5.0", "resolved": "https://registry.npmjs.org/lodash.omit/-/lodash.omit-4.5.0.tgz", - "integrity": "sha512-XeqSp49hNGmlkj2EJlfrQFIzQ6lXdNro9sddtQzcJY8QaoC2GO0DT7xaIokHeyM+mIT0mPMlPvkYzg2xCuHdZg==" + "integrity": "sha512-XeqSp49hNGmlkj2EJlfrQFIzQ6lXdNro9sddtQzcJY8QaoC2GO0DT7xaIokHeyM+mIT0mPMlPvkYzg2xCuHdZg==", + "license": "MIT" }, "node_modules/lodash.sortby": { "version": "4.7.0", @@ -4617,6 +4634,7 @@ "version": "1.52.0", "resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz", "integrity": "sha512-sPU4uV7dYlvtWJxwwxHD0PuihVNiE7TyAbQ5SWxDCB9mUYvOgroQOwYQQOKPJ8CIbE+1ETVlOoK1UC2nU3gYvg==", + "license": "MIT", "engines": { "node": ">= 0.6" } @@ -4625,6 +4643,7 @@ "version": "2.1.35", "resolved": "https://registry.npmjs.org/mime-types/-/mime-types-2.1.35.tgz", "integrity": "sha512-ZDY+bPm5zTTF+YpCrAU9nK0UgICYPT0QtT1NZWFv4s++TNkcgVaT0g6+4R2uI4MjQjzysHB1zxuWL50hzaeXiw==", + "license": "MIT", "dependencies": { "mime-db": "1.52.0" }, @@ -5081,7 +5100,8 @@ "node_modules/proxy-from-env": { "version": "1.1.0", "resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz", - "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==" + "integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==", + "license": "MIT" }, "node_modules/punycode": { "version": "2.3.1", @@ -5332,7 +5352,8 @@ "node_modules/sha256-uint8array": { "version": "0.10.7", "resolved": "https://registry.npmjs.org/sha256-uint8array/-/sha256-uint8array-0.10.7.tgz", - "integrity": "sha512-1Q6JQU4tX9NqsDGodej6pkrUVQVNapLZnvkwIhddH/JqzBZF1fSaxSWNY6sziXBE8aEa2twtGkXUrwzGeZCMpQ==" + "integrity": "sha512-1Q6JQU4tX9NqsDGodej6pkrUVQVNapLZnvkwIhddH/JqzBZF1fSaxSWNY6sziXBE8aEa2twtGkXUrwzGeZCMpQ==", + "license": "MIT" }, "node_modules/shebang-command": { "version": "2.0.0", @@ -6009,6 +6030,7 @@ "version": "0.33.2", "resolved": "https://registry.npmjs.org/valibot/-/valibot-0.33.2.tgz", "integrity": "sha512-ZpFWuI+bs5+PP66q4zVFn4e4t/s5jmMw5iPBZmGUoi8iQqXyU9YY/BLCAyk62Z/bNS8qdUNBEyx52952qdqW3w==", + "license": "MIT", "peer": true }, "node_modules/walker": { diff --git a/package.json b/package.json index ea82eec..3cccf1c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sats-connect", - "version": "2.6.0", + "version": "2.7.0", "main": "dist/index.mjs", "files": [ "dist" @@ -24,7 +24,7 @@ ] }, "dependencies": { - "@sats-connect/core": "0.1.2", + "@sats-connect/core": "0.1.2-b66f8be", "@sats-connect/make-default-provider-config": "0.0.5", "@sats-connect/ui": "0.0.6" }, diff --git a/src/index.ts b/src/index.ts index 1743d4b..239037b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -11,6 +11,7 @@ import { removeDefaultProvider, RpcErrorCode, BaseAdapter, + type AddListener, } from '@sats-connect/core'; import { Config, @@ -107,6 +108,17 @@ class Wallet { } return response; } + + public addListener: AddListener = (event, cb) => { + if (!this.isProviderSet()) { + throw new Error( + 'No wallet provider selected. The user must first select a wallet before adding listeners to wallet events.' + ); + } + + const adapter = this.defaultAdapters[this.providerId as string]; + return new adapter().addListener(event, cb); + }; } export * from '@sats-connect/core';