mirror of
https://github.com/Permissionless-Software-Foundation/psf-memo-client.git
synced 2026-09-21 16:52:02 -07:00
feat(publish): Publishing to BCH
This commit is contained in:
+29
-77
@@ -2,99 +2,51 @@
|
||||
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-resistent,
|
||||
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
|
||||
*/
|
||||
|
||||
const FILE_PATH = './build'
|
||||
|
||||
const { Web3Storage, getFilesFromPath } = require('web3.storage')
|
||||
// Global npm libraries
|
||||
// const BCHJS = require('@psf/bch-js')
|
||||
// const BchWallet = require('minimal-slp-wallet/index')
|
||||
// const BchMessageLib = require('bch-message-lib/index')
|
||||
const fs = require('fs')
|
||||
const BchWallet = require('minimal-slp-wallet/index')
|
||||
const BchMessageLib = require('bch-message-lib/index')
|
||||
|
||||
async function publish () {
|
||||
async function publishToBch (cid) {
|
||||
try {
|
||||
// Get the Filecoin token from the environment variable.
|
||||
const filecoinToken = process.env.FILECOIN_TOKEN
|
||||
if (!filecoinToken) {
|
||||
const wif = process.env.REACT_BOOTSTRAP_WEB3_SPA_WIF
|
||||
if (!wif) {
|
||||
throw new Error(
|
||||
'Filecoin token not detected. Get a token from https://web3.storage and save it to the FILECOIN_TOKEN environment variable.'
|
||||
'WIF private key not detected. Get a private key from https://wallet.fullstack.cash and save it to the REACT_BOOTSTRAP_WEB3_SPA_WIF environment variable.'
|
||||
)
|
||||
}
|
||||
|
||||
// Get the WIF for updating Trout's blog from the environment variable.
|
||||
// const troutWif = process.env.TROUT_BLOG_WIF
|
||||
// if (!troutWif) {
|
||||
// throw new Error(
|
||||
// 'WIF for troutsblog.com not detect. Add it to the TROUT_BLOG_WIF environment variable.'
|
||||
// )
|
||||
// }
|
||||
|
||||
// Get a list of all the files to be uploaded.
|
||||
const fileAry = await getFileList()
|
||||
// console.log(`fileAry: ${JSON.stringify(fileAry, null, 2)}`)
|
||||
|
||||
// Upload the files to Filecoin.
|
||||
const cid = await uploadToFilecoin(fileAry, filecoinToken)
|
||||
// const cid = 'bafybeibgekzxiqr7irgs26g5pw5sv6xtvfiihccyagpmn7anzl6ffb2xc4'
|
||||
console.log('Content added with CID:', cid)
|
||||
console.log(`https://${cid}.ipfs.dweb.link/`)
|
||||
|
||||
// Initialize libraries for working with BCH blockchain.
|
||||
// const bchjs = new BCHJS()
|
||||
// const wallet = new BchWallet(troutWif, {
|
||||
// interface: 'consumer-api',
|
||||
// })
|
||||
// await wallet.walletInfoPromise
|
||||
// const bchMsg = new BchMessageLib({ wallet })
|
||||
//
|
||||
// // Publish the CID to the BCH blockchain.
|
||||
// const hex = await bchMsg.memo.memoPush(cid, 'IPFS UPDATE')
|
||||
//
|
||||
// // const txid = await bchjs.RawTransactions.sendRawTransaction(hex)
|
||||
// // Broadcast the transaction to the network.
|
||||
// const txid = await wallet.ar.sendTx(hex)
|
||||
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)
|
||||
}
|
||||
}
|
||||
publish()
|
||||
|
||||
function getFileList () {
|
||||
const fileAry = []
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readdir(FILE_PATH, (err, files) => {
|
||||
if (err) return reject(err)
|
||||
|
||||
files.forEach(file => {
|
||||
// console.log(file)
|
||||
fileAry.push(`${FILE_PATH}/${file}`)
|
||||
})
|
||||
|
||||
return resolve(fileAry)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
async function uploadToFilecoin (fileAry, token) {
|
||||
const storage = new Web3Storage({ token })
|
||||
|
||||
const files = []
|
||||
for (let i = 0; i < fileAry.length; i++) {
|
||||
const thisPath = fileAry[i]
|
||||
// console.log('thisPath: ', thisPath)
|
||||
|
||||
const pathFiles = await getFilesFromPath(thisPath)
|
||||
// console.log('pathFiles: ', pathFiles)
|
||||
|
||||
files.push(...pathFiles)
|
||||
}
|
||||
|
||||
console.log(`Uploading ${files.length} files. Please wait...`)
|
||||
const cid = await storage.put(files)
|
||||
|
||||
return cid
|
||||
}
|
||||
module.exports = publishToBch
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
// Local libraries
|
||||
const publishToFilecoin = require('./publish-filecoin')
|
||||
const publishToPinata = require('./publish-pinata')
|
||||
const publishToBch = require('./publish-bch')
|
||||
|
||||
async function publish () {
|
||||
try {
|
||||
@@ -16,6 +17,11 @@ async function publish () {
|
||||
|
||||
// Publish to Pinata
|
||||
await publishToPinata(cid)
|
||||
|
||||
// Public to BCH
|
||||
const txid = await publishToBch(cid)
|
||||
console.log(`\nBCH blockchain updated with new CID. TXID: ${txid}`)
|
||||
console.log(`https://blockchair.com/bitcoin-cash/transaction/${txid}`)
|
||||
} catch (err) {
|
||||
console.error('Error while trying to publish app: ', err)
|
||||
}
|
||||
|
||||
Generated
+3851
-12
File diff suppressed because it is too large
Load Diff
@@ -35,6 +35,8 @@
|
||||
]
|
||||
},
|
||||
"devDependencies": {
|
||||
"bch-message-lib": "2.2.1",
|
||||
"minimal-slp-wallet": "5.0.3",
|
||||
"standard": "17.0.0",
|
||||
"web3.storage": "4.3.0"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user