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 includes JSX processing.

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

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

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 accounts = {
  'paypal': EthCrypto.createIdentity(),
  'aparna': EthCrypto.createIdentity(),
  'jing': EthCrypto.createIdentity()
}

var initialState = {
  utxos: [0],
  txOutputs: [{value: 1000, owner: accounts.paypal.address}]
}

var unsignedTxs = [
  {
    inputs: [0],
    outputs: [
      {value: 250, owner: accounts.jing.address},
      {value: 250, owner: accounts.aparna.address},
      {value: 500, owner: accounts.paypal.address}
    ]
  },
  {
    inputs: [1, 2],
    outputs: [
      {value: 500, owner: accounts.paypal.address}
    ]
  }
]

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

var signedTxs = [
  {
    contents: unsignedTxs[0],
    sigs: [EthCrypto.sign(accounts.paypal.privateKey, getTxHash(unsignedTxs[0]))]
  },
  {
    contents: unsignedTxs[1],
    sigs: [
      EthCrypto.sign(accounts.jing.privateKey, getTxHash(unsignedTxs[1])),
      EthCrypto.sign(accounts.aparna.privateKey, getTxHash(unsignedTxs[1]))
    ]
  }
]

function applyTransaction (state, tx) {
  // Recover addresses for all signatures
  const signers = []
  for (let sig of tx.sigs) {
    signers.push(EthCrypto.recover(sig, getTxHash(tx.contents)))
  }
  let totalInputValue = 0
  // Check that all inputs are indeed unspent, and that we have the signature for the owner
  for (let inputIndex of tx.contents.inputs) {
    const input = state.txOutputs[inputIndex]
    if (!signers.includes(input.owner)) {
      throw new Error('Missing signature for: ' + input.owner)
    }
    if (state.utxos[inputIndex] !== 0) {
      throw new Error('Trying to spend spent tx output!')
    }
    // Add to the total input value
    totalInputValue += input.value
  }
  let totalOutputValue = 0
  for (let output of tx.contents.outputs) {
    totalOutputValue += output.value
  }
  if (totalInputValue !== totalOutputValue) {
    throw new Error('Non-equal input and output values!')
  }
  // Update the state object!
  // First make the inputs spent
  for (let inputIndex of tx.contents.inputs) {
    state.utxos[inputIndex] = 1
  }
  // Next append the outputs to our list of outputs and add a 0 to our list of utxos
  for (let output of tx.contents.outputs) {
    state.txOutputs.push(output)
    state.utxos.push(0)
  }
  return state
}

console.log(signedTxs)


// 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 aparna's transaction with jing's privatekey and see if we catch it
const invalidSigTx = {
  contents: unsignedTxs[1],
  sigs: [EthCrypto.sign(accounts.paypal.privateKey, getTxHash(unsignedTxs[1]))]
}

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[1])
} catch (err) {
  console.log('We caught the error!', err)
}
// Woot!
console.log('Success!')
              
            
!
999px

Console