Skip to content

Commit

Permalink
Merge branch 'release/next'
Browse files Browse the repository at this point in the history
  • Loading branch information
kiltbot committed Jan 15, 2020
2 parents 988cabf + 41f1532 commit 8e10162
Show file tree
Hide file tree
Showing 30 changed files with 1,122 additions and 448 deletions.
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@kiltprotocol/sdk-js",
"version": "0.16.2",
"version": "0.17.0",
"description": "",
"main": "./build/index.js",
"typings": "./build/index.d.ts",
Expand All @@ -15,7 +15,7 @@
"style:fix": "yarn style --write",
"build": "tsc --declaration",
"build:docs": "typedoc --theme default --out docs/api && touch docs/.nojekyll",
"buildAndPublish": "yarn build && node ./npm-create-rc.js && npm publish"
"buildAndPublish": "yarn build && node ./npm-create-rc.js && npm publish --access public"
},
"husky": {
"hooks": {
Expand Down Expand Up @@ -49,7 +49,6 @@
"@commitlint/cli": "^7.2.1",
"@commitlint/config-conventional": "^7.1.2",
"@types/jest": "^23.3.9",
"@types/lodash": "^4.14.138",
"@types/uuid": "^3.4.4",
"@typescript-eslint/eslint-plugin": "^2.8.0",
"@typescript-eslint/parser": "^2.8.0",
Expand Down Expand Up @@ -81,7 +80,6 @@
"ajv": "^6.6.1",
"bn.js": "^4.11.8",
"jsonabc": "^2.3.1",
"lodash": "^4.17.11",
"typescript-logging": "^0.6.3",
"uuid": "^3.3.2"
},
Expand Down
2 changes: 1 addition & 1 deletion src/attestation/Attestation.chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ function decode(encoded: QueryResult, claimHash: string): Attestation | null {
revoked: attestationTuple[3],
}
log.info(`Decoded attestation: ${JSON.stringify(attestation)}`)
return Attestation.fromObject(attestation)
return Attestation.fromAttestation(attestation)
}
}
return null
Expand Down
67 changes: 46 additions & 21 deletions src/attestation/Attestation.spec.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { Text } from '@polkadot/types'
import Bool from '@polkadot/types/primitive/Bool'
import { Tuple } from '@polkadot/types/codec'
import Crypto from '../crypto'
import Identity from '../identity/Identity'
import Attestation from './Attestation'
import CType from '../ctype/CType'
import IAttestation from '../types/Attestation'
import ICType from '../types/CType'
import RequestForAttestation from '../requestforattestation/RequestForAttestation'
import IClaim from '../types/Claim'
import Claim from '../claim/Claim'

jest.mock('../blockchainApiConnection/BlockchainApiConnection')

Expand All @@ -15,28 +17,50 @@ describe('Attestation', () => {

const Blockchain = require('../blockchain/Blockchain').default

const cTypeHash = Crypto.hashStr('testCtype')
const claim = {
cType: cTypeHash,
contents: {},
owner: identityBob.address,
} as IClaim
const requestForAttestation: RequestForAttestation = new RequestForAttestation(
claim,
const rawCType: ICType['schema'] = {
$id: 'http://example.com/ctype-1',
$schema: 'http://kilt-protocol.org/draft-01/ctype#',
properties: {
name: { type: 'string' },
},
type: 'object',
}

const fromRawCType: ICType = {
schema: rawCType,
owner: identityAlice.address,
hash: '',
}

const testCType: CType = CType.fromCType(fromRawCType)

const testcontents = {}
const testClaim = Claim.fromCTypeAndClaimContents(
testCType,
testcontents,
identityBob.address
)
const requestForAttestation: RequestForAttestation = RequestForAttestation.fromClaimAndIdentity(
testClaim,
identityBob,
[],
identityBob
null
)

it('stores attestation', async () => {
Blockchain.api.query.attestation.attestations = jest.fn(() => {
const tuple = new Tuple(
[Text, Text, Text, Bool],
[cTypeHash, identityAlice.address, undefined, false]
[testCType.hash, identityAlice.address, undefined, false]
)
return Promise.resolve(tuple)
})

const attestation = new Attestation(requestForAttestation, identityAlice)
const attestation: Attestation = Attestation.fromRequestAndPublicIdentity(
requestForAttestation,
identityAlice,
null
)
expect(await attestation.verify()).toBeTruthy()
})

Expand All @@ -45,11 +69,12 @@ describe('Attestation', () => {
return Promise.resolve(new Tuple([], []))
})

const attestation = new Attestation(
requestForAttestation,
identityAlice,
false
)
const attestation: Attestation = Attestation.fromAttestation({
claimHash: requestForAttestation.rootHash,
cTypeHash: testCType.hash,
owner: identityAlice.address,
revoked: false,
} as IAttestation)
expect(await attestation.verify()).toBeFalsy()
})

Expand All @@ -59,15 +84,15 @@ describe('Attestation', () => {
new Tuple(
// Attestations: claim-hash -> (ctype-hash, account, delegation-id?, revoked)
[Text, Text, Text, Bool],
[cTypeHash, identityAlice, undefined, true]
[testCType.hash, identityAlice, undefined, true]
)
)
})

const attestation = new Attestation(
const attestation: Attestation = Attestation.fromRequestAndPublicIdentity(
requestForAttestation,
identityAlice,
true
null
)
expect(await attestation.verify()).toBeFalsy()
})
Expand Down
108 changes: 72 additions & 36 deletions src/attestation/Attestation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,18 @@
/**
* Dummy comment needed for correct doc display, do not remove.
*/
import IRequestForAttestation from '../types/RequestForAttestation'
import TxStatus from '../blockchain/TxStatus'
import { factory } from '../config/ConfigLog'
import Identity from '../identity/Identity'
import IAttestation from '../types/Attestation'
import IRequestForAttestation from '../types/RequestForAttestation'
import { revoke, query, store } from './Attestation.chain'
import { IDelegationBaseNode } from '../types/Delegation'
import IPublicIdentity from '../types/PublicIdentity'

const log = factory.getLogger('Attestation')

export default class Attestation implements IAttestation {
public claimHash: IAttestation['claimHash']
public cTypeHash: IAttestation['cTypeHash']
public owner: IAttestation['owner']
public revoked: IAttestation['revoked']
public delegationId: IAttestation['delegationId'] | null

/**
* Builds a new [[Attestation]] instance.
*
* @param requestForAttestation - A request for attestation, usually sent by a claimer.
* @param attester - The identity of the attester.
* @param revoked - Whether the attestation should be revoked.
* @example ```javascript
* // create an attestation, e.g. to store it on-chain
* new Attestation(requestForAttestation, attester);
* ```
*/
public constructor(
requestForAttestation: IRequestForAttestation,
attester: Identity,
revoked = false
) {
this.owner = attester.address
this.claimHash = requestForAttestation.hash
this.cTypeHash = requestForAttestation.claim.cType
this.delegationId = requestForAttestation.delegationId
this.revoked = revoked
}

/**
* [STATIC] [ASYNC] Queries the chain for a given attestation, by `claimHash`.
*
Expand Down Expand Up @@ -90,16 +63,79 @@ export default class Attestation implements IAttestation {
* [STATIC] Builds an instance of [[Attestation]], from a simple object with the same properties.
* Used for deserialization.
*
* @param obj - The base object from which to create the attestation.
* @param attestationInput - The base object from which to create the attestation.
* @returns A new [[Attestation]] object.
* @example ```javascript
* // create an Attestation object, so we can call methods on it (`serialized` is a serialized Attestation object )
* Attestation.fromObject(JSON.parse(serialized));
* Attestation.fromAttestation(JSON.parse(serialized));
* ```
*/
public static fromObject(obj: IAttestation): Attestation {
const newAttestation: Attestation = Object.create(Attestation.prototype)
return Object.assign(newAttestation, obj)
public static fromAttestation(attestationInput: IAttestation): Attestation {
return new Attestation(attestationInput)
}

/**
* [STATIC] Builds a new instance of an [[Attestation]], from a complete set of input required for an attestation.
*
* @param request - The base request for attestation.
* @param attesterPublicIdentity - The attesters public identity, used to attest the underlying claim.
* @param [delegationIdInput] - optional delegationId for which the attester attests the claim.
* @returns A new [[Attestation]] object.
* @example ```javascript
* // create a complete new attestation from the RequestForAttestation and all other needed properties
* Attestation.fromRequestAndPublicIdentity(request, attesterPublicIdentity, delegationId);
* ```
*/
public static fromRequestAndPublicIdentity(
request: IRequestForAttestation,
attesterPublicIdentity: IPublicIdentity,
delegationIdInput: IDelegationBaseNode['id'] | null
) {
return new Attestation({
claimHash: request.rootHash,
cTypeHash: request.claim.cTypeHash,
owner: attesterPublicIdentity.address,
delegationId: delegationIdInput,
revoked: false,
})
}

public claimHash: IAttestation['claimHash']
public cTypeHash: IAttestation['cTypeHash']
public owner: IAttestation['owner']
public revoked: IAttestation['revoked']
public delegationId: IAttestation['delegationId'] | null

/**
* Builds a new [[Attestation]] instance.
*
* @param attestationInput - The base object from which to create the attestation.
* @example ```javascript
* // create an attestation, e.g. to store it on-chain
* new Attestation(attestationInput);
* ```
*/
public constructor(attestationInput: IAttestation) {
if (
!attestationInput.cTypeHash ||
!attestationInput.claimHash ||
!attestationInput.owner
) {
throw new Error(
`Property Not Provided while building Attestation!\n
attestationInput.cTypeHash:\n
${attestationInput.cTypeHash}\n
attestationInput.claimHash:\n
${attestationInput.claimHash}\n
attestationInput.owner:\n
${attestationInput.owner}`
)
}
this.owner = attestationInput.owner
this.claimHash = attestationInput.claimHash
this.cTypeHash = attestationInput.cTypeHash
this.delegationId = attestationInput.delegationId
this.revoked = attestationInput.revoked
}

/**
Expand All @@ -110,7 +146,7 @@ export default class Attestation implements IAttestation {
* @example ```javascript
* // Use [[store]] to store an attestation on chain, and to create an [[AttestedClaim]] upon success:
* attestation.store(attester).then(() => {
* // the attestation was successfully stored, so now we can for example create an AttestedClaim
* // the attestation was successfully stored, so now we can for example create an AttestedClaim
* });
* ```
*/
Expand Down
55 changes: 39 additions & 16 deletions src/attestedclaim/AttestedClaim.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import Identity from '../identity/Identity'
import RequestForAttestation from '../requestforattestation/RequestForAttestation'
import AttestedClaim from './AttestedClaim'
import Attestation from '../attestation/Attestation'
import IClaim from '../types/Claim'
import CType from '../ctype/CType'
import ICType from '../types/CType'
import RequestForAttestation from '../requestforattestation/RequestForAttestation'
import Claim from '../claim/Claim'

function buildAttestedClaim(
claimer: Identity,
Expand All @@ -12,35 +14,56 @@ function buildAttestedClaim(
legitimations: AttestedClaim[]
): AttestedClaim {
// create claim
const claim = {
cType: ctype,
const identityAlice = Identity.buildFromURI('//Alice')

const rawCType: ICType['schema'] = {
$id: 'http://example.com/ctype-1',
$schema: 'http://kilt-protocol.org/draft-01/ctype#',
properties: {
name: { type: 'string' },
},
type: 'object',
}

const fromRawCType: ICType = {
schema: rawCType,
owner: identityAlice.address,
hash: '',
}

const testCType: CType = CType.fromCType(fromRawCType)

const claim = Claim.fromCTypeAndClaimContents(
testCType,
contents,
owner: claimer.address,
} as IClaim
claimer.address
)
// build request for attestation with legimitations
const requstForAttestation: RequestForAttestation = new RequestForAttestation(
const requestForAttestation = RequestForAttestation.fromClaimAndIdentity(
claim,
claimer,
legitimations,
claimer
null
)
// build attestation
const attestation: Attestation = new Attestation(
requstForAttestation,
attester
const testAttestation: Attestation = Attestation.fromRequestAndPublicIdentity(
requestForAttestation,
attester,
null
)
// combine to attested claim
const attestedClaim: AttestedClaim = new AttestedClaim(
requstForAttestation,
attestation
const attestedClaim: AttestedClaim = AttestedClaim.fromRequestAndAttestation(
requestForAttestation,
testAttestation
)
return attestedClaim
}

describe('RequestForAttestation', () => {
const identityAlice = Identity.buildFromURI('//Alice')

const identityBob = Identity.buildFromURI('//Bob')
const identityCharlie = Identity.buildFromURI('//Charlie')
const identityDoria = Identity.buildFromURI('//Doria')

const legitimation: AttestedClaim = buildAttestedClaim(
identityAlice,
Expand All @@ -53,7 +76,7 @@ describe('RequestForAttestation', () => {
it('verify attested claims', async () => {
const attestedClaim: AttestedClaim = buildAttestedClaim(
identityCharlie,
identityDoria,
identityAlice,
'ctype',
{
a: 'a',
Expand Down
Loading

0 comments on commit 8e10162

Please sign in to comment.