Skip to content

Commit

Permalink
Merge pull request #7 from netkicorp/upgrade_library
Browse files Browse the repository at this point in the history
Upgrading to library 0.1.0-alpha16
  • Loading branch information
alan10fm authored Oct 22, 2020
2 parents 254fd77 + 96fb91c commit c9460de
Show file tree
Hide file tree
Showing 10 changed files with 280 additions and 29 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ Description: `Send a GET request to this to receive back an invoiceRequest binar
Verb: `GET`
Response: `Binary invoiceRequest`

### Initial invoice request Encrypted

Endpoint: `/initial-invoice-request-encrypted`
Description: `Send a GET request to this to receive back an invoiceRequest binary encrypted so that you can test parsing thing`
Verb: `GET`
Response: `Binary invoiceRequest encrypted`

Verb: `POST`
Description: `If you want to test your full flow with getting an invoiceRequest object at your correct endpoint use the POST as described and it will send the binary object to that URL.`
Params:
Expand Down Expand Up @@ -91,3 +98,11 @@ Verb: `POST`
Params:
- payment: `Binary containing payment`
Response: `Binary containing paymentAck`

### Encryption
You can generate EncryptedMessages to test this functionality. Once you start the service you can fetch a set of ECDSA keys to use in your ProtocolMessages.

Endpoint: `/encryption/keys`
Description: `Send a GET request to this to receive a set of sender/recipient keys to test encrypted messages`
Verb: `GET`
Response: `Set of keys to do encryption`
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ plugins {
}

group = "com.netki"
version = "0.1.0-alpha15"
version = "0.1.0-alpha16"
java.sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
Expand All @@ -18,7 +18,7 @@ repositories {
}

dependencies {
implementation("com.netki:transactid:0.1.0-alpha15")
implementation("com.netki:transactid:0.1.0-alpha16")

implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,33 @@
package com.netki.transactidlibraryjavademo

import com.netki.transactidlibraryjavademo.model.EncryptionKeys
import com.netki.transactidlibraryjavademo.util.CryptoModule
import com.netki.transactidlibraryjavademo.util.KeyGenerator.Keys.generateKeyPairECDSA
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
import org.springframework.context.annotation.Bean
import java.security.AlgorithmParameters
import java.security.KeyPair
import java.security.KeyPairGenerator
import java.security.SecureRandom
import java.security.spec.ECGenParameterSpec
import java.security.spec.ECParameterSpec

@SpringBootApplication
class TransactidLibraryJavaDemoApplication
class TransactidLibraryJavaDemoApplication {

@Bean
fun getEncryptionKeys(): EncryptionKeys {
val senderKeys = generateKeyPairECDSA()
val recipientKeys = generateKeyPairECDSA()
return EncryptionKeys(
CryptoModule.objectToPrivateKeyPem(senderKeys.private),
CryptoModule.objectToPublicKeyPem(senderKeys.public),
CryptoModule.objectToPrivateKeyPem(recipientKeys.private),
CryptoModule.objectToPublicKeyPem(recipientKeys.public)
)
}
}

fun main(args: Array<String>) {
runApplication<TransactidLibraryJavaDemoApplication>(*args)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.netki.transactidlibraryjavademo.controller

import com.netki.transactidlibraryjavademo.model.EncryptionKeys
import com.netki.transactidlibraryjavademo.service.TransactIdService
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter
Expand All @@ -20,6 +21,18 @@ class TransactIdController {
@Autowired
private lateinit var transactIdService: TransactIdService

@Operation(
summary = "Get Encryption keys",
description = "Get the set of keys for the sender/recipient, this is needed if you want to generate encrypted messages"
)
@RequestMapping(
method = [RequestMethod.GET],
value = ["/encryption/keys"],
produces = [MediaType.APPLICATION_JSON_VALUE]
)
fun getEncryptionKeys(): ResponseEntity<EncryptionKeys> =
ResponseEntity.ok(transactIdService.getEncryptionKeys())

@Operation(
summary = "Get invoiceRequest binary",
description = "Request receive back an invoiceRequest binary so that you can test parsing things"
Expand All @@ -29,8 +42,24 @@ class TransactIdController {
value = ["/initial-invoice-request"],
produces = [MediaType.APPLICATION_OCTET_STREAM_VALUE]
)
fun getInitialInvoiceRequest(): ResponseEntity<ByteArray> =
ResponseEntity.ok(transactIdService.getInitialInvoiceRequest())
fun getInitialInvoiceRequest() = ResponseEntity(
transactIdService.getInitialInvoiceRequest(),
HttpStatus.CREATED
)

@Operation(
summary = "Get invoiceRequest binary encrypted",
description = "Request receive back an invoiceRequest binary encrypted so that you can test parsing things"
)
@RequestMapping(
method = [RequestMethod.GET],
value = ["/initial-invoice-request-encrypted"],
produces = [MediaType.APPLICATION_OCTET_STREAM_VALUE]
)
fun getInitialInvoiceRequestEncrypted() = ResponseEntity(
transactIdService.getInitialInvoiceRequestEncrypted(),
HttpStatus.CREATED
)

@Operation(
summary = "Post invoiceRequest binary",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.netki.transactidlibraryjavademo.model

data class EncryptionKeys(
val senderPrivateKey: String,
val senderPublicKey: String,
val recipientPrivateKey: String,
val recipientPublicKey: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.netki.sapphire.model

data class ServiceError(
var type: ServiceErrorType,
val message: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.netki.sapphire.model

enum class ServiceErrorType {
ADDRESS_PROVIDER_ERROR,
ADDRESS_PROVIDER_UNAUTHORIZED,
CERTIFICATE_PROVIDER,
CERTIFICATE_PROVIDER_UNAUTHORIZED,
INVALID_CERTIFICATE_CHAIN,
INVALID_CERTIFICATE,
INVALID_OBJECT,
INVALID_OWNERS,
INVALID_PRIVATE_KEY,
INVALID_SIGNATURE,
KEY_MANAGEMENT_FETCH,
KEY_MANAGEMENT_STORE,
OBJECT_NOT_FOUND,
INVALID_DATA,
ENCRYPTION_ERROR,
UNKNOWN
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,59 @@
package com.netki.transactidlibraryjavademo.service

import com.netki.TransactId
import com.netki.model.EncryptionParameters
import com.netki.model.MessageInformation
import com.netki.model.RecipientParameters
import com.netki.model.SenderParameters
import com.netki.transactidlibraryjavademo.model.EncryptionKeys
import com.netki.transactidlibraryjavademo.util.TestData.Attestations.ATTESTATIONS_REQUESTED
import com.netki.transactidlibraryjavademo.util.TestData.InvoiceRequest.INVOICE_REQUEST_DATA
import com.netki.transactidlibraryjavademo.util.TestData.Owners.NO_PRIMARY_OWNER_PKI_X509SHA256
import com.netki.transactidlibraryjavademo.util.TestData.Owners.PRIMARY_OWNER_PKI_X509SHA256
import com.netki.transactidlibraryjavademo.util.TestData.Payment.PAYMENT_PARAMETERS
import com.netki.transactidlibraryjavademo.util.TestData.PaymentRequest.PAYMENT_REQUEST_PARAMETERS
import com.netki.transactidlibraryjavademo.util.TestData.PkiData.PKI_DATA_SENDER_X509SHA256
import com.netki.transactidlibraryjavademo.util.TestData.Senders.SENDER_PKI_X509SHA256
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Service
import javax.annotation.PostConstruct

@Service
class TransactIdService {

@Autowired
private lateinit var encryptionKeys: EncryptionKeys
private lateinit var recipientParameters: RecipientParameters
private lateinit var senderParameters: SenderParameters

private val logger = LoggerFactory.getLogger(this.javaClass)
private var transactId = TransactId.getInstance("src/main/resources/certificates")
val ownerParameters = listOf(PRIMARY_OWNER_PKI_X509SHA256, NO_PRIMARY_OWNER_PKI_X509SHA256)
val messageInformationEncrypted = MessageInformation(
encryptMessage = true
)

@PostConstruct
fun setUp() {
recipientParameters = RecipientParameters(
"VASP_1",
"1234567890ABCD",
EncryptionParameters(
encryptionKeys.recipientPrivateKey,
encryptionKeys.recipientPublicKey
)
)
senderParameters = SenderParameters(
PKI_DATA_SENDER_X509SHA256,
EncryptionParameters(
encryptionKeys.senderPrivateKey,
encryptionKeys.senderPublicKey
)
)
}

fun getEncryptionKeys() = encryptionKeys

fun getInitialInvoiceRequest(): ByteArray {
logger.info("Creating InvoiceRequest...")
Expand All @@ -30,45 +67,107 @@ class TransactIdService {
return invoiceRequest
}

fun getInitialInvoiceRequestEncrypted(): ByteArray {
logger.info("Creating InvoiceRequest encrypted...")
val invoiceRequest = transactId.createInvoiceRequest(
INVOICE_REQUEST_DATA,
ownerParameters,
senderParameters,
ATTESTATIONS_REQUESTED,
recipientParameters,
messageInformationEncrypted
)
logger.info("Returning InvoiceRequest...")
return invoiceRequest
}

fun postInvoiceRequest(invoiceRequest: ByteArray): ByteArray {
logger.info("InvoiceRequest received")
logger.info("InvoiceRequest valid? ${transactId.isInvoiceRequestValid(invoiceRequest)}")
val invoiceRequestModel = transactId.parseInvoiceRequest(invoiceRequest)
logger.info(
"InvoiceRequest valid? ${transactId.isInvoiceRequestValid(
invoiceRequest,
recipientParameters
)}"
)
val invoiceRequestModel =
transactId.parseInvoiceRequest(invoiceRequest, recipientParameters)
logger.info("InvoiceRequest parsed: $invoiceRequestModel")

logger.info("Creating PaymentRequest...")
val paymentRequest = transactId.createPaymentRequest(
PAYMENT_REQUEST_PARAMETERS,
ownerParameters,
SENDER_PKI_X509SHA256,
ATTESTATIONS_REQUESTED,
1
)
logger.info("Returning PaymentRequest...")
return paymentRequest
return if (invoiceRequestModel.protocolMessageMetadata.encrypted) {
logger.info("Creating PaymentRequest Encrypted...")
logger.info("Returning PaymentRequest Encrypted...")
transactId.createPaymentRequest(
PAYMENT_REQUEST_PARAMETERS,
ownerParameters,
senderParameters,
ATTESTATIONS_REQUESTED,
1,
messageInformationEncrypted,
recipientParameters
)
} else {
logger.info("Creating PaymentRequest...")
logger.info("Returning PaymentRequest...")
transactId.createPaymentRequest(
PAYMENT_REQUEST_PARAMETERS,
ownerParameters,
SENDER_PKI_X509SHA256,
ATTESTATIONS_REQUESTED,
1
)
}
}

fun postPaymentRequest(paymentRequest: ByteArray): ByteArray {
logger.info("PaymentRequest received")
logger.info("PaymentRequest valid? ${transactId.isPaymentRequestValid(paymentRequest)}")
val paymentRequestModel = transactId.parsePaymentRequest(paymentRequest)
logger.info(
"PaymentRequest valid? ${transactId.isPaymentRequestValid(
paymentRequest,
recipientParameters
)}"
)
val paymentRequestModel =
transactId.parsePaymentRequest(paymentRequest, recipientParameters)
logger.info("PaymentRequest parsed: $paymentRequestModel")

logger.info("Creating Payment...")
val payment = transactId.createPayment(PAYMENT_PARAMETERS, ownerParameters)
logger.info("Returning Payment...")
return payment
return if (paymentRequestModel.protocolMessageMetadata.encrypted) {
logger.info("Creating Payment Encrypted...")
logger.info("Returning Payment Encrypted...")
transactId.createPayment(
PAYMENT_PARAMETERS,
ownerParameters,
messageInformationEncrypted,
senderParameters,
recipientParameters
)

} else {
logger.info("Creating Payment...")
logger.info("Returning Payment...")
transactId.createPayment(PAYMENT_PARAMETERS, ownerParameters)
}
}

fun postPayment(payment: ByteArray): ByteArray {
logger.info("Payment received")
logger.info("Payment valid? ${transactId.isPaymentValid(payment)}")
val paymentModel = transactId.parsePayment(payment)
logger.info("Payment valid? ${transactId.isPaymentValid(payment, recipientParameters)}")
val paymentModel = transactId.parsePayment(payment, recipientParameters)
logger.info("Payment parsed: $paymentModel")

logger.info("Creating PaymentAck...")
val paymentAck = transactId.createPaymentAck(paymentModel, "Payment successful")
logger.info("Returning PaymentAck...")
return paymentAck
return if (paymentModel.protocolMessageMetadata!!.encrypted) {
logger.info("Creating PaymentAck Encrypted...")
logger.info("Returning PaymentAck Encrypted...")
transactId.createPaymentAck(
paymentModel,
"Payment successful",
messageInformationEncrypted,
senderParameters,
recipientParameters
)
} else {
logger.info("Creating PaymentAck...")
logger.info("Returning PaymentAck...")
transactId.createPaymentAck(paymentModel, "Payment successful")
}
}
}
Loading

0 comments on commit c9460de

Please sign in to comment.