This is a simple example of a smart contract written in ECMAScript which uses SQL database for storing the state.
class Engine {
constructor(db, contracts) {
this.db = db
this.contracts = contracts
}| plant.use(async ({req, res}) => { | |
| const body = await req.json() | |
| res.json(body) | |
| }) |
| import {app} from 'electron' | |
| import {createServer} from '@plant/electron' | |
| import Plant from '@plant/plant' | |
| app.on('ready', () => { | |
| // Define simple Plant server | |
| const plant = new Plant() | |
| plant.use(({res}) => { | |
| res.text('OK') |
| export function decodeJson64(value) { | |
| return JSON.parse( | |
| Buffer.from(value, 'base64') | |
| ) | |
| } | |
| export function encodeJson64(value) { | |
| return Buffer.from( | |
| JSON.stringify(value), 'utf8' | |
| ) |
| /** | |
| * Split string by delimiter limited times. By default 1. | |
| * | |
| * @example | |
| * split(':', 'user:id:1') -> ['user', 'id:1'] | |
| * split(':', 'user:id:1:login:admin', 2) -> ['user', 'id' , '1:login:admin'] | |
| * split(':', 'hello') -> ['hello'] | |
| */ | |
| export function split(delim, str, count = 1) { | |
| if (count < 1) { |
| "use strict"; | |
| Object.defineProperty(exports, "__esModule", { value: true }); | |
| /** | |
| * RLP Encoding based on: https://github.com/ethereum/wiki/wiki/%5BEnglish%5D-RLP | |
| * This function takes in a data, convert it to buffer if not, and a length for recursion | |
| * @param input - will be converted to buffer | |
| * @returns returns buffer of encoded data | |
| **/ | |
| function encode(input) { | |
| if (Array.isArray(input)) { |
| /* | |
| Source: https://mariusschulz.com | |
| */ | |
| .token.cdata,.token.comment,.token.doctype,.token.prolog { | |
| color: #a0a1a7; | |
| font-style: italic | |
| } | |
| .token.boolean { |
| // Got from https://github.com/NodeGuy/Deterministic.js | |
| function createRandomizer() { | |
| let s0, s1, s2, c; | |
| s0 = 0.8725217853207141; | |
| s1 = 0.520505596883595; | |
| s2 = 0.22893249243497849; | |
| c = 1; |
| /* chacha20 - 256 bits */ | |
| // Written in 2014 by Devi Mandiri. Public domain. | |
| // | |
| // Implementation derived from chacha-ref.c version 20080118 | |
| // See for details: http://cr.yp.to/chacha/chacha-20080128.pdf | |
| function U8TO32_LE(x, i) { | |
| return x[i] | (x[i+1]<<8) | (x[i+2]<<16) | (x[i+3]<<24); | |
| } |