Skip to content

Commit 3bb9997

Browse files
Updated signature implementation and added initial transaction specifications.
1 parent f545494 commit 3bb9997

18 files changed

+518
-4
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ buildscript {
4141
}
4242

4343
group 'io.onixlabs'
44-
version '2.1.2'
44+
version '3.0.0-rc1'
4545

4646
subprojects {
4747
repositories {

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name=onixlabs-corda-core
22
group=io.onixlabs
3-
version=2.1.2
3+
version=3.0.0-rc1
44

55
kotlin.incremental=false
66
kotlin.code.style=official
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright 2020-2021 ONIXLabs
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.onixlabs.corda.core.contract
18+
19+
import net.corda.core.crypto.SecureHash
20+
21+
/**
22+
* Sorts and reduces an [Iterable] of [SecureHash] into a single hash.
23+
*
24+
* @return Returns a single hash representing the sorted and reduced input hashes.
25+
*/
26+
fun Iterable<SecureHash>.sortAndReduce(): SecureHash {
27+
return sorted().reduce { lhs, rhs -> lhs.concatenate(rhs) }
28+
}

onixlabs-corda-core-contract/src/main/kotlin/io/onixlabs/corda/core/contract/SignatureData.kt

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import net.corda.core.crypto.sign
2222
import net.corda.core.node.ServiceHub
2323
import net.corda.core.node.services.KeyManagementService
2424
import net.corda.core.serialization.CordaSerializable
25+
import net.corda.core.utilities.toBase58String
2526
import net.corda.core.utilities.toBase64
2627
import java.security.PrivateKey
2728
import java.security.PublicKey
@@ -81,14 +82,26 @@ data class SignatureData(val content: ByteArray, val signature: DigitalSignature
8182
}
8283
}
8384

85+
/**
86+
* Determines whether the signature data was signed by the specified key.
87+
*
88+
* @param publicKey The public key to verify against the signature.
89+
* @return Returns true if the signature data was signed by the specified key; otherwise, false.
90+
*/
91+
fun isValid(publicKey: PublicKey): Boolean {
92+
return Crypto.isValid(publicKey, signature.bytes, content)
93+
}
94+
8495
/**
8596
* Verifies the signature data using the specified public key.
8697
*
8798
* @param publicKey The public key to verify against the signature.
8899
* @return Returns true if the public key was used to sign the data; otherwise, false.
89100
*/
90-
fun verify(publicKey: PublicKey): Boolean {
91-
return Crypto.isValid(publicKey, signature.bytes, content)
101+
fun verify(publicKey: PublicKey) {
102+
if (!isValid(publicKey)) {
103+
throw SignatureException("Signature was not signed by the specified key: ${publicKey.toBase58String()}")
104+
}
92105
}
93106

94107
/**
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2020-2021 ONIXLabs
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.onixlabs.corda.core.specifications
18+
19+
import net.corda.core.contracts.CommandData
20+
import java.security.PublicKey
21+
22+
/**
23+
* Represents a specification that determines whether a command has been signed by the specified signing key.
24+
*
25+
* @param signingKey The signing key for which the command should be signed.
26+
* @param type The command type that should be signed.
27+
*/
28+
class HasCommandSignatureOfType(signingKey: PublicKey, type: Class<out CommandData>) :
29+
TransactionFunctionSpecification({ signingKey in commandsOfType(type).flatMap { it.signers } })
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2020-2021 ONIXLabs
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.onixlabs.corda.core.specifications
18+
19+
import net.corda.core.contracts.ContractState
20+
21+
/**
22+
* Represents a specification that determines whether a transaction contains the specified input type.
23+
*
24+
* @param type The contract state type that must be used as an input in the transaction.
25+
*/
26+
class HasInputsOfTypeTransactionSpecification(type: Class<out ContractState>) :
27+
TransactionFunctionSpecification({ inputsOfType(type).isNotEmpty() })
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2020-2021 ONIXLabs
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.onixlabs.corda.core.specifications
18+
19+
import net.corda.core.contracts.ContractState
20+
21+
/**
22+
* Represents a specification that determines whether a transaction contains the specified output type.
23+
*
24+
* @param type The contract state type that must be used as an output in the transaction.
25+
*/
26+
class HasOutputsOfTypeTransactionSpecification(type: Class<out ContractState>) :
27+
TransactionFunctionSpecification({ outputsOfType(type).isNotEmpty() })
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2020-2021 ONIXLabs
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.onixlabs.corda.core.specifications
18+
19+
import net.corda.core.contracts.ContractState
20+
21+
/**
22+
* Represents a specification that determines whether a transaction contains the specified reference type.
23+
*
24+
* @param type The contract state type that must be used as an reference in the transaction.
25+
*/
26+
class HasReferencesOfTypeTransactionSpecification(type: Class<out ContractState>) :
27+
TransactionFunctionSpecification({ referenceInputsOfType(type).isNotEmpty() })
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2020-2021 ONIXLabs
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.onixlabs.corda.core.specifications
18+
19+
import java.security.PublicKey
20+
21+
/**
22+
* Represents a specification that determines whether a transaction has been signed by the specified signing key.
23+
*
24+
* @param signingKey The signing key for which the transaction should be signed.
25+
*/
26+
class HasSignatureTransactionSpecification(signingKey: PublicKey) :
27+
TransactionFunctionSpecification({ signingKey in commands.flatMap { it.signers } })
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Copyright 2020-2021 ONIXLabs
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package io.onixlabs.corda.core.specifications
18+
19+
import net.corda.core.contracts.ContractState
20+
21+
/**
22+
* Represents a specification that determines whether a transaction contains the specified input type.
23+
*
24+
* @param type The contract state type that must be used as a single input in the transaction.
25+
*/
26+
class HasSingleInputOfTypeTransactionSpecification(type: Class<out ContractState>) :
27+
TransactionFunctionSpecification({ inputsOfType(type).size == 1 })

0 commit comments

Comments
 (0)