-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- improved getting started - added Cauldron DEX to examples, remove transferWithTimeout - moved operators section to types page - improved notice on pragma usage - improved optimization guide - added a guide page for walletconnect - add SignatureAlgorithm to the SignatureTemplate docs - separated the SignatureTemplate docs to a separate page - Improved the 'Contract Instantiation' page sub titles
- Loading branch information
Showing
10 changed files
with
318 additions
and
141 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
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,93 @@ | ||
--- | ||
title: WalletConnect | ||
--- | ||
|
||
The BCH WalletConnect specification was created in July of 2023 and has become popular since. The standard integrates nicely with CashScript contract development as it was designed with CashScript contract usage in mind. | ||
|
||
You can find a full list of Bitcoin Cash dapps supporting WalletConnect on [Tokenaut.cash](https://tokenaut.cash/dapps?filter=walletconnect). | ||
|
||
:::tip | ||
The specification is called ['wc2-bch-bcr'](https://github.com/mainnet-pat/wc2-bch-bcr) and has extra discussion and info on the [BCH research forum](https://bitcoincashresearch.org/t/wallet-connect-v2-support-for-bitcoincash/). | ||
::: | ||
|
||
## signTransaction Interface | ||
|
||
Most relevant for smart contract usage is the BCH-WalletConnect `signTransaction` interface. | ||
|
||
> This is a most generic interface to propose a bitcoincash transaction to a wallet which reconstructs it and signs it on behalf of the wallet user. | ||
```typescript | ||
signTransaction: ( | ||
options: { | ||
transaction: string | TransactionBCH, | ||
sourceOutputs: (Input | Output | ContractInfo)[], | ||
broadcast?: boolean, | ||
userPrompt?: string | ||
} | ||
) => Promise<{ signedTransaction: string, signedTransactionHash: string } | undefined>; | ||
``` | ||
|
||
Important to note is how the wallet knows which inputs to sign: | ||
|
||
>To signal that the wallet needs to sign an input, the app sets the corresponding input's `unlockingBytecode` to empty Uint8Array. | ||
## signTransaction Example | ||
|
||
### Create wcTransactionObj | ||
|
||
Example code from the 'Cash-Ninjas' minting dapp repository, [link to source code](https://github.com/cashninjas/ninjas.cash/blob/main/js/mint.js). | ||
|
||
```javascript | ||
import { Contract } from "cashscript"; | ||
import { hexToBin, decodeTransaction } from "@bitauth/libauth"; | ||
|
||
async function transactionWalletConnect(){ | ||
// use the CashScript SDK to build a transaction | ||
const rawTransactionHex = await transaction.build(); | ||
const decodedTransaction = decodeTransaction(hexToBin(rawTransactionHex)); | ||
|
||
// set input unlockingBytecode to empty Uint8Array for wallet signing | ||
decodedTransaction.inputs[1].unlockingBytecode = Uint8Array.from([]); | ||
|
||
// construct list SourceOutputs, see source code | ||
const listSourceOutputs = constructSourceOuputs(decodedTransaction, utxoInfo, contract) | ||
|
||
// wcTransactionObj to pass to signTransaction endpoint | ||
const wcTransactionObj = { | ||
transaction: decodedTransaction, | ||
sourceOutputs: listSourceOutputs, | ||
broadcast: true, | ||
userPrompt: "Mint Cash-Ninja NFT" | ||
}; | ||
|
||
// pass wcTransactionObj to WalletConnect client | ||
const signResult = await signTransaction(wcTransactionObj); | ||
|
||
// Handle signResult success / failure | ||
} | ||
``` | ||
|
||
### signTransaction Client interaction | ||
|
||
Below is an implementation of `signTransaction` used earlier, this is where the communication with the client for 'signTransaction' takes place. See the source code for `signClient`, `connectedChain` and `session` details. | ||
|
||
```javascript | ||
import SignClient from '@walletconnect/sign-client'; | ||
import { stringify } from "@bitauth/libauth"; | ||
|
||
async function signTransaction(options){ | ||
try { | ||
const result = await signClient.request({ | ||
chainId: connectedChain, | ||
topic: session.topic, | ||
request: { | ||
method: "bch_signTransaction", | ||
params: JSON.parse(stringify(options)), | ||
}, | ||
}); | ||
return result; | ||
} catch (error) { | ||
return undefined; | ||
} | ||
} | ||
``` |
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.