Models

plasma-utils provides several different “models” that represent the various common data structures we use. These models implement the schemas that we use in serialization.

It’s pretty simple to import all of the available models:

const utils = require('plasma-utils')
const models = utils.serialization.models

Transfer

A Transfer is the basic component of every transaction. Every transaction has one or more transfers.

const Transfer = models.Transfer

const transfer = new Transfer({
  token: 0,
  start: 0,
  end: 100,
  sender: '0x1E3a4a2edec2b3568B5Ad0656ec3b48d9C699dB6',
  recipient: '0x8508c8aCA521512D4695eCF6976d2e8D2666a46d'
})

Properties


token

transfer.token

ID of the token being transferred.

Returns

BigNum: The token ID.


start

transfer.start

Start of the range being transferred.

Returns

BigNum: The range start.


typedStart

transfer.typedStart

The “typed” start of the range being transferred. Calculated by concatenating token and start. Primarily used for calculating state updates in plasma-core.

Returns

BigNum: The typed start.


end

transfer.end

End of the range being transferred.

Returns

BigNum: The range end.


typedEnd

transfer.typedEnd

The “typed” end of the range being transferred. Calculated by concatenating token and end. Primarily used for calculating state updates in plasma-core.

Returns

BigNum: The typed end.


sender

transfer.sender

Address of the user sending the transfer.

Returns

string: Sender address.


recipient

transfer.recipient

Address of the user receiving the transfer.

Returns

string: Recipient address.


encoded

transfer.encoded

The encoded version of the transfer according to the rules in our schemas.

Returns

string: The encoded transfer.


UnsignedTransaction

An UnsignedTransaction contains transfers and a block number, but no signatures.

const UnsignedTransaction = models.UnsignedTransaction

const unsigned = new UnsignedTransaction({
  block: 123,
  transfers: [
    {
      token: 0,
      start: 0,
      end: 100,
      sender: '0x1E3a4a2edec2b3568B5Ad0656ec3b48d9C699dB6',
      recipient: '0x8508c8aCA521512D4695eCF6976d2e8D2666a46d'
    }
  ]
})

Properties


block

unsigned.block

The block in which this transaction was included.

Returns

BigNum: The transaction block number.


transfers

unsigned.transfers

A list of Transfers that make up this transaction.

Returns

Array<Transfer>: A list of transfers.


encoded

unsigned.encoded

The hex-encoded version of this transaction.

Returns

string: Encoded transaction.


hash

unsigned.hash

The keccak256 (Ethereum’s SHA3) hash of the encoded transaction.

Returns

string: Hash of the transaction.


SignedTransaction

An SignedTransaction contains transfers, and a block number, and a signature for each transfer.

const SignedTransaction = models.SignedTransaction

const signed = new SignedTransaction({
  block: 123,
  transfers: [
    {
      token: 0,
      start: 0,
      end: 100,
      sender: '0x1E3a4a2edec2b3568B5Ad0656ec3b48d9C699dB6',
      recipient: '0x8508c8aCA521512D4695eCF6976d2e8D2666a46d'
    }
  ],
  signatures: [
    {
      v: '0x1b',
      r: '0xd693b532a80fed6392b428604171fb32fdbf953728a3a7ecc7d4062b1652c042',
      s: '0x24e9c602ac800b983b035700a14b23f78a253ab762deab5dc27e3555a750b354'
    }
  ]
})

Properties


block

signed.block

The block in which this transaction was included.

Returns

BigNum: The transaction block number.


transfers

signed.transfers

A list of Transfers that make up this transaction.

Returns

Array<Transfer>: A list of transfers.


signatures

signed.signatures

A list of Signatures on this transaction. There should be one signature for each transfer, where the signature is from the sender of the transfer.

Returns

Array<Signature>: A list of signatures.


encoded

signed.encoded

The hex-encoded version of this transaction.

Returns

string: Encoded transaction.


hash

signed.hash

The keccak256 (Ethereum’s SHA3) hash of the encoded unsigned version of this transaction. Effectively the same as casting this transaction to an UnsignedTransaction and getting the hash.

Returns

string: Hash of the unsigned version of this transaction.


Methods


checkSigs

signed.checkSigs()

Checks that the signatures on the transaction are valid.

Returns

boolean: true if the transaction is valid, false otherwise.