Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add xverse wallet support for pegin #698

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module.exports = {
transform: {
'^.+\\.vue$': '@vue/vue3-jest',
'^.+\\.(mts|mjs|jsx|ts|tsx)$': 'ts-jest',
},
preset: '@vue/cli-plugin-unit-jest/presets/typescript-and-babel',
collectCoverage: true,
collectCoverageFrom: ['src/(common|pegin)/(providers|services|utils)/*.ts'],
Expand Down
138 changes: 127 additions & 11 deletions package-lock.json

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

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"moment": "^2.29.4",
"os-browserify": "^0.3.0",
"process": "^0.11.10",
"sats-connect": "2.3.x",
"stackjs": "^1.0.0",
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
Expand Down
Binary file added src/assets/wallet-icons/xverse-white.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/wallet-icons/xverse.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/common/components/exchange/SelectBitcoinWallet.vue
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ export default {
case constants.WALLET_NAMES.LEATHER.long_name:
wallet = constants.WALLET_NAMES.LEATHER.short_name;
break;
case constants.WALLET_NAMES.XVERSE.long_name:
wallet = constants.WALLET_NAMES.XVERSE.short_name;
break;
default:
wallet = '';
break;
Expand Down
144 changes: 144 additions & 0 deletions src/common/services/XverseService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/* eslint-disable class-methods-use-this */
import Wallet, { AddressPurpose, BitcoinNetworkType } from 'sats-connect';
import * as bitcoin from 'bitcoinjs-lib';
import { WalletService } from '@/common/services/index';
import * as constants from '@/common/store/constants';
import { XverseTx } from '@/pegin/middleware/TxBuilder/XverseTxBuilder';
import {
WalletAddress, Tx, SignedTx, BtcAccount, Step,
} from '../types';

export default class XverseService extends WalletService {
satsBtcNetwork: BitcoinNetworkType;

constructor() {
super();
switch (this.network) {
case constants.BTC_NETWORK_MAINNET:
this.satsBtcNetwork = BitcoinNetworkType.Mainnet;
break;
default:
this.satsBtcNetwork = BitcoinNetworkType.Testnet;
break;
}
}

getAccountAddresses(): Promise<WalletAddress[]> {
return new Promise<WalletAddress[]>((resolve, reject) => {
const walletAddresses: WalletAddress[] = [];
const payload = {
purposes: ['payment'] as AddressPurpose[],
message: 'Welcome to the 2wp-app, please select your Bitcoin account to start.',
network: {
type: this.satsBtcNetwork,
},
};
Wallet.request('getAddresses', payload)
.then((response) => {
if (response.status === 'error') {
reject(new Error(response.error.message));
} else {
response.result.addresses
.forEach((addr: { address: string; publicKey: string; }) => {
walletAddresses.push({
address: addr.address,
publicKey: addr.publicKey,
derivationPath: '',
});
});
}
resolve(walletAddresses);
})
.catch(reject);
});
}

sign(tx: Tx): Promise<SignedTx> {
const xverseTx = tx as XverseTx;
return new Promise<SignedTx>((resolve, reject) => {
const signInputs: Record<string, number[]> = {};
xverseTx.inputs.forEach((input: { address: string; idx: number; }, inputIdx) => {
if (signInputs[input.address]) {
signInputs[input.address].push(inputIdx);
} else {
signInputs[input.address] = [inputIdx];
}
});
const signPsbtOptions = {
psbt: xverseTx.base64UnsignedPsbt,
signInputs,
broadcast: false,
};
Wallet.request('signPsbt', signPsbtOptions)
.then((response) => {
if (response.status === 'error') {
reject(new Error(response.error.message));
} else {
const signedPsbt = bitcoin.Psbt.fromBase64(response.result.psbt as string);
if (!signedPsbt.validateSignaturesOfAllInputs()) {
reject(new Error('Invalid signature provided'));
} else {
resolve({
signedTx: signedPsbt.finalizeAllInputs().extractTransaction().toHex(),
});
}
}
})
.catch(() => reject(new Error('Invalid psbt provided')));
});
}

isConnected(): Promise<boolean> {
return Promise.resolve(true);
}

reconnect(): Promise<void> {
return new Promise<void>((resolve, reject) => {
this.getAccountAddresses()
.then(() => resolve())
.catch(reject);
});
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
getXpub(accountType: BtcAccount, accountNumber: number): Promise<string> {
throw new Error('Method not supported.');
}

areEnoughUnusedAddresses(): boolean {
return this.addressesToFetch.segwit.lastIndex >= 1;
}

availableAccounts(): BtcAccount[] {
return [constants.BITCOIN_SEGWIT_ADDRESS];
}

name(): Record<'formal_name' | 'short_name' | 'long_name', string> {
return constants.WALLET_NAMES.XVERSE;
}

confirmationSteps(): Step[] {
return [
{
title: 'Transaction information',
subtitle: '',
outputsToshow: {
opReturn: {
value: false,
amount: true,
},
change: {
address: true,
amount: true,
},
federation: {
address: true,
amount: true,
},
},
fullAmount: false,
fee: true,
},
];
}
}
1 change: 1 addition & 0 deletions src/common/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@ export { default as TrezorService } from './TrezorService';
export { default as LedgerService } from './LedgerService';
export { default as LiqualityService } from './LiqualityService';
export { default as LeatherService } from './LeatherService';
export { default as XverseService } from './XverseService';
Loading
Loading