-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add bitcoin signer for phantom on hub
- Loading branch information
1 parent
6fd292b
commit 6ed6089
Showing
24 changed files
with
307 additions
and
42 deletions.
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,87 @@ | ||
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 PSBT { | ||
// inputs is list of PSBTInput, which have witnessUtxo or nonWitnessUtxo | ||
inputsToSign: { address: string; signingIndexes: number[] }[]; | ||
// psbt in Base64 | ||
unsignedPsbtBase64: 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; | ||
} | ||
|
||
function base64ToUint8Array(base64String: string) { | ||
const binaryString = atob(base64String); | ||
const length = binaryString.length; | ||
const uint8Array = new Uint8Array(length); | ||
|
||
for (let i = 0; i < length; i++) { | ||
uint8Array[i] = binaryString.charCodeAt(i); | ||
} | ||
|
||
return uint8Array; | ||
} | ||
|
||
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 { 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( | ||
base64ToUint8Array(apiObj.unsignedPsbtBase64), | ||
{ | ||
inputsToSign: apiObj.inputsToSign, | ||
} | ||
); | ||
|
||
// 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.