Skip to content

Commit

Permalink
fix(btc): switch network
Browse files Browse the repository at this point in the history
  • Loading branch information
imtianx committed Apr 11, 2024
1 parent fc4ba21 commit f7225bb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 15 deletions.
4 changes: 2 additions & 2 deletions dist/trust-min.js
Git LFS file not shown
40 changes: 27 additions & 13 deletions src/btc_provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import Utils from "./utils";
const BtcNetworks = {
mainnet: "livenet", // mainnet
testnet: "testnet",
segnit: "segnit",
segnet: "segnet",
}

class TrustBtcWeb3Provider extends BaseProvider {
Expand All @@ -27,7 +27,7 @@ class TrustBtcWeb3Provider extends BaseProvider {
} catch (error) {
console.log(error);
}
if (!(this._network in BtcNetworks)) {
if (!Object.values(BtcNetworks).includes(this._network)) {
this._network = BtcNetworks.mainnet;
}
}
Expand Down Expand Up @@ -62,7 +62,8 @@ class TrustBtcWeb3Provider extends BaseProvider {
account() {
return this._request("requestAccounts", { network: this._network }).then((data) => {
this.setAddress(data[0]);
this.printLog(data)
this.printLog(data);
this.emitAccountChanged(data);
return data;
});
}
Expand All @@ -84,44 +85,57 @@ class TrustBtcWeb3Provider extends BaseProvider {
}

switchNetwork(network) {
if (!(network in BtcNetworks)) {
throw Error(`cur network is: ${network},only support networl is :${BtcNetworks}`)
if (!Object.values(BtcNetworks).includes(network)) {
throw Error(`cur network is: ${network},only support networks are :${Object.keys(BtcNetworks).join(', ')}}`)
}
this._request("switchNetwork", { network: network })
return this._request("switchNetwork", { network: network }).then((response) => {
this.printLog(response);
this._network = response;
this.emitNetworkChanged(response);
return response;
});
}

signMessage(message, type = "ecdsa") {
if (type !== "ecdsa" && type !== "bip322-simple") {
throw Error("Invida params, only support type is: ecdsa | bip322-simple")
throw Error("Invalid params, only support type is: ecdsa | bip322-simple")
}
return this._request("signMessage", { data: message, type: type }).then((response) => {
return response;
});
}

signPsbt(psbtHex) {
return this._request("signPsbt", { tx: psbtHex }).then((response) => {
this.printLog(response)
return this._request("signPsbt", { data: psbtHex }).then((response) => {
this.printLog(response);
return response;
});
}

pushPsbt(psbtHex) {
return this._request("pushPsbt", { tx: psbtHex })
return this._request("pushPsbt", { data: psbtHex })
.then((response) => {
this.printLog(response)
this.printLog(response);
return response
});
}

pushTx(signedTx) {
return this._request("submitTransaction", { tx: signedTx })
return this._request("submitTransaction", { data: signedTx })
.then((response) => {
this.printLog(response)
this.printLog(response);
return response;
});
}

emitAccountChanged(data) {
this.emit("accountsChanged", data);
}

emitNetworkChanged(network) {
this.emit("networkChanged", network);
}

/**
* @private Internal rpc handler
*/
Expand Down

0 comments on commit f7225bb

Please sign in to comment.