Package nxt.addons

Class AbstractContractContext

java.lang.Object
nxt.addons.AbstractContractContext
Direct Known Subclasses:
AbstractOperationContext, BlockContext, DelegatedContext, InitializationContext, RequestContext, ShutdownContext

public abstract class AbstractContractContext extends Object
  • Field Details

  • Method Details

    • getContext

      public <T extends AbstractContractContext> T getContext()
    • getContractName

      public String getContractName()
      Returns the contract name
      Returns:
      the contract name
    • getBlock

      public abstract BlockResponse getBlock()
      Returns the Json representation of the current block
      Returns:
      the current block represented as Json
    • initRandom

      public RandomnessSource initRandom(long userSeed)
      Initializes a predictable random seed so that all nodes running the contract will generate the same random values
      Parameters:
      userSeed - the predictable seed based on user specified info which ideally should be encrypted when submitted
      Returns:
      the random number generator
    • getRandomnessSource

      public RandomnessSource getRandomnessSource()
      Return an initialized random object
      Returns:
      the random number generator object
    • getSource

      Returns the source of the contract invocation event
      Returns:
      the event source
    • getContractRunnerConfigParams

      public JO getContractRunnerConfigParams(String contractName)
      Returns the node specific configuration for a specific contract loaded from the contract runner configuration file Only use these parameters for secret information which cannot be deployed to the blockchain like secret phrases and admin passwords.
      Parameters:
      contractName - the contract name
      Returns:
      the parameter object in json format
    • getConfig

      public ContractRunnerConfig getConfig()
      Returns the contract runner configuration parameters
      Returns:
      the contract runner configuration parameters formatted as Json
    • getAccount

      public String getAccount()
      Returns the contract runner account as unsigned long number
      Returns:
      the account id
    • getAccountRs

      public String getAccountRs()
      Returns the contract runner account in Reed Solomon format
      Returns:
      the account id
    • getPublicKey

      public byte[] getPublicKey()
      Returns the public key as byte array
      Returns:
      the public key
    • getPublicKeyHexString

      public String getPublicKeyHexString()
      Returns the public key in hex string format
      Returns:
      the public key
    • getBlockchainConstants

      public JO getBlockchainConstants()
      Returns a Json object representing the response of the getConstants API call
      Returns:
      a Json object
    • getResponse

      public JO getResponse()
      Returns the contract invocation response
      Returns:
      the contract invocation response formatted as Json
    • setResponse

      public void setResponse(JO response)
    • canSetResponse

      public boolean canSetResponse()
    • generateResponse

      public JO generateResponse(JO response)
      Generate response of the contract invocation
      Parameters:
      response - the Json object which represents the contract invocation response
      Returns:
      the generated response
    • generateInfoResponse

      public JO generateInfoResponse(String description, Object... args)
      Generate info response for the contract invocation
      Parameters:
      description - the description as a string format
      args - the description string format arguments
      Returns:
      the generated info response
    • generateErrorResponse

      public JO generateErrorResponse(int code, String description, Object... args)
      Generate error response for the contract invocation
      Parameters:
      code - the error code
      description - the error description as a string format
      args - the description string format arguments
      Returns:
      the generated error response
    • generateInternalErrorResponse

      protected JO generateInternalErrorResponse(int code, String description, Object... args)
    • addTriggerData

      protected JO addTriggerData(JO jo, int managedAccountIndex)
    • createTransaction

      public JO createTransaction(APICall.Builder builder)
      Submit a transaction to the blockchain
      Parameters:
      builder - the API caller for the specific transaction type
      Returns:
      the response of the transaction creation
    • createTransaction

      public JO createTransaction(APICall.Builder builder, boolean reduceFeeFromAmount)
      Submit a transaction to the blockchain
      Parameters:
      builder - the API caller for the specific transaction type
      reduceFeeFromAmount - set to true to reduce the transaction fee from the transaction amount if applicable, false otherwise
      Returns:
      the response of the transaction creation
    • createTransaction

      public JO createTransaction(APICall.Builder builder, boolean reduceFeeFromAmount, int managedAccountIndex)
      Submit a transaction to the blockchain by a managed account. A ManagedAccountsBundler can be used to bundle such transactions.

      Note that a negative index will create the transaction with a hardened extended private key, which prevents the generation of the public key from the master public key. This will prevent the ManagedAccountsBundler to bundle the transaction.

      Parameters:
      builder - the API caller for the specific transaction type
      reduceFeeFromAmount - set to true to reduce the transaction fee from the transaction amount if applicable, false otherwise
      managedAccountIndex - index of the managed account with whose private key the transaction will be signed. If set to Integer.MAX_VALUE, the transaction is signed by the contract account
      Returns:
      the response of the transaction creation
    • getPhasingAttachment

      protected JO getPhasingAttachment()
    • loadContract

      public ContractAndSetupParameters loadContract(String name)
      Load a contract instance from the blockchain cloud data based on the contract reference name
      Parameters:
      name - the contract reference name for the contract runner account
      Returns:
      an instance of the contract class stored in the blockchain as cloud data
    • getHash

      public byte[] getHash(byte[] b)
      Calculate the SHA-256 hash of a message
      Parameters:
      b - the message bytes
      Returns:
      the hash of the message
    • getHash

      public byte[] getHash(byte[] b, String algorithm)
      Calculate the hash of a message
      Parameters:
      b - the message bytes
      algorithm - the hashing algorithm
      Returns:
      the hash of the message
    • getNetworkType

      public String getNetworkType()
    • sign

      public byte[] sign(byte[] message, String secretPhrase)
      Sign a message with an account secret phrase
      Parameters:
      message - the message bytes
      secretPhrase - the account secret phrase
      Returns:
      the signature bytes
    • verify

      public boolean verify(byte[] signature, byte[] message, byte[] publicKey)
      Verify an account signature
      Parameters:
      signature - the signature bytes
      message - the message bytes
      publicKey - the signer account public key
      Returns:
      true if the the signature is valid, false otherwise
    • fullHashToId

      public long fullHashToId(byte[] fullHash)
      Convert a 32 byte hash represented as byte array to a numeric entity id
      Parameters:
      fullHash - the hash represented as byte array
      Returns:
      the numeric entity id
    • fullHashToId

      public long fullHashToId(String fullHashStr)
      Convert a 32 byte hash represented as hex string to a numeric entity id
      Parameters:
      fullHashStr - the hash represented as hex string
      Returns:
      the numeric entity id
    • publicKeyToAccountId

      public long publicKeyToAccountId(byte[] publicKey)
      Convert a public key represented as byte array to a numeric account id
      Parameters:
      publicKey - the public key represented as byte array
      Returns:
      the numeric account id
    • publicKeyToAccountId

      public long publicKeyToAccountId(String publicKeyStr)
      Convert a public key represented as hex string to a numeric account id
      Parameters:
      publicKeyStr - the public key represented as hex string
      Returns:
      the numeric account id
    • getChain

      public ChainWrapper getChain(String name)
      Returns the chain object represented by this chain name
      Parameters:
      name - the chain name
      Returns:
      the chain object
    • getChain

      public ChainWrapper getChain(int id)
      Returns the chain object represented by this chain id
      Parameters:
      id - the chain id
      Returns:
      the chain object
    • getParentChain

      public ChainWrapper getParentChain()
      Returns the parent chain object
      Returns:
      the parent chain object
    • parseAccountId

      public long parseAccountId(String account)
      Parameters:
      account - the RS or unsigned long account id
      Returns:
      the numeric account id
    • rsAccount

      public String rsAccount(long accountId)
      Convert a numeric account id to its Reed Solomon representation
      Parameters:
      accountId - the numeric account id
      Returns:
      the RS account id
    • getBlockchainHeight

      public int getBlockchainHeight()
    • logInfoMessage

      public void logInfoMessage(String format, Object... args)
      Log a formatted string into the node log file
      Parameters:
      format - the format string
      args - the format arguments
    • logErrorMessage

      public void logErrorMessage(Throwable t)
      Log an exception message and stack trace into the node log file
      Parameters:
      t - the throwable object
    • parseHexString

      public byte[] parseHexString(String hex)
      Parse hex string into a byte array
      Parameters:
      hex - the hex string
      Returns:
      the byte array
    • toHexString

      public String toHexString(byte[] bytes)
      Convert byte array to hex string
      Parameters:
      bytes - the byte array
      Returns:
      the hex string
    • getPublicKey

      public byte[] getPublicKey(String secretPhrase)
      Convert secret phrase to public key
      Parameters:
      secretPhrase - the secret phrase
      Returns:
      the public key
    • getPublicKey

      public byte[] getPublicKey(byte[] privateKey)
      Convert private key to public key
      Parameters:
      privateKey - the secret phrase
      Returns:
      the public key
    • isPermissionGranted

      public boolean isPermissionGranted(Permission permission)
      Check with the Security Manager if the contract code has a specific permission
      Parameters:
      permission - the permission to check
      Returns:
      true if permission is granted, false otherwise
    • getRuntimeParams

      public JO getRuntimeParams()
    • getContractSetupParameters

      public JO getContractSetupParameters()
    • getParams

      public <Params> Params getParams(Class<Params> clazz)
    • setContractSetupParameters

      public void setContractSetupParameters(JO contractSetupParameters)