All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nftco.flow.sdk.Flow.kt Maven / Gradle / Ivy

The newest version!
package com.nftco.flow.sdk

import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.module.kotlin.registerKotlinModule
import io.grpc.ManagedChannel
import io.grpc.ManagedChannelBuilder
import org.onflow.protobuf.access.AccessAPIGrpc
import com.nftco.flow.sdk.cadence.CadenceNamespace
import com.nftco.flow.sdk.cadence.Field
import com.nftco.flow.sdk.cadence.JsonCadenceMarshalling
import com.nftco.flow.sdk.impl.AsyncFlowAccessApiImpl
import com.nftco.flow.sdk.impl.FlowAccessApiImpl
import kotlin.reflect.KClass

object Flow {

    const val DEFAULT_USER_AGENT = "Flow JVM SDK"

    const val DEFAULT_MAX_MESSAGE_SIZE = 16777216

    var OBJECT_MAPPER: ObjectMapper

    var DEFAULT_CHAIN_ID: FlowChainId = FlowChainId.MAINNET
        private set

    var DEFAULT_ADDRESS_REGISTRY: AddressRegistry = AddressRegistry()
        private set

    init {
        OBJECT_MAPPER = ObjectMapper()
        OBJECT_MAPPER.registerKotlinModule()
        OBJECT_MAPPER.findAndRegisterModules()
    }

    @JvmStatic
    @JvmOverloads
    fun configureDefaults(
        chainId: FlowChainId = DEFAULT_CHAIN_ID,
        addressRegistry: AddressRegistry = DEFAULT_ADDRESS_REGISTRY
    ) {
        DEFAULT_CHAIN_ID = chainId
        DEFAULT_ADDRESS_REGISTRY = addressRegistry
    }

    @JvmStatic
    @JvmOverloads
    fun newAccessApi(host: String, port: Int = 9000, secure: Boolean = false, userAgent: String = DEFAULT_USER_AGENT, maxMessageSize: Int = DEFAULT_MAX_MESSAGE_SIZE): FlowAccessApi {
        val channel = openChannel(host, port, secure, userAgent, maxMessageSize)
        return FlowAccessApiImpl(AccessAPIGrpc.newBlockingStub(channel))
    }

    @JvmStatic
    @JvmOverloads
    fun newAsyncAccessApi(host: String, port: Int = 9000, secure: Boolean = false, userAgent: String = DEFAULT_USER_AGENT, maxMessageSize: Int = DEFAULT_MAX_MESSAGE_SIZE): AsyncFlowAccessApi {
        val channel = openChannel(host, port, secure, userAgent, maxMessageSize)
        return AsyncFlowAccessApiImpl(AccessAPIGrpc.newFutureStub(channel))
    }

    @JvmStatic
    private fun openChannel(host: String, port: Int, secure: Boolean, userAgent: String, maxMessageSize: Int): ManagedChannel {
        var channelBuilder = ManagedChannelBuilder
            .forAddress(host, port)
            .userAgent(userAgent)
            .maxInboundMessageSize(maxMessageSize)

        channelBuilder = if (secure) {
            channelBuilder.useTransportSecurity()
        } else {
            channelBuilder.usePlaintext()
        }

        return channelBuilder.build()
    }

    @JvmStatic
    fun > decodeJsonCadenceList(string: String): List = decodeJsonCadenceList(string.toByteArray(Charsets.UTF_8))
    @JvmStatic
    fun > decodeJsonCadenceList(bytes: ByteArray): List = OBJECT_MAPPER.readValue(bytes, object : TypeReference>() {})

    @JvmStatic
    fun > decodeJsonCadence(string: String): T = decodeJsonCadence(string.toByteArray(Charsets.UTF_8))
    @JvmStatic
    fun > decodeJsonCadence(bytes: ByteArray): T = OBJECT_MAPPER.readValue(bytes, object : TypeReference() {})

    @JvmStatic
    fun > encodeJsonCadenceList(jsonCadences: Iterable): ByteArray = OBJECT_MAPPER.writeValueAsBytes(jsonCadences)
    @JvmStatic
    fun > encodeJsonCadence(jsonCadence: T): ByteArray = OBJECT_MAPPER.writeValueAsBytes(jsonCadence)

    @JvmStatic
    fun  unmarshall(type: KClass, value: Field<*>, namespace: FlowAddress): T = JsonCadenceMarshalling.unmarshall(type, value, namespace)

    @JvmStatic
    @JvmOverloads
    fun  unmarshall(type: KClass, value: Field<*>, namespace: CadenceNamespace = CadenceNamespace()): T = JsonCadenceMarshalling.unmarshall(type, value, namespace)

    @JvmStatic
    fun  marshall(value: T, clazz: KClass, namespace: FlowAddress): Field<*> = JsonCadenceMarshalling.marshall(value, clazz, namespace)

    @JvmStatic
    @JvmOverloads
    fun  marshall(value: T, clazz: KClass, namespace: CadenceNamespace = CadenceNamespace()): Field<*> = JsonCadenceMarshalling.marshall(value, clazz, namespace)

    @JvmStatic
    fun  marshall(value: T, namespace: FlowAddress): Field<*> = JsonCadenceMarshalling.marshall(value, namespace)

    @JvmStatic
    @JvmOverloads
    fun  marshall(value: T, namespace: CadenceNamespace = CadenceNamespace()): Field<*> = JsonCadenceMarshalling.marshall(value, namespace)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy