-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpublish-bch.js
52 lines (43 loc) · 1.73 KB
/
publish-bch.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
This script will write the CID for the current version of the app to an
address on the BCH blockchain. This creates an immutable, censorship-resistant,
globally available, and secure pointer to the latest version of the app.
The exported function expects an IPFS CID as input and returns a TXID for a
BCH transaction.
This function expects this environtment variable to contain a WIF private key
with BCH to write to the blockchain:
- REACT_BOOTSTRAP_WEB3_SPA_WIF
*/
// Global npm libraries
// const BCHJS = require('@psf/bch-js')
const BchWallet = require('minimal-slp-wallet/index')
const BchMessageLib = require('bch-message-lib/index')
async function publishToBch (cid) {
try {
// Get the Filecoin token from the environment variable.
const wif = process.env.BCH_WALLET_WEB3_ANDROID
if (!wif) {
throw new Error(
'WIF private key not detected. Get a private key from https://wallet.fullstack.cash and save it to the BCH_WALLET_WEB3_ANDROID environment variable.'
)
}
// Initialize libraries for working with BCH blockchain.
// const bchjs = new BCHJS()
const wallet = new BchWallet(wif, {
interface: 'consumer-api'
})
await wallet.walletInfoPromise
await wallet.initialize()
const bchMsg = new BchMessageLib({ wallet })
// Publish the CID to the BCH blockchain.
const hex = await bchMsg.memo.memoPush(cid, 'IPFS UPDATE')
// Broadcast the transaction to the network.
const txid = await wallet.ar.sendTx(hex)
// console.log(`BCH blockchain updated with new CID. TXID: ${txid}`)
// console.log(`https://blockchair.com/bitcoin-cash/transaction/${txid}`)
return txid
} catch (err) {
console.error(err)
}
}
module.exports = publishToBch