diff --git a/.github/workflows/build-and-publish-pr.yml b/.github/workflows/build-and-publish-pr.yml index 26fae6c..193bba4 100644 --- a/.github/workflows/build-and-publish-pr.yml +++ b/.github/workflows/build-and-publish-pr.yml @@ -16,6 +16,9 @@ jobs: - name: Install dependencies run: npm ci + - name: Run test build + run: npm run build-debug + - name: Run build run: npm run build diff --git a/package-lock.json b/package-lock.json index bb06925..916c81f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,12 +13,10 @@ "bitcoin-address-validation": "2.2.3", "buffer": "6.0.3", "jsontokens": "4.0.1", - "lodash.omit": "4.5.0", "valibot": "0.33.2" }, "devDependencies": { "@types/jest": "^29.2.6", - "@types/lodash.omit": "4.5.9", "husky": "^8.0.3", "lint-staged": "^13.2.3", "prettier": "3.3.3", @@ -1928,21 +1926,6 @@ "integrity": "sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==", "dev": true }, - "node_modules/@types/lodash": { - "version": "4.17.0", - "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.0.tgz", - "integrity": "sha512-t7dhREVv6dbNj0q17X12j7yDG4bD/DHYX7o5/DbDxobP0HnGPgpRz2Ej77aL7TZT3DSw13fqUTj8J4mMnqa7WA==", - "dev": true - }, - "node_modules/@types/lodash.omit": { - "version": "4.5.9", - "resolved": "https://registry.npmjs.org/@types/lodash.omit/-/lodash.omit-4.5.9.tgz", - "integrity": "sha512-zuAVFLUPJMOzsw6yawshsYGgq2hWUHtsZgeXHZmSFhaQQFC6EQ021uDKHkSjOpNhSvtNSU9165/o3o/Q51GpTw==", - "dev": true, - "dependencies": { - "@types/lodash": "*" - } - }, "node_modules/@types/node": { "version": "18.14.2", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.14.2.tgz", @@ -5035,11 +5018,6 @@ "integrity": "sha512-t7j+NzmgnQzTAYXcsHYLgimltOV1MXHtlOWf6GjL9Kj8GK5FInw5JotxvbOs+IvV1/Dzo04/fCGfLVs7aXb4Ag==", "dev": true }, - "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==" - }, "node_modules/lodash.sortby": { "version": "4.7.0", "resolved": "https://registry.npmjs.org/lodash.sortby/-/lodash.sortby-4.7.0.tgz", @@ -8548,21 +8526,6 @@ "integrity": "sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==", "dev": true }, - "@types/lodash": { - "version": "4.17.0", - "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.17.0.tgz", - "integrity": "sha512-t7dhREVv6dbNj0q17X12j7yDG4bD/DHYX7o5/DbDxobP0HnGPgpRz2Ej77aL7TZT3DSw13fqUTj8J4mMnqa7WA==", - "dev": true - }, - "@types/lodash.omit": { - "version": "4.5.9", - "resolved": "https://registry.npmjs.org/@types/lodash.omit/-/lodash.omit-4.5.9.tgz", - "integrity": "sha512-zuAVFLUPJMOzsw6yawshsYGgq2hWUHtsZgeXHZmSFhaQQFC6EQ021uDKHkSjOpNhSvtNSU9165/o3o/Q51GpTw==", - "dev": true, - "requires": { - "@types/lodash": "*" - } - }, "@types/node": { "version": "18.14.2", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.14.2.tgz", @@ -10832,11 +10795,6 @@ "integrity": "sha512-t7j+NzmgnQzTAYXcsHYLgimltOV1MXHtlOWf6GjL9Kj8GK5FInw5JotxvbOs+IvV1/Dzo04/fCGfLVs7aXb4Ag==", "dev": true }, - "lodash.omit": { - "version": "4.5.0", - "resolved": "https://registry.npmjs.org/lodash.omit/-/lodash.omit-4.5.0.tgz", - "integrity": "sha512-XeqSp49hNGmlkj2EJlfrQFIzQ6lXdNro9sddtQzcJY8QaoC2GO0DT7xaIokHeyM+mIT0mPMlPvkYzg2xCuHdZg==" - }, "lodash.sortby": { "version": "4.7.0", "resolved": "https://registry.npmjs.org/lodash.sortby/-/lodash.sortby-4.7.0.tgz", diff --git a/package.json b/package.json index cd8a554..dd7b8da 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ ], "scripts": { "test": "jest", - "build-debug": "webpack --mode development", + "build-debug": "npm run clean && webpack --mode development", "build": "npm run clean && tsup src/index.ts --format esm --dts", "build:watch": "npm run clean && tsup src/index.ts --format esm --dts --watch", "clean": "rimraf dist", @@ -29,12 +29,10 @@ "bitcoin-address-validation": "2.2.3", "buffer": "6.0.3", "jsontokens": "4.0.1", - "lodash.omit": "4.5.0", "valibot": "0.33.2" }, "devDependencies": { "@types/jest": "^29.2.6", - "@types/lodash.omit": "4.5.9", "husky": "^8.0.3", "lint-staged": "^13.2.3", "prettier": "3.3.3", diff --git a/src/adapters/unisat.ts b/src/adapters/unisat.ts index 2d2390d..2b3cc14 100644 --- a/src/adapters/unisat.ts +++ b/src/adapters/unisat.ts @@ -1,10 +1,11 @@ -import { Buffer } from 'buffer'; -import { Params, Requests, Return } from '../request'; -import { SatsConnectAdapter } from './satsConnectAdapter'; -import { RpcErrorCode, RpcResult } from '../types'; import { AddressType, getAddressInfo } from 'bitcoin-address-validation'; -import { Address, AddressPurpose } from '../addresses'; +import { Buffer } from 'buffer'; +import { AddListener } from 'src/provider/types'; import { DefaultAdaptersInfo } from '.'; +import { Address, AddressPurpose } from '../addresses'; +import { MessageSigningProtocols, Params, Requests, Return } from '../request'; +import { RpcErrorCode, RpcResult } from '../types'; +import { SatsConnectAdapter } from './satsConnectAdapter'; type InputType = { index: number; @@ -14,6 +15,8 @@ type InputType = { disableTweakSigner?: boolean; }[]; +type UnisatEvent = 'accountsChanged' | 'networkChanged'; + type Unisat = { requestAccounts: () => Promise; getAccounts: () => Promise; @@ -32,6 +35,8 @@ type Unisat = { } ) => Promise; pushPsbt: (psbtHex: string) => Promise; + on: (event: UnisatEvent, handler: () => void) => void; + removeListener: (event: UnisatEvent, handler: () => void) => void; }; declare global { @@ -40,10 +45,7 @@ declare global { } } -function convertSignInputsToInputType( - signInputs: Record, - allowedSignHash?: number -): InputType { +function convertSignInputsToInputType(signInputs: Record): InputType { let result: InputType = []; for (let address in signInputs) { let indexes = signInputs[address]; @@ -51,7 +53,6 @@ function convertSignInputsToInputType( result.push({ index: index, address: address, - sighashTypes: allowedSignHash ? [allowedSignHash] : undefined, }); } } @@ -85,10 +86,10 @@ class UnisatAdapter extends SatsConnectAdapter { }; const response: Return<'getAccounts'> = []; if (purposes.includes(AddressPurpose.Payment)) { - response.push(paymentAddress); + response.push({ ...paymentAddress, walletType: 'software' }); } if (purposes.includes(AddressPurpose.Ordinals)) { - response.push(ordinalsAddress); + response.push({ ...ordinalsAddress, walletType: 'software' }); } return response; } @@ -103,6 +104,7 @@ class UnisatAdapter extends SatsConnectAdapter { address, messageHash: '', signature: response, + protocol: MessageSigningProtocols.BIP322, }; } const response = await window.unisat.signMessage(message, 'ecdsa'); @@ -110,6 +112,7 @@ class UnisatAdapter extends SatsConnectAdapter { address, messageHash: '', signature: response, + protocol: MessageSigningProtocols.ECDSA, }; } @@ -126,11 +129,11 @@ class UnisatAdapter extends SatsConnectAdapter { } private async signPsbt(params: Params<'signPsbt'>): Promise> { - const { psbt, signInputs, allowedSignHash, broadcast } = params; + const { psbt, signInputs, broadcast } = params; const psbtHex = Buffer.from(psbt, 'base64').toString('hex'); const signedPsbt = await window.unisat.signPsbt(psbtHex, { autoFinalized: broadcast, - toSignInputs: convertSignInputsToInputType(signInputs, allowedSignHash), + toSignInputs: convertSignInputsToInputType(signInputs), }); if (broadcast) { const txid = await window.unisat.pushPsbt(psbtHex); @@ -204,6 +207,35 @@ class UnisatAdapter extends SatsConnectAdapter { }; } }; + + public addListener: AddListener = (eventName, cb) => { + switch (eventName) { + case 'accountChange': { + const handler = () => { + (cb as (event: { type: 'accountChange' }) => void)({ type: 'accountChange' }); + }; + window.unisat.on('accountsChanged', handler); + + return () => { + window.unisat.removeListener('accountsChanged', handler); + }; + } + case 'networkChange': { + const handler = () => { + (cb as (event: { type: 'networkChange' }) => void)({ type: 'networkChange' }); + }; + window.unisat.on('networkChanged', handler); + + return () => { + window.unisat.removeListener('networkChanged', handler); + }; + } + default: { + console.error('Event not supported by the selected wallet'); + return () => {}; + } + } + }; } export { UnisatAdapter }; diff --git a/src/provider/index.ts b/src/provider/index.ts index 83ac837..a84fc4d 100644 --- a/src/provider/index.ts +++ b/src/provider/index.ts @@ -1,6 +1,5 @@ import { DefaultAdaptersInfo } from '../adapters'; -import { type SupportedWallet, type BitcoinProvider, type Provider } from './types'; -import omit from 'lodash.omit'; +import { type BitcoinProvider, type Provider, type SupportedWallet } from './types'; export async function getProviderOrThrow( getProvider?: () => Promise @@ -41,11 +40,11 @@ export function removeDefaultProvider() { } export function getSupportedWallets(): SupportedWallet[] { - const btc_providers = getProviders(); - const allProviders = [...btc_providers]; - for (const key in omit(DefaultAdaptersInfo, ['xverse'])) { - allProviders.push(DefaultAdaptersInfo[key]); - } + const ambientProviders = getProviders(); + const { xverse, ...defaultProviders } = DefaultAdaptersInfo; + + const allProviders = [...ambientProviders, ...Object.values(defaultProviders)]; + const wallets: SupportedWallet[] = allProviders.map((provider) => { { return { diff --git a/src/request/types/btcMethods.ts b/src/request/types/btcMethods.ts index daddcf0..3ae8f97 100644 --- a/src/request/types/btcMethods.ts +++ b/src/request/types/btcMethods.ts @@ -2,9 +2,9 @@ * Represents the types and interfaces related to BTC methods. */ +import * as v from 'valibot'; import { AddressPurpose, addressSchema } from '../../addresses'; import { MethodParamsAndResult, rpcRequestMessageSchema } from '../../types'; -import * as v from 'valibot'; import { walletTypeSchema } from './common'; export const getInfoMethodName = 'getInfo'; @@ -167,11 +167,6 @@ export type SignPsbtParams = { * The key is the address and the value is an array of indexes of the inputs to sign. */ signInputs: Record; - /** - * the sigHash type to use for signing. - * will default to the sighash type of the input if not provided. - **/ - allowedSignHash?: number; /** * Whether to broadcast the transaction after signing. **/ diff --git a/src/request/types/stxMethods.ts b/src/request/types/stxMethods.ts index 3a88dc6..56511c5 100644 --- a/src/request/types/stxMethods.ts +++ b/src/request/types/stxMethods.ts @@ -1,6 +1,6 @@ -import { addressSchema } from 'src/addresses'; -import { MethodParamsAndResult, rpcRequestMessageSchema } from '../../types'; import * as v from 'valibot'; +import { addressSchema } from '../../addresses'; +import { MethodParamsAndResult, rpcRequestMessageSchema } from '../../types'; interface Pubkey { /** diff --git a/webpack.config.js b/webpack.config.js index 4181af9..72827f3 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -15,12 +15,6 @@ module.exports = { libraryTarget: 'var', path: path.resolve(process.cwd(), 'dist'), }, - plugins: [ - new webpack.ProvidePlugin({ - process: 'process/browser', - fetch: 'cross-fetch', - }), - ], optimization: { minimize: isProduction, },