Skip to content

Commit

Permalink
Merge pull request #43 from secretkeylabs/victor/eng-5319-update-sats…
Browse files Browse the repository at this point in the history
…-connect-schema

Remove allowedSignHash from sign PSBT args + some fixes
  • Loading branch information
m-aboelenein authored Oct 3, 2024
2 parents 02490d9 + 6909bca commit 6f59d4a
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 80 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/build-and-publish-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
42 changes: 0 additions & 42 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
60 changes: 46 additions & 14 deletions src/adapters/unisat.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,6 +15,8 @@ type InputType = {
disableTweakSigner?: boolean;
}[];

type UnisatEvent = 'accountsChanged' | 'networkChanged';

type Unisat = {
requestAccounts: () => Promise<string[]>;
getAccounts: () => Promise<string[]>;
Expand All @@ -32,6 +35,8 @@ type Unisat = {
}
) => Promise<string>;
pushPsbt: (psbtHex: string) => Promise<string>;
on: (event: UnisatEvent, handler: () => void) => void;
removeListener: (event: UnisatEvent, handler: () => void) => void;
};

declare global {
Expand All @@ -40,18 +45,14 @@ declare global {
}
}

function convertSignInputsToInputType(
signInputs: Record<string, number[]>,
allowedSignHash?: number
): InputType {
function convertSignInputsToInputType(signInputs: Record<string, number[]>): InputType {
let result: InputType = [];
for (let address in signInputs) {
let indexes = signInputs[address];
for (let index of indexes) {
result.push({
index: index,
address: address,
sighashTypes: allowedSignHash ? [allowedSignHash] : undefined,
});
}
}
Expand Down Expand Up @@ -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;
}
Expand All @@ -103,13 +104,15 @@ class UnisatAdapter extends SatsConnectAdapter {
address,
messageHash: '',
signature: response,
protocol: MessageSigningProtocols.BIP322,
};
}
const response = await window.unisat.signMessage(message, 'ecdsa');
return {
address,
messageHash: '',
signature: response,
protocol: MessageSigningProtocols.ECDSA,
};
}

Expand All @@ -126,11 +129,11 @@ class UnisatAdapter extends SatsConnectAdapter {
}

private async signPsbt(params: Params<'signPsbt'>): Promise<Return<'signPsbt'>> {
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);
Expand Down Expand Up @@ -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 };
13 changes: 6 additions & 7 deletions src/provider/index.ts
Original file line number Diff line number Diff line change
@@ -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<BitcoinProvider | undefined>
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 1 addition & 6 deletions src/request/types/btcMethods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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<string, number[]>;
/**
* 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.
**/
Expand Down
4 changes: 2 additions & 2 deletions src/request/types/stxMethods.ts
Original file line number Diff line number Diff line change
@@ -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 {
/**
Expand Down
6 changes: 0 additions & 6 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down

0 comments on commit 6f59d4a

Please sign in to comment.