nxt.addons

## Class AbstractContractContext

- java.lang.Object
- - nxt.addons.AbstractContractContext

- Direct Known Subclasses:
    AbstractOperationContext, BlockContext, DelegatedContext, RequestContext

      public abstract class AbstractContractContext
      extends java.lang.Object

- - ### Nested Class Summary

    Nested Classes
    Modifier and Type | Class and Description
    --- | ---
    `static class` | `AbstractContractContext.EventSource`

  - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected static java.util.Map<java.lang.Integer,ChainWrapper>` | `chainById`
    `protected static java.util.Map<java.lang.String,ChainWrapper>` | `chainByName`
    `protected ContractRunnerConfig` | `config`
    `protected java.lang.String` | `contractName`
    `protected static int` | `FEE_CANNOT_CALCULATE`
    `protected static int` | `FEE_EXCEEDS_AMOUNT`
    `static int` | `INTERNAL_ERROR_CODE_THRESHOLD`
    `protected static int` | `MESSAGE_TO_ENCRYPT_WITHOUT_SECRET_PHRASE`
    `protected AbstractContractContext.EventSource` | `source`
    `protected static int` | `VALIDATE_SAME_ACCOUNT_CODE`
    `protected static int` | `VALIDATE_SAME_CHAIN`
    `protected static int` | `VALIDATE_SAME_TRANSACTION_TYPE`

  - ### Method Summary

    All Methods Instance Methods Abstract Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected JO` | `addTriggerData(JO jo)`
    `JO` | `createTransaction(APICall.Builder builder)` Submit a transaction to the blockchain
    `JO` | `createTransaction(APICall.Builder builder, boolean reduceFeeFromAmount)` Submit a transaction to the blockchain
    `long` | `fullHashToId(byte[] fullHash)` Convert a 32 byte hash represented as byte array to a numeric entity id
    `long` | `fullHashToId(java.lang.String fullHashStr)` Convert a 32 byte hash represented as hex string to a numeric entity id
    `JO` | `generateErrorResponse(int code, java.lang.String description, java.lang.Object... args)` Generate error response for the contract invocation
    `JO` | `generateInfoResponse(java.lang.String description, java.lang.Object... args)` Generate info response for the contract invocation
    `protected JO` | `generateInternalErrorResponse(int code, java.lang.String description, java.lang.Object... args)`
    `JO` | `generateResponse(JO response)` Generate response of the contract invocation
    `java.lang.String` | `getAccount()` Returns the contract runner account as unsigned long number
    `java.lang.String` | `getAccountRs()` Returns the contract runner account in Reed Solomon format
    `abstract BlockResponse` | `getBlock()` Returns the Json representation of the current block
    `JO` | `getBlockchainConstants()` Returns a Json object representing the response of the getConstants API call
    `int` | `getBlockchainHeight()`
    `ChainWrapper` | `getChain(int id)` Returns the chain object represented by this chain id
    `ChainWrapper` | `getChain(java.lang.String name)` Returns the chain object represented by this chain name
    `ContractRunnerConfig` | `getConfig()` Returns the contract runner configuration parameters
    `<T extends AbstractContractContext> T` | `getContext()`
    `java.lang.String` | `getContractName()` Returns the contract name
    `JO` | `getContractRunnerConfigParams(java.lang.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.
    `JO` | `getContractSetupParameters()`
    `byte[]` | `getHash(byte[] b)` Calculate the SHA-256 hash of a message
    `byte[]` | `getHash(byte[] b, java.lang.String algorithm)` Calculate the hash of a message
    `<Params> Params` | `getParams(java.lang.Class<Params> clazz)`
    `ChainWrapper` | `getParentChain()` Returns the parent chain object
    `protected JO` | `getPhasingAttachment()`
    `byte[]` | `getPublicKey()` Returns the public key as byte array
    `byte[]` | `getPublicKey(java.lang.String secretPhrase)` Convert secret phrase to public key
    `java.lang.String` | `getPublicKeyHexString()` Returns the public key in hex string format
    `RandomnessSource` | `getRandomnessSource()` Return an initialized random object
    `protected abstract java.lang.String` | `getReferencedTransaction()`
    `JO` | `getResponse()` Returns the contract invocation response
    `JO` | `getRuntimeParams()`
    `AbstractContractContext.EventSource` | `getSource()` Returns the source of the contract invocation event
    `RandomnessSource` | `initRandom(long userSeed)` Initializes a predictable random seed so that all nodes running the contract will generate the same random values
    `boolean` | `isPermissionGranted(java.security.Permission permission)` Check with the Security Manager if the contract code has a specific permission
    `ContractAndSetupParameters` | `loadContract(java.lang.String name)` Load a contract instance from the blockchain cloud data based on the contract reference name
    `void` | `logErrorMessage(java.lang.Throwable t)` Log an exception message and stack trace into the node log file
    `void` | `logInfoMessage(java.lang.String format, java.lang.Object... args)` Log a formatted string into the node log file
    `long` | `parseAccountId(java.lang.String account)`
    `byte[]` | `parseHexString(java.lang.String hex)` Parse hex string into a byte array
    `long` | `publicKeyToAccountId(byte[] publicKey)` Convert a public key represented as byte array to a numeric account id
    `long` | `publicKeyToAccountId(java.lang.String publicKeyStr)` Convert a public key represented as hex string to a numeric account id
    `java.lang.String` | `rsAccount(long accountId)` Convert a numeric account id to its Reed Solomon representation
    `void` | `setContractSetupParameters(JO contractSetupParameters)`
    `byte[]` | `sign(byte[] message, java.lang.String secretPhrase)` Sign a message with an account secret phrase
    `java.lang.String` | `toHexString(byte[] bytes)` Convert byte array to hex string
    `boolean` | `verify(byte[] signature, byte[] message, byte[] publicKey)` Verify an account signature

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### INTERNAL_ERROR_CODE_THRESHOLD

          public static final int INTERNAL_ERROR_CODE_THRESHOLD

      See Also:
        Constant Field Values

    - #### VALIDATE_SAME_ACCOUNT_CODE

          protected static final int VALIDATE_SAME_ACCOUNT_CODE

      See Also:
        Constant Field Values

    - #### VALIDATE_SAME_TRANSACTION_TYPE

          protected static final int VALIDATE_SAME_TRANSACTION_TYPE

      See Also:
        Constant Field Values

    - #### VALIDATE_SAME_CHAIN

          protected static final int VALIDATE_SAME_CHAIN

      See Also:
        Constant Field Values

    - #### FEE_CANNOT_CALCULATE

          protected static final int FEE_CANNOT_CALCULATE

      See Also:
        Constant Field Values

    - #### FEE_EXCEEDS_AMOUNT

          protected static final int FEE_EXCEEDS_AMOUNT

      See Also:
        Constant Field Values

    - #### MESSAGE_TO_ENCRYPT_WITHOUT_SECRET_PHRASE

          protected static final int MESSAGE_TO_ENCRYPT_WITHOUT_SECRET_PHRASE

      See Also:
        Constant Field Values

    - #### chainById

          protected static final java.util.Map<java.lang.Integer,ChainWrapper> chainById

    - #### chainByName

          protected static final java.util.Map<java.lang.String,ChainWrapper> chainByName

    - #### source

          protected AbstractContractContext.EventSource source

    - #### config

          protected ContractRunnerConfig config

    - #### contractName

          protected final java.lang.String contractName

  - ### Method Detail

    - #### getContext

          public <T extends AbstractContractContext> T getContext()

    - #### getContractName

          public java.lang.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

          public AbstractContractContext.EventSource getSource()

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

    - #### getContractRunnerConfigParams

          public JO getContractRunnerConfigParams(java.lang.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 java.lang.String getAccount()

      Returns the contract runner account as unsigned long number
      Returns:
        the account id

    - #### getAccountRs

          public java.lang.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 java.lang.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

    - #### 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(java.lang.String description,
                                         java.lang.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,
                                          java.lang.String description,
                                          java.lang.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,
                                                     java.lang.String description,
                                                     java.lang.Object... args)

    - #### addTriggerData

          protected JO addTriggerData(JO jo)

    - #### getReferencedTransaction

          protected abstract java.lang.String getReferencedTransaction()

    - #### 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

    - #### getPhasingAttachment

          protected JO getPhasingAttachment()

    - #### loadContract

          public ContractAndSetupParameters loadContract(java.lang.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,
                                java.lang.String algorithm)

      Calculate the hash of a message
      Parameters:
        `b` - the message bytes
        `algorithm` - the hashing algorithm
      Returns:
        the hash of the message

    - #### sign

          public byte[] sign(byte[] message,
                             java.lang.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(java.lang.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(java.lang.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(java.lang.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(java.lang.String account)

      Parameters:
        `account` - the RS or unsigned long account id
      Returns:
        the numeric account id

    - #### rsAccount

          public java.lang.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(java.lang.String format,
                                     java.lang.Object... args)

      Log a formatted string into the node log file
      Parameters:
        `format` - the format string
        `args` - the format arguments

    - #### logErrorMessage

          public void logErrorMessage(java.lang.Throwable t)

      Log an exception message and stack trace into the node log file
      Parameters:
        `t` - the throwable object

    - #### parseHexString

          public byte[] parseHexString(java.lang.String hex)

      Parse hex string into a byte array
      Parameters:
        `hex` - the hex string
      Returns:
        the byte array

    - #### toHexString

          public java.lang.String toHexString(byte[] bytes)

      Convert byte array to hex string
      Parameters:
        `bytes` - the byte array
      Returns:
        the hex string

    - #### getPublicKey

          public byte[] getPublicKey(java.lang.String secretPhrase)

      Convert secret phrase to public key
      Parameters:
        `secretPhrase` - the secret phrase
      Returns:
        the public key

    - #### isPermissionGranted

          public boolean isPermissionGranted(java.security.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(java.lang.Class<Params> clazz)

    - #### setContractSetupParameters

          public void setContractSetupParameters(JO contractSetupParameters)