Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <h1>Click console at the bottom, or inspect element</h1>
              
            
!

CSS

              
                
              
            
!

JS

              
                var initialState = {}

/* Signed transaction format
tx = {
  contents: {
    type: string,  // either 'mint' or 'send'
    amount: int,   // some quantity of coins
    from: string,  // the address of the sender
    to: string,    // the address of the recipient
  },
  sig: string      // the signature of the sender
}
*/

var accounts = {
  'paymentProcessor': EthCrypto.createIdentity(),
  'alice': EthCrypto.createIdentity(),
  'jing': EthCrypto.createIdentity()
}

var unsignedTxs = [
  {
    type: 'mint',
    amount: 100,
    from: accounts.paymentProcessor.address,
    to: accounts.paymentProcessor.address,
    nonce: 0
  },
  {
    type: 'send',
    amount: 65,
    from: accounts.paymentProcessor.address,
    to: accounts.alice.address,
    nonce: 1
  },
  {
    type: 'send',
    amount: 10,
    from: accounts.alice.address,
    to: accounts.jing.address,
    nonce: 0
  }
]

function getTxHash (tx) {
  return EthCrypto.hash.keccak256(JSON.stringify(tx))
}

var signedTxs = [
  {
    contents: unsignedTxs[0],
    sig: EthCrypto.sign(accounts.paymentProcessor.privateKey, getTxHash(unsignedTxs[0]))
  },
  {
    contents: unsignedTxs[1],
    sig: EthCrypto.sign(accounts.paymentProcessor.privateKey, getTxHash(unsignedTxs[1]))
  },
  {
    contents: unsignedTxs[2],
    sig: EthCrypto.sign(accounts.alice.privateKey, getTxHash(unsignedTxs[2]))
  }
]

function applyTransaction (state, tx) {
  // Check the from address matches the signature
  const signer = EthCrypto.recover(tx.sig, getTxHash(tx.contents))
  if (signer !== tx.contents.from) {
    throw new Error('Invalid signature!')
  }
  // If we don't have a record for this address, create one
  if (!(tx.contents.to in state)) {
    state[[tx.contents.to]] = {
      balance: 0,
      nonce: 0
    }
  }
  // Check that the nonce is correct for replay protection
  if (tx.contents.nonce !== state[[tx.contents.from]].nonce) {
    throw new Error('Invalid nonce!')
  }
  // Mint coins **only if identity is paymentProcessor**
  if (tx.contents.type === 'mint' && tx.contents.from === accounts.paymentProcessor.address) {
    state[[tx.contents.to]].balance += tx.contents.amount
  } else if (tx.contents.type === 'send') { // Send coins
    if (state[[tx.contents.from]].balance - tx.contents.amount < 0) {
      throw new Error('Not enough money!')
    }
    state[[tx.contents.from]].balance -= tx.contents.amount
    state[[tx.contents.to]].balance += tx.contents.amount
  }
  state[[tx.contents.from]].nonce += 1
  return state
}

// Apply all transactions and print out all intermediate state
let state = initialState
for (let i = 0; i < signedTxs.length; i++) {
  state = applyTransaction(state, signedTxs[i])
  console.log(('State at time ' + i), state)
}

// Just for fun, let's try signing alice's transaction with jing's privatekey and see if we catch it
const invalidSigTx = {
  contents: unsignedTxs[2], // alice sending jing 10
  sig: EthCrypto.sign(accounts.jing.privateKey, getTxHash(unsignedTxs[2]))
}

try {
  applyTransaction(state, invalidSigTx)
} catch (err) {
  console.log('We caught the error!', err)
}

// Now let's try replaying a tx and see if we catch it
try {
  applyTransaction(state, signedTxs[2])
} catch (err) {
  console.log('We caught the error!', err)
}
// Woot!
console.log('Success!')
              
            
!
999px

Console