-
Notifications
You must be signed in to change notification settings - Fork 29
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
!WIP feat: add bitcoin signer for phantom on hub #972
Open
RyukTheCoder
wants to merge
1
commit into
next
Choose a base branch
from
feat/add-btc-signer-for-phantom-on-hub
base: next
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+328
−42
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@rango-dev/wallets-core/namespaces/utxo", | ||
"type": "module", | ||
"main": "../../dist/namespaces/utxo/mod.js", | ||
"module": "../../dist/namespaces/utxo/mod.js", | ||
"types": "../../dist/namespaces/utxo/mod.d.ts", | ||
"sideEffects": false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ type RangoNamespace = | |
| 'EVM' | ||
| 'Solana' | ||
| 'Cosmos' | ||
| 'UTXO' | ||
| 'Utxo' | ||
| 'Starknet' | ||
| 'Tron' | ||
| 'Ton'; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { recommended as commonRecommended } from '../common/actions.js'; | ||
|
||
export const recommended = [...commonRecommended]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { recommended as commonRecommended } from '../common/after.js'; | ||
|
||
export const recommended = [...commonRecommended]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { connectAndUpdateStateForSingleNetwork } from '../common/mod.js'; | ||
|
||
export const recommended = [ | ||
['connect', connectAndUpdateStateForSingleNetwork] as const, | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { recommended as commonRecommended } from '../common/before.js'; | ||
|
||
export const recommended = [...commonRecommended]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { UtxoActions } from './types.js'; | ||
|
||
import { ActionBuilder } from '../../mod.js'; | ||
import { intoConnectionFinished } from '../common/after.js'; | ||
import { connectAndUpdateStateForSingleNetwork } from '../common/and.js'; | ||
|
||
export const connect = () => | ||
new ActionBuilder<UtxoActions, 'connect'>('connect') | ||
.and(connectAndUpdateStateForSingleNetwork) | ||
.after(intoConnectionFinished); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const CAIP_NAMESPACE = 'bip122'; | ||
export const CAIP_BITCOIN_CHAIN_ID = '000000000019d6689c085ae165831e93'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
export * as actions from './actions.js'; | ||
export * as after from './after.js'; | ||
export * as and from './and.js'; | ||
export * as before from './before.js'; | ||
export * as builders from './builders.js'; | ||
|
||
export type { ProviderAPI, UtxoActions } from './types.js'; | ||
export { CAIP_NAMESPACE, CAIP_BITCOIN_CHAIN_ID } from './constants.js'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { Accounts } from '../../types/accounts.js'; | ||
import type { | ||
AutoImplementedActionsByRecommended, | ||
CommonActions, | ||
} from '../common/types.js'; | ||
|
||
export interface UtxoActions | ||
extends AutoImplementedActionsByRecommended, | ||
CommonActions { | ||
connect: () => Promise<Accounts>; | ||
} | ||
|
||
export type ProviderAPI = Record<string, any>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
import type { GenericSigner, Transfer } from 'rango-types'; | ||
|
||
import * as secp256k1 from '@bitcoinerlab/secp256k1'; | ||
import { Networks } from '@rango-dev/wallets-shared'; | ||
import axios from 'axios'; | ||
import * as bitcoin from 'bitcoinjs-lib'; | ||
import { SignerError } from 'rango-types'; | ||
|
||
type TransferExternalProvider = any; | ||
|
||
const BTC_RPC_URL = 'https://go.getblock.io/f37bad28a991436483c0a3679a3acbee'; | ||
|
||
interface PSBTInput { | ||
hash: string; | ||
index: number; | ||
witnessUtxo: { | ||
// BigInteger | ||
value: string; | ||
script: string; | ||
} | null; | ||
nonWitnessUtxo: string | null; | ||
} | ||
|
||
interface PSBTOutput { | ||
// BigInteger | ||
value: string; | ||
address?: string; | ||
script?: string; | ||
} | ||
|
||
interface PSBT { | ||
// inputs is list of PSBTInput, which have witnessUtxo or nonWitnessUtxo | ||
psbtInputs: PSBTInput[]; | ||
// ouputs is either PSBTOutputToAddress or PSBTOutputToScript | ||
psbtOutputs: PSBTOutput[]; | ||
// psbt in hex | ||
psbt: string; | ||
} | ||
interface TransferNext extends Transfer { | ||
utxo: any; // TODO: I think this will be removed from server, if not, we can define the type. | ||
psbt: PSBT | null; | ||
} | ||
|
||
const fromHexString = (hexString: string) => | ||
Uint8Array.from( | ||
hexString | ||
.match(/.{1,2}/g) | ||
?.map((byte) => parseInt(byte, 16)) as Iterable<number> | ||
); | ||
|
||
export class BTCSigner implements GenericSigner<Transfer> { | ||
private provider: TransferExternalProvider; | ||
constructor(provider: TransferExternalProvider) { | ||
this.provider = provider; | ||
} | ||
|
||
async signMessage(): Promise<string> { | ||
throw SignerError.UnimplementedError('signMessage'); | ||
} | ||
|
||
async signAndSendTx(tx: TransferNext): Promise<{ hash: string }> { | ||
const { fromWalletAddress, asset, psbt: apiObj } = tx; | ||
|
||
if (!apiObj) { | ||
throw new Error('TODO'); | ||
} | ||
|
||
if (asset.blockchain !== Networks.BTC) { | ||
throw new Error( | ||
`Signing ${asset.blockchain} transaction is not implemented by the signer.` | ||
); | ||
} | ||
// Initialize ECC library | ||
bitcoin.initEccLib(secp256k1); | ||
|
||
const signedPSBTBytes = await this.provider.signPSBT( | ||
fromHexString(apiObj.psbt), | ||
{ | ||
inputsToSign: [ | ||
{ | ||
address: fromWalletAddress, | ||
// TODO: Should we sign all the inputs? | ||
signingIndexes: apiObj.psbtInputs.map((_, i) => i), | ||
}, | ||
], | ||
} | ||
); | ||
|
||
// Finalize PSBT | ||
const finalPsbt = bitcoin.Psbt.fromBuffer(Buffer.from(signedPSBTBytes)); | ||
finalPsbt.finalizeAllInputs(); | ||
|
||
const finalPsbtBaseHex = finalPsbt.extractTransaction().toHex(); | ||
|
||
// Broadcast PSBT to rpc node | ||
const response = await axios.post(BTC_RPC_URL, { | ||
id: 'test', | ||
method: 'sendrawtransaction', | ||
params: [finalPsbtBaseHex], | ||
}); | ||
|
||
if (!response.data.result) { | ||
throw new Error(response.data.error?.message); | ||
} | ||
|
||
return { hash: response.data.result }; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import type { CaipAccount } from '@rango-dev/wallets-core/namespaces/common'; | ||
import type { UtxoActions } from '@rango-dev/wallets-core/namespaces/utxo'; | ||
|
||
import { NamespaceBuilder } from '@rango-dev/wallets-core'; | ||
import { builders as commonBuilders } from '@rango-dev/wallets-core/namespaces/common'; | ||
import { | ||
builders, | ||
CAIP_NAMESPACE, | ||
} from '@rango-dev/wallets-core/namespaces/utxo'; | ||
import { CAIP } from '@rango-dev/wallets-core/utils'; | ||
import { Networks } from '@rango-dev/wallets-shared'; | ||
|
||
import { WALLET_ID } from '../constants.js'; | ||
import { bitcoinPhantom, getBitcoinAccounts } from '../utils.js'; | ||
|
||
const connect = builders | ||
.connect() | ||
.action(async function () { | ||
const bitcoinInstance = bitcoinPhantom(); | ||
const result = await getBitcoinAccounts({ | ||
instance: bitcoinInstance, | ||
meta: [], | ||
}); | ||
if (Array.isArray(result)) { | ||
throw new Error( | ||
'Expecting bitcoin response to be a single value, not an array.' | ||
); | ||
} | ||
|
||
const formatAccounts = result.accounts.map( | ||
(account) => | ||
CAIP.AccountId.format({ | ||
address: account, | ||
chainId: { | ||
namespace: CAIP_NAMESPACE, | ||
reference: Networks.BTC, | ||
}, | ||
}) as CaipAccount | ||
); | ||
|
||
return [formatAccounts[0]]; | ||
}) | ||
.build(); | ||
|
||
const disconnect = commonBuilders.disconnect<UtxoActions>().build(); | ||
|
||
const utxo = new NamespaceBuilder<UtxoActions>('Utxo', WALLET_ID) | ||
.action(connect) | ||
.action(disconnect) | ||
.build(); | ||
|
||
export { utxo }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the wallet doesn't support for all the evms