Joel
Joel

Reputation: 23160

Class is not annotated or on the whitelist, so cannot be used in serialization

In Corda, I have defined the following flow:

object Flow {
    @InitiatingFlow
    @StartableByRPC
    class Initiator(val otherParty: Party) : FlowLogic<Unit>() {
        override val progressTracker = ProgressTracker()

        @Suspendable
        override fun call() {
            val otherPartyFlow = initiateFlow(otherParty)
            otherPartyFlow.send(MyClass())
        }
    }

    @InitiatedBy(Initiator::class)
    class Acceptor(val otherPartyFlow: FlowSession) : FlowLogic<Unit>() {
        @Suspendable
        override fun call() {
            val unregisteredClassInstance = otherPartyFlow.receive<MyClass>()
        }
    }
}

However, when I try and run the flow, I get the following error:

Class com.example.flow.MyClass is not annotated or on the whitelist, so cannot be used in serialization

How can I annotate or whitelist the class to allow it to be sent within the flow? Why do I need to do this?

Upvotes: 1

Views: 2002

Answers (1)

Joel
Joel

Reputation: 23160

By default, for security purposes, only classes present on the default serialization whitelist can be sent within flows or over RPC.

There are two ways to add a specific class to the serialization whitelist:

1. Annotating the class as @CordaSerializable:

@CordaSerializable
class MyClass

2. Creating a serialization whitelist plugin:

Define a serialisation plugin as follows:

class TemplateSerializationWhitelist : SerializationWhitelist {
    override val whitelist: List<Class<*>> = listOf(MyClass::class.java)
}

Then list the serialization whitelist plugin's fully-qualified class name (e.g. com.example.TemplateSerializationWhitelist in a file called net.corda.core.serialization.SerializationWhitelist in the src/main/resources/META-INF/services folder of your CorDapp.

Why are there two ways to add a class to the serialization whitelist?

The first approach is easier, but is not possible when you're not able to add the annotation to the class you wish to send.

Upvotes: 6

Related Questions