UNPKG

bitmask-core

Version:

Core functionality for the BitMask wallet

705 lines (592 loc) 16.1 kB
// Methods meant to work with RGB contracts defined within the web::rgb module from bitmask-core: // https://github.com/diba-io/bitmask-core/blob/development/src/web.rs import * as BMC from "./bitmask_core"; export const getVersion = (): string => JSON.parse(BMC.get_rgb_version()); export const getContract = async ( nostrHexSk: string, request: string ): Promise<ContractResponse> => JSON.parse(await BMC.get_contract(nostrHexSk, request)); export const listContracts = async ( nostrHexSk: string, hidden: boolean ): Promise<ContractResponse[]> => JSON.parse(await BMC.list_contracts(nostrHexSk, hidden)); export const importContract = async ( nostrHexSk: string, request: string ): Promise<ContractResponse> => JSON.parse(await BMC.import_contract(nostrHexSk, request)); export const issueContract = async ( nostrHexSk: string, request: IssueRequest ): Promise<IssueResponse> => JSON.parse(await BMC.issue_contract_proxy(nostrHexSk, request)); export const createInvoice = async ( nostrHexSk: string, request: RgbInvoiceRequest ): Promise<RgbInvoiceResponse> => JSON.parse(await BMC.create_rgb_invoice(nostrHexSk, request)); export const canCreateTransfer = async ( nostrHexSk: string, request: RgbTransferRequest ): Promise<RgbValidationResponse> => JSON.parse(await BMC.can_create_transfer_contract(nostrHexSk, request)); export const createTransfer = async ( nostrHexSk: string, request: RgbTransferRequest ): Promise<RgbTransferResponse> => JSON.parse(await BMC.create_rgb_transfer(nostrHexSk, request)); export const createAndPublishTransfer = async ( nostrHexSk: string, request: RgbTransferRequest, secrets: string[] ): Promise<RgbTransferResponse> => JSON.parse(await BMC.create_and_publish_rgb_transfer(nostrHexSk, request, secrets)); export const acceptTransfer = async ( nostrHexSk: string, request: AcceptRequest ): Promise<AcceptResponse> => JSON.parse(await BMC.accept_transfer(nostrHexSk, request)); export const removeInvoice = async ( nostrHexSk: string, request: string ): Promise<void> => { await BMC.remove_rgb_invoice(nostrHexSk, request); }; export const listInvoices = async ( nostrHexSk: string, ): Promise<RgbInvoiceStatusResponse[]> => JSON.parse(await BMC.list_rgb_invoices(nostrHexSk)); export const saveTransfer = async ( nostrHexSk: string, request: SaveTransferRequest ): Promise<RgbTransferItem> => JSON.parse(await BMC.save_transfer(nostrHexSk, request)); export const listTransfers = async ( nostrHexSk: string, ): Promise<RgbTransferItem[]> => JSON.parse(await BMC.list_transfers(nostrHexSk)); export const verifyTransfers = async ( nostrHexSk: string ): Promise<RgbTransferItem[]> => JSON.parse(await BMC.verify_transfers(nostrHexSk)); export const listOffers = async ( nostrHexSk: string, ): Promise<RgbOfferResponse[]> => JSON.parse(await BMC.list_offers(nostrHexSk)); export const offers = async ( nostrHexSk: string, ): Promise<RgbOfferResponse[]> => JSON.parse(await BMC.offers(nostrHexSk)); export const bids = async ( nostrHexSk: string, ): Promise<WatcherWalletResponse> => JSON.parse(await BMC.bids(nostrHexSk)); export const offerSwapSuppl = async ( nostrHexSk: string, offerId: string, ): Promise<SwapSupplement> => JSON.parse(await BMC.offer_swap_suppl(nostrHexSk, offerId)); export const bidSwapSuppl = async ( nostrHexSk: string, bidId: string, ): Promise<SwapSupplement> => JSON.parse(await BMC.bid_swap_suppl(nostrHexSk, bidId)); export const canCreateOffer = async ( nostrHexSk: string, request: RgbOfferRequest, secrets: string[], ): Promise<RgbValidationResponse> => JSON.parse(await BMC.can_create_offer(nostrHexSk, request, secrets)); export const createOffer = async ( nostrHexSk: string, request: RgbOfferRequest, secrets: string[], ): Promise<RgbOfferResponse> => JSON.parse(await BMC.create_offer(nostrHexSk, request, secrets)); export const cancelOffer = async ( nostrHexSk: string, request: RgbCancelOffersRequest, ): Promise<RgbSwapCancelResponse> => JSON.parse(await BMC.cancel_offer(nostrHexSk, request)); export const canCreateBid = async ( nostrHexSk: string, request: RgbBidRequest, secrets: string[], ): Promise<RgbValidationResponse> => JSON.parse(await BMC.can_create_bid(nostrHexSk, request, secrets)); export const createBid = async ( nostrHexSk: string, request: RgbBidRequest, secrets: string[], ): Promise<RgbBidResponse> => JSON.parse(await BMC.create_bid(nostrHexSk, request, secrets)); export const cancelBid = async ( nostrHexSk: string, request: RgbCancelBidsRequest, ): Promise<RgbSwapCancelResponse> => JSON.parse(await BMC.cancel_bid(nostrHexSk, request)); // BITCOIN export const backupWallet = async ( nostrHexSk: string, ): Promise<boolean> => JSON.parse(await BMC.backup_rgb_data(nostrHexSk)); export const restoreWallet = async ( nostrHexSk: string, ): Promise<boolean> => JSON.parse(await BMC.restore_rgb_data(nostrHexSk)); export const getRgbWallet = async ( nostrHexSk: string, ): Promise<WatcherWalletResponse> => JSON.parse(await BMC.get_rgb_wallet(nostrHexSk)); export const decodeInvoice = async ( invoice: string ): Promise<RgbInvoiceDecoded> => JSON.parse(await BMC.decode_rgb_invoice(invoice)); export const psbtPublishFile = async ( request: PublishPsbtRequest ): Promise<SignedPsbtResponse> => JSON.parse(await BMC.psbt_publish_file(request)); export const psbtSignAndPublishFile = async ( _nostrHexSk: string, request: SignPsbtRequest ): Promise<PublishedPsbtResponse> => JSON.parse(await BMC.psbt_sign_and_publish_file(request)); export const toContractAmountRaw = ( decimal: string, precision: number ): string => JSON.parse(BMC.convert_contract_amount_raw(decimal, precision)); export const toContractAmountStr = ( amount: bigint, precision: number ): string => JSON.parse(BMC.convert_contract_amount_string(amount, precision)); export const toContractAmount = (amount: string): ContractAmount => JSON.parse(BMC.parse_contract_amount(amount)); export const toContractPrecision = ( amount: string, precision: number ): string => JSON.parse(BMC.parse_contract_amount_precision(amount, precision)); // Core type interfaces based on structs defined within the bitmask-core Rust crate: // https://github.com/diba-io/bitmask-core/blob/development/src/structs.rs export interface RgbContainer { rgbStock?: string; rgbAccount?: string; rgbTransfers?: string; } export interface IssueRequest { /// The ticker of the asset ticker: string; /// Name of the asset name: string; /// The name of the iface (ex: RGB20) iface: string; /// Description of the asset description: string; /// Amount of the asset supply: string; /// Precision of the asset precision: number; /// Seal of the initial owner seal: string; /// Chain Target /// enum ChainTarget { /// "bitcoin" /// } chain: string; /// contract metadata (only RGB21/UDA) meta?: IssueMedia; } export type ChainFee = { value?: bigint; feeRate?: number; none?: string; }; export interface IssueMedia { /// Preview of the uda preview?: MediaData; /// Media of the uda media?: MediaData; /// Attachments of the uda attachments: MediaData[]; } export interface MediaData { /// Mime Type of the media type: string; /// Source (aka. hyperlink) of the media uri: string; } export type ContractResponse = IssueResponse; export interface IssueResponse { /// The contract id contractId: string; /// The contract interface iface: string; /// The Issue Utxo issueUtxo: string; /// The ticker of the asset ticker: string; /// Name of the asset name: string; /// creation date (timestamp) created: bigint; /// Description of the asset description: string; /// Amount of the asset supply: bigint; /// Precision of the asset precision: number; /// Amount of the asset balance: ContractValue; /// The contract state (multiple formats) contract: string; /// The contract allocations allocations: ContractAllocation[]; /// attachments and media (only RGB21/UDA) meta?: UDAItem; } export type ContractValue = { value?: bigint; uda?: UDAValue; }; export interface UDAValue { tokenIndex: number; fraction: bigint; } export interface ContractAllocation { /// Anchored UTXO utxo: string; /// Asset Value value: ContractValue; /// Derivation Path derivation: string; /// Derivation Path isMine: boolean; /// Allocation spent? isSpent: boolean; } export interface UDAItem { tokenIndex: number; media: IssueMedia; } export interface RgbInvoiceRequest { /// The contract id contractId?: string; /// The contract interface iface?: string; /// Amount of the asset (set to "0" for blinded invoices when the sender will provide amount at transfer time) amount: RgbInvoiceValue; /// Seal Selection seal: RgbSelection; /// Invoice Experation Date expireAt?: number; } export interface RgbSelection { /// Chain UTXO next_utxo?: string; /// Chain UTXO next_address?: string; /// Chain UTXO utxo?: string; /// Chain Address address?: string; } export interface RgbInvoiceValue { /// Decimal representation value?: string; // Allocation representation uda?: UDAValue; } export interface RgbInvoiceResponse { /// Invoice encoded in Baid58 invoice: string; } export interface RgbInvoiceStatusResponse { /// Asset Name assetName: String, /// Asset Ticker assetTicker: String, /// ContractID contractId: String, /// Invoice Amount amount: RgbInvoiceValue, /// Invoice Status status: RgbInvoiceStatus, /// Invoice Encoded invoice: String, } export type RgbInvoiceStatusItem = RgbInvoiceStatusResponse; export interface RgbInvoiceStatus { opened?: string; payed?: string; canceled?: string; } export interface RgbTransferRequest { /// The contract id contractId: string; /// The contract interface iface: string; /// RGB Invoice invoice: string; /// Optional amount provided by the sender for blinded invoices (ignored if invoice carries amount/allocation) senderAmount?: RgbInvoiceValue; /// Chain Target /// enum ChainTarget { /// "bitcoin" /// } chain: string; /// Bitcoin Fee chainFee: ChainFee; } export interface RgbTransferResponse { /// Consignment ID (in base58) consigId: string; /// Consignment encoded (in armored) consig: string; /// Transfer Bitcoin L1 transaction id txid: string; /// PSBT File (in base64) psbt: string; /// Tapret Commitment commit?: string; } export interface AcceptRequest { /// Consignment encoded in base58 consignment: string; /// Force Consignment accept force: boolean; } export interface SaveTransferRequest { /// Consignment encoded in base58 consignment: string; } export interface AcceptResponse { /// Transfer ID transferId: string; /// Contract ID contractId: string; /// Transfer accept status valid: boolean; /// Anchor Bitcoin transaction id txid: string; } export interface RgbTransferItem { contractId: String; iface: String; direction: Direction } export interface Direction { in?: TransferIn; out?: TransferOut; } export interface TransferIn { txId: String; txStatus: TxStatus; invoice: String; transfer?: TransferItem; amount?: ContractAmount; } export interface TransferOut { txId: String; txStatus: TxStatus; invoice: String; transfer: TransferItem; amount?: ContractAmount; } export interface TransferItem{ transferId: String; transfer: String; isAccept: boolean; } export interface SignPsbtRequest { /// PSBT encoded in Base64 psbt: string; /// Descriptors to Sign descriptors: string[]; } export interface SignedPsbtResponse { /// PSBT is signed? sign: boolean; /// PSBT signed psbt: string; } export interface PublishPsbtRequest { /// PSBT encoded in Base64 psbt: string; } export interface PublishedPsbtResponse { /// PSBT is signed? sign: boolean; /// TX id txid: string; } export interface WatcherRequest { /// The watcher name name: string; /// The xpub will be watch btcXpub: string; /// The xpub will be watch rgbXpub: string; /// Recover mode recover: boolean; } export interface WatcherResponse { /// The watcher name name: string; /// network network: string; /// migrate? migrate: boolean; } export interface WatcherStatusResponse { /// The watcher name name: string; /// migrate? created: boolean; } export interface WatcherStatusResponse { /// The watcher name name: string; /// migrate? created: boolean; } export interface WatcherWalletResponse { nextUtxo?: string; nextUtxoValue?: number; nextAddress?: string; listContracts: ContractResponse[]; transfers: RgbTransferItem[]; invoices: RgbInvoiceStatusItem[]; } export interface NextUtxoResponse { utxo?: string; value?: number; } export interface NextAddressResponse { address: string; network: string; } export interface TxStatus { notFound?: any; error?: string; mempool?: any; block?: { height: number; timestamp: number; }; confirmed?: string; } export interface RgbInvoiceDecoded { contractId: string; amount: RgbInvoiceValue; } export interface ContractAmount { int: number; fract: number; precision: number; } export interface RgbOfferRequest { /// The Offer Strategy strategy: RgbSwap, /// Offers offers: RgbOfferItem[], } export enum RgbSwap { auction = "auction", p2p = "p2p", hotSwap = "hotswap", airdrop = "airdrop" } export interface RgbOfferItem { /// The Contract ID contractId: string, /// Contract Amount contractAmount: string, /// Bitcoin Price (in sats) counterParty: OrderValue, /// Offer Expire Date expireAt?: number } export interface RgbOfferResponse { /// The Order ID id: string, /// Order Status status: string, /// The Contract Kit contract: ContractKit, /// Contract Amount amount: ContractAmount, /// Counterparty counterParty: OrderValue } export interface RgbBidRequest { /// The Offer ID offerId: string, /// Order Status assetPrice: OrderValue, /// L1 Fee fee: ChainFee, /// Counterparty bundle?: string, } export interface RgbBidResponse { /// The Order ID id: string, /// The Offer ID offerId: string, /// Order Status status: string, /// The Asset Kit asset: AssetKit, /// Order Amount amount: ContractAmount, /// Counterparty counter_party: OrderValue, /// Anchor Bitcoin transaction id (non-optional; present on successful bid) txid: string, } export interface RgbAuctionCloseRequest { /// Bundle ID bundleId: string } export interface RgbSwapRequest { /// The Offer ID offerId: string, /// The Bid ID bidId: string, /// The Share Key sharedKey: string } export interface RgbSwapResponse { /// Consignment ID (in base58) consigId: string, /// Consignment (in armored) consig: string, /// L1 Transaction ID txid: string, /// PSBT File (in base64) psbt: string } export interface RgbCancelOffersRequest { offers: string[], } export interface RgbCancelBidsRequest { bids: string[], } export interface RgbSwapCancelResponse { records: number, offers: string[], bids: string[], } export interface RgbValidationResponse { valid: boolean, message: string, warnings: string[], failures:string[], } export interface ContractKit { info: ContractInfo, armored: string } export interface ContractInfo { contractId: string, contractType: string, name: string, ticker: string, precision: number, } export interface AssetKit { bitcoin?: string, contract?: ContractKit, } export interface OrderValue { bitcoin?: BigInt, contract?: ContractCounterParty, } export interface ContractCounterParty { kit: ContractKit, amount: ContractAmount } export interface SwapSupplement { transferId: string, transfer: string, tapret: string, psbt: string, }