mirror of
https://github.com/Qortal/piratewallet-light-cli.git
synced 2025-02-11 17:55:47 +00:00
ZcashClient.sendToAddress()
This commit is contained in:
parent
96e6f1b5fe
commit
f9361b27d9
@ -34,6 +34,7 @@ $ docker run -d -p 8081:8081 --network=host lightwalletd/envoy
|
|||||||
## Running the demo
|
## Running the demo
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
|
$ ln -s "$HOME/.zcash-params" demo-www/params
|
||||||
$ cd demo-www
|
$ cd demo-www
|
||||||
$ npm run start
|
$ npm run start
|
||||||
```
|
```
|
||||||
|
@ -14,6 +14,19 @@
|
|||||||
<h2 id="zcash-client-address"></h2>
|
<h2 id="zcash-client-address"></h2>
|
||||||
<p>That's your Zcash address!</p>
|
<p>That's your Zcash address!</p>
|
||||||
<h2 id="zcash-client-balance"></h2>
|
<h2 id="zcash-client-balance"></h2>
|
||||||
|
<table id="zcash-client-yes-balance">
|
||||||
|
<tr>
|
||||||
|
<td><label for="zcash-client-send-to-address">To:</label></td>
|
||||||
|
<td><input id="zcash-client-send-to-address" /></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><label for="zcash-client-send-value">Amount:</label></td>
|
||||||
|
<td><input id="zcash-client-send-value" type="number" /></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td><button id="zcash-client-send-action">Send!</button></td>
|
||||||
|
</tr>
|
||||||
|
</div>
|
||||||
<p id="zcash-client-no-balance">You have no TAZ. Go <a href="https://faucet.testnet.z.cash/" target="blank">here</a> to get some!</p>
|
<p id="zcash-client-no-balance">You have no TAZ. Go <a href="https://faucet.testnet.z.cash/" target="blank">here</a> to get some!</p>
|
||||||
<p id="zcash-client-sync-status">Syncing...</p>
|
<p id="zcash-client-sync-status">Syncing...</p>
|
||||||
</div>
|
</div>
|
||||||
|
@ -2,7 +2,11 @@ import { ZcashClient } from 'zcash-client-sdk'
|
|||||||
|
|
||||||
const address = document.getElementById('zcash-client-address')
|
const address = document.getElementById('zcash-client-address')
|
||||||
const balance = document.getElementById('zcash-client-balance')
|
const balance = document.getElementById('zcash-client-balance')
|
||||||
|
const yesBalance = document.getElementById('zcash-client-yes-balance')
|
||||||
const noBalance = document.getElementById('zcash-client-no-balance')
|
const noBalance = document.getElementById('zcash-client-no-balance')
|
||||||
|
const sendToAddress = document.getElementById('zcash-client-send-to-address')
|
||||||
|
const sendValue = document.getElementById('zcash-client-send-value')
|
||||||
|
const sendAction = document.getElementById('zcash-client-send-action')
|
||||||
const syncStatus = document.getElementById('zcash-client-sync-status')
|
const syncStatus = document.getElementById('zcash-client-sync-status')
|
||||||
|
|
||||||
var zcashClient = new ZcashClient('http://localhost:8081', {
|
var zcashClient = new ZcashClient('http://localhost:8081', {
|
||||||
@ -12,8 +16,10 @@ var zcashClient = new ZcashClient('http://localhost:8081', {
|
|||||||
updateBalance: (newBalance) => {
|
updateBalance: (newBalance) => {
|
||||||
balance.textContent = `Balance: ${newBalance} TAZ`
|
balance.textContent = `Balance: ${newBalance} TAZ`
|
||||||
if (newBalance > 0) {
|
if (newBalance > 0) {
|
||||||
|
yesBalance.style.display = ''
|
||||||
noBalance.style.display = 'none'
|
noBalance.style.display = 'none'
|
||||||
} else {
|
} else {
|
||||||
|
yesBalance.style.display = 'none'
|
||||||
noBalance.style.display = ''
|
noBalance.style.display = ''
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -31,6 +37,20 @@ var zcashClient = new ZcashClient('http://localhost:8081', {
|
|||||||
})
|
})
|
||||||
|
|
||||||
zcashClient.load(() => {
|
zcashClient.load(() => {
|
||||||
|
// Register event handlers
|
||||||
|
sendAction.onclick = () => {
|
||||||
|
sendAction.disabled = true
|
||||||
|
sendAction.textContent = 'Sending...'
|
||||||
|
|
||||||
|
var to = sendToAddress.value
|
||||||
|
var value = sendValue.value
|
||||||
|
|
||||||
|
zcashClient.sendToAddress(to, value, () => {
|
||||||
|
sendAction.disabled = false
|
||||||
|
sendAction.textContent = 'Send!'
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// Loading complete, show the wallet
|
// Loading complete, show the wallet
|
||||||
document.getElementById('zcash-client-loading').remove()
|
document.getElementById('zcash-client-loading').remove()
|
||||||
document.getElementById('zcash-client-content').style.display = ''
|
document.getElementById('zcash-client-content').style.display = ''
|
||||||
|
@ -1,12 +1,14 @@
|
|||||||
import { Client } from 'zcash-client-backend-wasm'
|
import { Client } from 'zcash-client-backend-wasm'
|
||||||
|
|
||||||
const { BlockID, BlockRange, ChainSpec } = require('./service_pb.js')
|
const { BlockID, BlockRange, ChainSpec, RawTransaction } = require('./service_pb.js')
|
||||||
const { CompactTxStreamerClient } = require('./service_grpc_web_pb.js')
|
const { CompactTxStreamerClient } = require('./service_grpc_web_pb.js')
|
||||||
const grpc = {}
|
const grpc = {}
|
||||||
grpc.web = require('grpc-web')
|
grpc.web = require('grpc-web')
|
||||||
|
|
||||||
const COIN = 100000000
|
const COIN = 100000000
|
||||||
|
|
||||||
|
const SAPLING_CONSENSUS_BRANCH_ID = 0x76b809bb
|
||||||
|
|
||||||
const CHAIN_REFRESH_INTERVAL = 60 * 1000
|
const CHAIN_REFRESH_INTERVAL = 60 * 1000
|
||||||
const BATCH_SIZE = 1000
|
const BATCH_SIZE = 1000
|
||||||
|
|
||||||
@ -21,6 +23,40 @@ export class ZcashClient {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fetchSpendParams () {
|
||||||
|
var self = this
|
||||||
|
|
||||||
|
var req = new XMLHttpRequest()
|
||||||
|
req.addEventListener('load', function () {
|
||||||
|
var buffer = req.response
|
||||||
|
if (buffer) {
|
||||||
|
self.spendParams = new Uint8Array(buffer)
|
||||||
|
} else {
|
||||||
|
console.error("Didn't receive sapling-spend.params")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
req.open('GET', 'params/sapling-spend.params')
|
||||||
|
req.responseType = 'arraybuffer'
|
||||||
|
req.send()
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchOutputParams () {
|
||||||
|
var self = this
|
||||||
|
|
||||||
|
var req = new XMLHttpRequest()
|
||||||
|
req.addEventListener('load', function () {
|
||||||
|
var buffer = req.response
|
||||||
|
if (buffer) {
|
||||||
|
self.outputParams = new Uint8Array(buffer)
|
||||||
|
} else {
|
||||||
|
console.error("Didn't receive sapling-spend.params")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
req.open('GET', 'params/sapling-output.params')
|
||||||
|
req.responseType = 'arraybuffer'
|
||||||
|
req.send()
|
||||||
|
}
|
||||||
|
|
||||||
updateUI () {
|
updateUI () {
|
||||||
this.uiHandlers.updateBalance(this.client.balance() / COIN)
|
this.uiHandlers.updateBalance(this.client.balance() / COIN)
|
||||||
}
|
}
|
||||||
@ -109,10 +145,47 @@ export class ZcashClient {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sendToAddress (to, value, onFinished) {
|
||||||
|
var self = this
|
||||||
|
|
||||||
|
console.log(`Sending ${value} TAZ to ${to}`)
|
||||||
|
|
||||||
|
var tx = self.client.send_to_address(
|
||||||
|
SAPLING_CONSENSUS_BRANCH_ID,
|
||||||
|
self.spendParams,
|
||||||
|
self.outputParams,
|
||||||
|
to,
|
||||||
|
value * COIN)
|
||||||
|
if (tx == null) {
|
||||||
|
console.error('Failed to create transaction')
|
||||||
|
onFinished()
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var rawTx = new RawTransaction()
|
||||||
|
rawTx.setData(tx)
|
||||||
|
|
||||||
|
console.log('Sending transaction...')
|
||||||
|
self.lightwalletd.sendTransaction(rawTx, {}, (response) => {
|
||||||
|
console.log('Sent transaction')
|
||||||
|
if (response != null) {
|
||||||
|
console.log(`Error code: ${response.getErrorcode()} "${response.getErrormessage()}"`)
|
||||||
|
}
|
||||||
|
|
||||||
|
self.updateUI()
|
||||||
|
|
||||||
|
onFinished()
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
load (onFinished) {
|
load (onFinished) {
|
||||||
var self = this
|
var self = this
|
||||||
|
|
||||||
var loader = () => {
|
var loader = () => {
|
||||||
|
// Fetch Sapling parameters
|
||||||
|
self.fetchSpendParams()
|
||||||
|
self.fetchOutputParams()
|
||||||
|
|
||||||
// Register event handlers
|
// Register event handlers
|
||||||
|
|
||||||
// Initial UI updates
|
// Initial UI updates
|
||||||
|
Loading…
x
Reference in New Issue
Block a user