Skip to content

Commit 1c65a76

Browse files
Added claims to accounts.
1 parent dcd8e82 commit 1c65a76

File tree

5 files changed

+127
-16
lines changed

5 files changed

+127
-16
lines changed

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ buildscript {
1414
junit_version = '5.3.1'
1515

1616
onixlabs_group = 'io.onixlabs'
17-
onixlabs_corda_core_release_version = '2.1.2'
17+
onixlabs_corda_core_release_version = '3.0.0-rc1'
1818

1919
cordapp_platform_version = 10
2020
cordapp_contract_name = 'ONIXLabs Corda Identity Framework Contract'
@@ -43,8 +43,8 @@ buildscript {
4343
}
4444
}
4545

46-
group 'io.onixlabs'
47-
version '3.0.0-rc1'
46+
group getProperty('group')
47+
version getProperty('version')
4848

4949
subprojects {
5050
repositories {

onixlabs-corda-identity-framework-contract/src/main/kotlin/io/onixlabs/corda/identityframework/contract/accounts/Account.kt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package io.onixlabs.corda.identityframework.contract.accounts
1818

1919
import io.onixlabs.corda.identityframework.contract.accounts.AccountSchema.AccountEntity
2020
import io.onixlabs.corda.identityframework.contract.accounts.AccountSchema.AccountSchemaV1
21+
import io.onixlabs.corda.identityframework.contract.claims.AbstractClaim
2122
import io.onixlabs.corda.identityframework.contract.toDataClassString
2223
import net.corda.core.contracts.BelongsToContract
2324
import net.corda.core.contracts.LinearState
@@ -38,6 +39,7 @@ import java.util.*
3839
@BelongsToContract(AccountContract::class)
3940
open class Account(
4041
val owner: AbstractParty,
42+
val claims: Set<AbstractClaim<*>> = emptySet(),
4143
override val linearId: UniqueIdentifier = UniqueIdentifier()
4244
) : LinearState, QueryableState {
4345

@@ -51,11 +53,7 @@ open class Account(
5153
* @return Returns a persistent state entity.
5254
*/
5355
override fun generateMappedObject(schema: MappedSchema): PersistentState = when (schema) {
54-
is AccountSchemaV1 -> AccountEntity(
55-
linearId = linearId.id,
56-
externalId = linearId.externalId,
57-
owner = owner
58-
)
56+
is AccountSchemaV1 -> AccountEntity.fromAccount(this)
5957
else -> throw IllegalArgumentException("Unrecognised schema: $schema.")
6058
}
6159

onixlabs-corda-identity-framework-contract/src/main/kotlin/io/onixlabs/corda/identityframework/contract/accounts/AccountParty.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class AccountParty internal constructor(
109109
* @return Returns a string that represents the current object.
110110
*/
111111
override fun toString(): String {
112-
return "$party@$accountLinearId"
112+
return "$accountLinearId@$party"
113113
}
114114

115115
/**

onixlabs-corda-identity-framework-contract/src/main/kotlin/io/onixlabs/corda/identityframework/contract/accounts/AccountSchema.kt

Lines changed: 82 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,30 @@
1616

1717
package io.onixlabs.corda.identityframework.contract.accounts
1818

19+
import io.onixlabs.corda.identityframework.contract.claims.AbstractClaim
1920
import net.corda.core.crypto.NullKeys.NULL_PARTY
21+
import net.corda.core.crypto.SecureHash
2022
import net.corda.core.identity.AbstractParty
2123
import net.corda.core.schemas.MappedSchema
2224
import net.corda.core.schemas.PersistentState
2325
import java.util.*
24-
import javax.persistence.Column
25-
import javax.persistence.Entity
26-
import javax.persistence.Table
26+
import javax.persistence.*
2727

2828
object AccountSchema {
2929

30-
object AccountSchemaV1 : MappedSchema(AccountSchema.javaClass, 1, listOf(AccountEntity::class.java)) {
30+
object AccountSchemaV1 :
31+
MappedSchema(AccountSchema.javaClass, 1, listOf(AccountEntity::class.java, AccountClaim::class.java)) {
3132
override val migrationResource: String = "account-schema.changelog-master"
3233
}
3334

3435
@Entity
35-
@Table(name = "onixlabs_account_states")
36+
@Table(
37+
name = "onixlabs_account_states",
38+
indexes = [
39+
Index(name = "account_id_index", columnList = "linear_id"),
40+
Index(name = "owner_index", columnList = "owner")
41+
]
42+
)
3643
class AccountEntity(
3744
@Column(name = "linear_id", nullable = false)
3845
val linearId: UUID = UUID.randomUUID(),
@@ -41,6 +48,74 @@ object AccountSchema {
4148
val externalId: String? = null,
4249

4350
@Column(name = "owner", nullable = false)
44-
val owner: AbstractParty = NULL_PARTY
45-
) : PersistentState()
51+
val owner: AbstractParty = NULL_PARTY,
52+
53+
@OneToMany(fetch = FetchType.LAZY, cascade = [CascadeType.PERSIST])
54+
@JoinColumns(
55+
JoinColumn(name = "transaction_id", referencedColumnName = "transaction_id"),
56+
JoinColumn(name = "output_index", referencedColumnName = "output_index")
57+
)
58+
@OrderColumn
59+
val claims: MutableSet<AccountClaim> = mutableSetOf()
60+
) : PersistentState() {
61+
62+
companion object {
63+
fun fromAccount(account: Account): AccountEntity {
64+
val accountEntity = AccountEntity(
65+
linearId = account.linearId.id,
66+
externalId = account.linearId.externalId,
67+
owner = account.owner
68+
)
69+
70+
mapClaimsToAccountClaims(accountEntity, account.claims)
71+
72+
return accountEntity
73+
}
74+
75+
private fun mapClaimsToAccountClaims(accountEntity: AccountEntity, claims: Set<AbstractClaim<*>>) {
76+
claims.forEach {
77+
val hash = with(it) { SecureHash.sha256("$property$value${value.javaClass}$javaClass") }
78+
79+
val claim = AccountClaim(
80+
property = it.property,
81+
value = it.value.toString(),
82+
hash = hash.toString(),
83+
account = accountEntity
84+
)
85+
86+
accountEntity.claims.add(claim)
87+
}
88+
}
89+
}
90+
}
91+
92+
@Entity
93+
@Table(
94+
name = "onixlabs_account_claims",
95+
indexes = [
96+
Index(name = "account_claim_index", columnList = "hash", unique = false)
97+
]
98+
)
99+
class AccountClaim(
100+
@Id
101+
@GeneratedValue
102+
@Column(name = "id", unique = true, nullable = false)
103+
val id: UUID = UUID.randomUUID(),
104+
105+
@Column(name = "property", nullable = false)
106+
val property: String = "",
107+
108+
@Column(name = "value", nullable = false, columnDefinition = "clob")
109+
val value: String = "",
110+
111+
@Column(name = "hash", nullable = false)
112+
val hash: String = "",
113+
114+
@ManyToOne(fetch = FetchType.LAZY)
115+
@JoinColumns(
116+
JoinColumn(name = "transaction_id", referencedColumnName = "transaction_id"),
117+
JoinColumn(name = "output_index", referencedColumnName = "output_index")
118+
)
119+
val account: AccountEntity = AccountEntity()
120+
)
46121
}

onixlabs-corda-identity-framework-contract/src/main/resources/migration/account-schema.changelog-v1.xml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,43 @@
2121
<addPrimaryKey columnNames="output_index, transaction_id"
2222
constraintName="PK_onixlabs_account_states"
2323
tableName="onixlabs_account_states"/>
24+
<createIndex tableName="onixlabs_account_states" indexName="IDX_account_id">
25+
<column name="linear_id"/>
26+
</createIndex>
27+
<createIndex tableName="onixlabs_account_states" indexName="IDX_owner">
28+
<column name="owner"/>
29+
</createIndex>
30+
</changeSet>
31+
<changeSet author="ONIXLabs" id="create-onixlabs_account_claims">
32+
<createTable tableName="onixlabs_account_claims">
33+
<column name="output_index" type="int">
34+
<constraints nullable="false"/>
35+
</column>
36+
<column name="transaction_id" type="nvarchar(64)">
37+
<constraints nullable="false"/>
38+
</column>
39+
<column name="id" type="uuid">
40+
<constraints nullable="false" unique="true"/>
41+
</column>
42+
<column name="property" type="nvarchar(256)">
43+
<constraints nullable="false"/>
44+
</column>
45+
<column name="value" type="clob">
46+
<constraints nullable="false"/>
47+
</column>
48+
<column name="hash" type="nvarchar(64)">
49+
<constraints nullable="false"/>
50+
</column>
51+
</createTable>
52+
<addPrimaryKey columnNames="id"
53+
constraintName="PK_onixlabs_account_claims"
54+
tableName="onixlabs_account_claims"/>
55+
<createIndex tableName="onixlabs_account_claims" indexName="IDX_account_claim_index">
56+
<column name="hash"/>
57+
</createIndex>
58+
<createIndex tableName="onixlabs_account_claims" indexName="IDX_transaction_reference">
59+
<column name="output_index"/>
60+
<column name="transaction_id"/>
61+
</createIndex>
2462
</changeSet>
2563
</databaseChangeLog>

0 commit comments

Comments
 (0)