Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Verifying transactions with csv attachments - message contract verify function
override fun verify(tx: LedgerTransaction) {
val command = tx.commands.requireSingleCommand<Commands>()
when (command.value) {
is Commands.Send -> requireThat {
"No inputs should be consumed when sending a message." using (tx.inputs.isEmpty())
"Only one output state should be created when sending a message." using (tx.outputs.size == 1)
}
is Commands.Reply -> requireThat {
"One input should be consumed when replying to a message." using (tx.inputs.size == 1)
"Only one output state should be created when replying to a message." using (tx.outputs.size == 1)
}
}
require(isMessageInCsv(tx)) {
"The output message must be contained within the csv of valid messages. " +
"See attachment with hash = ${tx.attachments.first().id} for its contents"
}
}
private fun isMessageInCsv(tx: LedgerTransaction): Boolean {
val message = tx.outputsOfType<MessageState>().first()
val attachmentId = tx.commandsOfType<CommandWithAttachmentId>().single().value.attachmentId
return tx.getAttachment(attachmentId).openAsJAR().use { zipInputStream: JarInputStream ->
zipInputStream.nextJarEntry.name
val csv = CSVFormat.DEFAULT.withHeader("valid_messages")
.withFirstRecordAsHeader()
.parse(InputStreamReader(zipInputStream))
csv.records.any { row -> row.get("valid messages") == message.contents }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.