PNG to XDR

node v14.15.1
version: 1.0.1
endpointsharetweet
const { TransactionBuilder, Keypair, BASE_FEE, Operation, Asset } = require('stellar-sdk') const cors = require('cors') const { json } = require('body-parser') const multer = require('multer') const express = require('@runkit/tyvdh/express-endpoint/1.0.0') const sendError = require('@runkit/tyvdh/send-error/1.0.1') const setupStellarNetwork = require('@runkit/tyvdh/setup-stellar-network/1.0.0') const app = express(exports) const upload = multer() app.use(cors()) app.use(json()) app.post('/', upload.single('png'), async (req, res) => { try { const { network } = req.body const { buffer } = req.file let png = buffer.toString('base64') const { server, networkPassphrase } = setupStellarNetwork(network) const BADGE_SEK = req.headers['X-BADGE'] || req.headers['x-badge'] const badgeKeypair = Keypair.fromSecret(BADGE_SEK) const badgePublicKey = badgeKeypair.publicKey() const ROOT_SEK = req.headers['X-ROOT'] || req.headers['x-root'] const rootKeypair = Keypair.fromSecret(ROOT_SEK) const rootPublicKey = rootKeypair.publicKey() const SIGNER_PUK = req.headers['X-SIGNER'] || req.headers['x-signer'] const SIGNER_SEK = req.headers['X-SIGNER'] || req.headers['x-signer'] const signerKeypair = Keypair.fromSecret(SIGNER_SEK) const signerPublicKey = signerKeypair.publicKey() const transaction = await server.loadAccount(rootPublicKey) .then((account) => { let transaction = new TransactionBuilder(account, { fee: BASE_FEE, networkPassphrase, }) .setTimeout(0) let i = 0 transaction.addOperation(Operation.payment({ destination: badgePublicKey, source: rootPublicKey, amount: '0.5', asset: Asset.native(), })) transaction.addOperation(Operation.setOptions({ masterWeight: 0, lowThreshold: 1, medThreshold: 1, highThreshold: 1, signer: { ed25519PublicKey: SIGNER_PUK, weight: 1 }, source: badgePublicKey })) while (png.length) { const name = `${String(i).padStart(2, '0')}${png.substr(0, 62)}` const value = png.substr(62, 64) || '=' transaction.addOperation(Operation.payment({ destination: badgePublicKey, source: rootPublicKey, amount: '0.5', asset: Asset.native(), })) transaction.addOperation(Operation.manageData({ name, value, source: badgePublicKey })) png = png.substr(62 + 64) i++ } transaction = transaction.build() transaction.sign( badgeKeypair, signerKeypair, rootKeypair, ) return transaction }) res.send(transaction.toXDR()) } catch(err) { sendError(err, res) } })
Loading…

no comments

    sign in to comment