Skip to content

Commit

Permalink
feat: export Blockchain utils (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
tjwelde authored Dec 3, 2020
1 parent 87ae6b8 commit e48f1e2
Show file tree
Hide file tree
Showing 19 changed files with 218 additions and 199 deletions.
12 changes: 6 additions & 6 deletions docs/example.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Kilt, {
IRevocationHandle,
PublicAttesterIdentity,
} from '../src'
import Blockchain, { IS_IN_BLOCK } from '../src/blockchain/Blockchain'
import { BlockchainUtils } from '../src/blockchain'
import constants from '../src/test/constants'

const NODE_URL = 'ws://127.0.0.1:9944'
Expand Down Expand Up @@ -82,11 +82,11 @@ async function setup(): Promise<{
// ! This costs tokens !
// Also note, that the completely same ctype can only be stored once on the blockchain.
try {
await ctype
.store(attester)
.then((tx) =>
Blockchain.submitTxWithReSign(tx, attester, { resolveOn: IS_IN_BLOCK })
)
await ctype.store(attester).then((tx) =>
BlockchainUtils.submitTxWithReSign(tx, attester, {
resolveOn: BlockchainUtils.IS_IN_BLOCK,
})
)
} catch (e) {
console.log(
'Error while storing CType. Probably either insufficient funds or ctype does already exist.',
Expand Down
31 changes: 16 additions & 15 deletions src/__integrationtests__/Attestation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import { IAttestedClaim, IClaim } from '..'
import Attestation from '../attestation/Attestation'
import { revoke } from '../attestation/Attestation.chain'
import AttestedClaim from '../attestedclaim/AttestedClaim'
import Blockchain, {
import { IBlockchainApi } from '../blockchain/Blockchain'
import {
IS_IN_BLOCK,
IS_READY,
IBlockchainApi,
} from '../blockchain/Blockchain'
submitTxWithReSign,
} from '../blockchain/Blockchain.utils'
import getCached, { DEFAULT_WS_ADDRESS } from '../blockchainApiConnection'
import Claim from '../claim/Claim'
import Credential from '../credential/Credential'
Expand Down Expand Up @@ -47,7 +48,7 @@ describe('handling attestations that do not exist', () => {
it('Attestation.revoke', async () => {
return expect(
Attestation.revoke('0x012012012', alice).then((tx) =>
Blockchain.submitTxWithReSign(tx, alice, { resolveOn: IS_IN_BLOCK })
submitTxWithReSign(tx, alice, { resolveOn: IS_IN_BLOCK })
)
).rejects.toThrow()
}, 30_000)
Expand All @@ -68,7 +69,7 @@ describe('When there is an attester, claimer and ctype drivers license', () => {
// console.log(`verify stored: ${await DriversLicense.verifyStored()}`)
if (!ctypeExists) {
await DriversLicense.store(attester).then((tx) =>
Blockchain.submitTxWithReSign(tx, attester, { resolveOn: IS_READY })
submitTxWithReSign(tx, attester, { resolveOn: IS_READY })
)
}
}, 60_000)
Expand Down Expand Up @@ -107,7 +108,7 @@ describe('When there is an attester, claimer and ctype drivers license', () => {
await attestation
.store(attester)
.then((tx) =>
Blockchain.submitTxWithReSign(tx, attester, { resolveOn: IS_IN_BLOCK })
submitTxWithReSign(tx, attester, { resolveOn: IS_IN_BLOCK })
)
const cred = await Credential.fromRequestAndAttestation(
claimer,
Expand Down Expand Up @@ -143,7 +144,7 @@ describe('When there is an attester, claimer and ctype drivers license', () => {

await expect(
attestation.store(bobbyBroke).then((tx) =>
Blockchain.submitTxWithReSign(tx, bobbyBroke, {
submitTxWithReSign(tx, bobbyBroke, {
resolveOn: IS_IN_BLOCK,
})
)
Expand Down Expand Up @@ -189,7 +190,7 @@ describe('When there is an attester, claimer and ctype drivers license', () => {
)
await expect(
attestation.store(attester).then((tx) =>
Blockchain.submitTxWithReSign(tx, attester, {
submitTxWithReSign(tx, attester, {
resolveOn: IS_IN_BLOCK,
})
)
Expand All @@ -214,7 +215,7 @@ describe('When there is an attester, claimer and ctype drivers license', () => {
attester.getPublicIdentity()
)
await attestation.store(attester).then((tx) =>
Blockchain.submitTxWithReSign(tx, attester, {
submitTxWithReSign(tx, attester, {
resolveOn: IS_IN_BLOCK,
})
)
Expand All @@ -230,7 +231,7 @@ describe('When there is an attester, claimer and ctype drivers license', () => {
it('should not be possible to attest the same claim twice', async () => {
await expect(
attClaim.attestation.store(attester).then((tx) =>
Blockchain.submitTxWithReSign(tx, attester, {
submitTxWithReSign(tx, attester, {
resolveOn: IS_IN_BLOCK,
})
)
Expand Down Expand Up @@ -258,7 +259,7 @@ describe('When there is an attester, claimer and ctype drivers license', () => {
it('should not be possible for the claimer to revoke an attestation', async () => {
await expect(
revoke(attClaim.getHash(), claimer).then((tx) =>
Blockchain.submitTxWithReSign(tx, claimer, { resolveOn: IS_IN_BLOCK })
submitTxWithReSign(tx, claimer, { resolveOn: IS_IN_BLOCK })
)
).rejects.toThrowError('not permitted')
await expect(attClaim.verify()).resolves.toBeTruthy()
Expand All @@ -267,7 +268,7 @@ describe('When there is an attester, claimer and ctype drivers license', () => {
it('should be possible for the attester to revoke an attestation', async () => {
await expect(attClaim.verify()).resolves.toBeTruthy()
await revoke(attClaim.getHash(), attester).then((tx) =>
Blockchain.submitTxWithReSign(tx, attester, { resolveOn: IS_IN_BLOCK })
submitTxWithReSign(tx, attester, { resolveOn: IS_IN_BLOCK })
)
await expect(attClaim.verify()).resolves.toBeFalsy()
}, 40_000)
Expand All @@ -277,7 +278,7 @@ describe('When there is an attester, claimer and ctype drivers license', () => {
beforeAll(async () => {
if (!(await CtypeOnChain(IsOfficialLicenseAuthority))) {
await IsOfficialLicenseAuthority.store(faucet).then((tx) =>
Blockchain.submitTxWithReSign(tx, faucet, { resolveOn: IS_IN_BLOCK })
submitTxWithReSign(tx, faucet, { resolveOn: IS_IN_BLOCK })
)
}
await expect(
Expand Down Expand Up @@ -308,7 +309,7 @@ describe('When there is an attester, claimer and ctype drivers license', () => {
await licenseAuthorizationGranted
.store(faucet)
.then((tx) =>
Blockchain.submitTxWithReSign(tx, faucet, { resolveOn: IS_IN_BLOCK })
submitTxWithReSign(tx, faucet, { resolveOn: IS_IN_BLOCK })
)
// make request including legitimation
const iBelieveICanDrive = Claim.fromCTypeAndClaimContents(
Expand Down Expand Up @@ -336,7 +337,7 @@ describe('When there is an attester, claimer and ctype drivers license', () => {
attester.getPublicIdentity()
)
await LicenseGranted.store(attester).then((tx) =>
Blockchain.submitTxWithReSign(tx, attester, { resolveOn: IS_IN_BLOCK })
submitTxWithReSign(tx, attester, { resolveOn: IS_IN_BLOCK })
)
const license = await Credential.fromRequestAndAttestation(
claimer,
Expand Down
5 changes: 2 additions & 3 deletions src/__integrationtests__/AttestationPE.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import {
import { Claim, IBlockchainApi, IClaim, Message } from '..'
import { Attester, Claimer, Verifier } from '../actor'
import { ClaimerAttestationSession } from '../actor/Claimer'
import Blockchain from '../blockchain'
import { IS_IN_BLOCK } from '../blockchain/Blockchain'
import { IS_IN_BLOCK, submitTxWithReSign } from '../blockchain/Blockchain.utils'
import getCached, { DEFAULT_WS_ADDRESS } from '../blockchainApiConnection'
import Credential from '../credential'
import Identity, { AttesterIdentity } from '../identity'
Expand Down Expand Up @@ -54,7 +53,7 @@ describe('Privacy enhanced claim, attestation, verification process', () => {
// set up claim (ctype missing on fresh chain)
if (!(await CtypeOnChain(DriversLicense))) {
await DriversLicense.store(attester).then((tx) =>
Blockchain.submitTxWithReSign(tx, attester, { resolveOn: IS_IN_BLOCK })
submitTxWithReSign(tx, attester, { resolveOn: IS_IN_BLOCK })
)
}
claim = Claim.fromCTypeAndClaimContents(
Expand Down
23 changes: 12 additions & 11 deletions src/__integrationtests__/Balance.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ import {
listenToBalanceChanges,
makeTransfer,
} from '../balance/Balance.chain'
import Blockchain, {
import { IBlockchainApi } from '../blockchain/Blockchain'
import {
IS_IN_BLOCK,
IS_READY,
IBlockchainApi,
} from '../blockchain/Blockchain'
submitTxWithReSign,
} from '../blockchain/Blockchain.utils'
import getCached, { DEFAULT_WS_ADDRESS } from '../blockchainApiConnection'
import Identity from '../identity/Identity'
import {
Expand Down Expand Up @@ -70,7 +71,7 @@ describe('when there is a dev chain with a faucet', () => {
listenToBalanceChanges(ident.address, funny)
const balanceBefore = await getBalance(faucet.address)
await makeTransfer(faucet, ident.address, MIN_TRANSACTION).then((tx) =>
Blockchain.submitTxWithReSign(tx, faucet, { resolveOn: IS_IN_BLOCK })
submitTxWithReSign(tx, faucet, { resolveOn: IS_IN_BLOCK })
)
const [balanceAfter, balanceIdent] = await Promise.all([
getBalance(faucet.address),
Expand Down Expand Up @@ -101,7 +102,7 @@ describe('When there are haves and have-nots', () => {
stormyD.address,
MIN_TRANSACTION
).then((tx) =>
Blockchain.submitTxWithReSign(tx, richieRich, { resolveOn: IS_IN_BLOCK })
submitTxWithReSign(tx, richieRich, { resolveOn: IS_IN_BLOCK })
)
const balanceTo = await getBalance(stormyD.address)
expect(balanceTo.toNumber()).toBe(MIN_TRANSACTION.toNumber())
Expand All @@ -111,7 +112,7 @@ describe('When there are haves and have-nots', () => {
const originalBalance = await getBalance(stormyD.address)
await expect(
makeTransfer(bobbyBroke, stormyD.address, MIN_TRANSACTION).then((tx) =>
Blockchain.submitTxWithReSign(tx, bobbyBroke, {
submitTxWithReSign(tx, bobbyBroke, {
resolveOn: IS_IN_BLOCK,
})
)
Expand All @@ -128,7 +129,7 @@ describe('When there are haves and have-nots', () => {
const RichieBalance = await getBalance(richieRich.address)
await expect(
makeTransfer(richieRich, bobbyBroke.address, RichieBalance).then((tx) =>
Blockchain.submitTxWithReSign(tx, richieRich, {
submitTxWithReSign(tx, richieRich, {
resolveOn: IS_IN_BLOCK,
})
)
Expand All @@ -145,10 +146,10 @@ describe('When there are haves and have-nots', () => {
const listener = jest.fn()
listenToBalanceChanges(faucet.address, listener)
await makeTransfer(faucet, richieRich.address, MIN_TRANSACTION).then((tx) =>
Blockchain.submitTxWithReSign(tx, faucet, { resolveOn: IS_READY })
submitTxWithReSign(tx, faucet, { resolveOn: IS_READY })
)
await makeTransfer(faucet, stormyD.address, MIN_TRANSACTION).then((tx) =>
Blockchain.submitTxWithReSign(tx, faucet, { resolveOn: IS_IN_BLOCK })
submitTxWithReSign(tx, faucet, { resolveOn: IS_IN_BLOCK })
)

expect(listener).toBeCalledWith(
Expand All @@ -164,10 +165,10 @@ describe('When there are haves and have-nots', () => {
listenToBalanceChanges(faucet.address, listener)
await Promise.all([
makeTransfer(faucet, richieRich.address, MIN_TRANSACTION).then((tx) =>
Blockchain.submitTxWithReSign(tx, faucet, { resolveOn: IS_IN_BLOCK })
submitTxWithReSign(tx, faucet, { resolveOn: IS_IN_BLOCK })
),
makeTransfer(faucet, stormyD.address, MIN_TRANSACTION).then((tx) =>
Blockchain.submitTxWithReSign(tx, faucet, { resolveOn: IS_IN_BLOCK })
submitTxWithReSign(tx, faucet, { resolveOn: IS_IN_BLOCK })
),
])
expect(listener).toBeCalledWith(
Expand Down
14 changes: 6 additions & 8 deletions src/__integrationtests__/Ctypes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
*/

import { Identity } from '..'
import Blockchain, {
IS_IN_BLOCK,
IBlockchainApi,
} from '../blockchain/Blockchain'
import { IBlockchainApi } from '../blockchain/Blockchain'
import { IS_IN_BLOCK, submitTxWithReSign } from '../blockchain/Blockchain.utils'
import getCached, { DEFAULT_WS_ADDRESS } from '../blockchainApiConnection'
import CType from '../ctype/CType'
import { getOwner } from '../ctype/CType.chain'
Expand Down Expand Up @@ -49,7 +47,7 @@ describe('When there is an CtypeCreator and a verifier', () => {
)
await expect(
ctype.store(bobbyBroke).then((tx) =>
Blockchain.submitTxWithReSign(tx, bobbyBroke, {
submitTxWithReSign(tx, bobbyBroke, {
resolveOn: IS_IN_BLOCK,
})
)
Expand All @@ -60,7 +58,7 @@ describe('When there is an CtypeCreator and a verifier', () => {
it('should be possible to create a claim type', async () => {
const ctype = makeCType()
await ctype.store(ctypeCreator).then((tx) =>
Blockchain.submitTxWithReSign(tx, ctypeCreator, {
submitTxWithReSign(tx, ctypeCreator, {
resolveOn: IS_IN_BLOCK,
})
)
Expand All @@ -75,13 +73,13 @@ describe('When there is an CtypeCreator and a verifier', () => {
it('should not be possible to create a claim type that exists', async () => {
const ctype = makeCType()
await ctype.store(ctypeCreator).then((tx) =>
Blockchain.submitTxWithReSign(tx, ctypeCreator, {
submitTxWithReSign(tx, ctypeCreator, {
resolveOn: IS_IN_BLOCK,
})
)
await expect(
ctype.store(ctypeCreator).then((tx) =>
Blockchain.submitTxWithReSign(tx, ctypeCreator, {
submitTxWithReSign(tx, ctypeCreator, {
resolveOn: IS_IN_BLOCK,
})
)
Expand Down
25 changes: 11 additions & 14 deletions src/__integrationtests__/Delegation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
import { cryptoWaitReady } from '@polkadot/util-crypto'
import { Identity } from '..'
import Attestation from '../attestation/Attestation'
import Blockchain, {
import { IBlockchainApi } from '../blockchain/Blockchain'
import {
IS_IN_BLOCK,
IS_READY,
IBlockchainApi,
} from '../blockchain/Blockchain'
submitTxWithReSign,
} from '../blockchain/Blockchain.utils'
import getCached, { DEFAULT_WS_ADDRESS } from '../blockchainApiConnection'
import Claim from '../claim/Claim'
import Credential from '../credential/Credential'
Expand Down Expand Up @@ -52,7 +53,7 @@ describe('when there is an account hierarchy', () => {

if (!(await CtypeOnChain(DriversLicense))) {
await DriversLicense.store(attester).then((tx) =>
Blockchain.submitTxWithReSign(tx, attester, { resolveOn: IS_READY })
submitTxWithReSign(tx, attester, { resolveOn: IS_READY })
)
}
}, 30_000)
Expand All @@ -65,9 +66,7 @@ describe('when there is an account hierarchy', () => {
)
await rootNode
.store(uncleSam)
.then((tx) =>
Blockchain.submitTxWithReSign(tx, uncleSam, { resolveOn: IS_READY })
)
.then((tx) => submitTxWithReSign(tx, uncleSam, { resolveOn: IS_READY }))
const delegatedNode = new DelegationNode(
UUID.generate(),
rootNode.id,
Expand All @@ -79,7 +78,7 @@ describe('when there is an account hierarchy', () => {
await delegatedNode
.store(uncleSam, HashSignedByDelegate)
.then((tx) =>
Blockchain.submitTxWithReSign(tx, uncleSam, { resolveOn: IS_IN_BLOCK })
submitTxWithReSign(tx, uncleSam, { resolveOn: IS_IN_BLOCK })
)
await Promise.all([
expect(rootNode.verify()).resolves.toBeTruthy(),
Expand Down Expand Up @@ -108,11 +107,9 @@ describe('when there is an account hierarchy', () => {
HashSignedByDelegate = attester.signStr(delegatedNode.generateHash())
await rootNode
.store(uncleSam)
.then((tx) =>
Blockchain.submitTxWithReSign(tx, uncleSam, { resolveOn: IS_READY })
)
.then((tx) => submitTxWithReSign(tx, uncleSam, { resolveOn: IS_READY }))
await delegatedNode.store(uncleSam, HashSignedByDelegate).then((tx) =>
Blockchain.submitTxWithReSign(tx, uncleSam, {
submitTxWithReSign(tx, uncleSam, {
resolveOn: IS_IN_BLOCK,
})
)
Expand Down Expand Up @@ -145,7 +142,7 @@ describe('when there is an account hierarchy', () => {
attester.getPublicIdentity()
)
await attestation.store(attester).then((tx) =>
Blockchain.submitTxWithReSign(tx, attester, {
submitTxWithReSign(tx, attester, {
resolveOn: IS_IN_BLOCK,
})
)
Expand All @@ -160,7 +157,7 @@ describe('when there is an account hierarchy', () => {

// revoke attestation through root
await attClaim.attestation.revoke(uncleSam).then((tx) =>
Blockchain.submitTxWithReSign(tx, uncleSam, {
submitTxWithReSign(tx, uncleSam, {
resolveOn: IS_IN_BLOCK,
})
)
Expand Down
6 changes: 3 additions & 3 deletions src/__integrationtests__/ErrorHandler.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import BN from 'bn.js'
import { Attestation, IBlockchainApi } from '..'
import { makeTransfer } from '../balance/Balance.chain'
import Blockchain, { IS_IN_BLOCK } from '../blockchain/Blockchain'
import { IS_IN_BLOCK, submitTxWithReSign } from '../blockchain/Blockchain.utils'
import { DEFAULT_WS_ADDRESS, getCached } from '../blockchainApiConnection'
import { ERROR_CTYPE_NOT_FOUND, ERROR_UNKNOWN } from '../errorhandling'
import Identity from '../identity'
Expand All @@ -24,7 +24,7 @@ it('records an unknown extrinsic error when transferring less than the existenti
const to = await Identity.buildFromMnemonic('')
await expect(
makeTransfer(alice, to.address, new BN(1)).then((tx) =>
Blockchain.submitTxWithReSign(tx, alice, { resolveOn: IS_IN_BLOCK })
submitTxWithReSign(tx, alice, { resolveOn: IS_IN_BLOCK })
)
).rejects.toThrow(ERROR_UNKNOWN)
}, 30_000)
Expand All @@ -41,7 +41,7 @@ it('records an extrinsic error when ctype does not exist', async () => {
})
const tx = await attestation.store(alice)
await expect(
Blockchain.submitTxWithReSign(tx, alice, { resolveOn: IS_IN_BLOCK })
submitTxWithReSign(tx, alice, { resolveOn: IS_IN_BLOCK })
).rejects.toThrow(ERROR_CTYPE_NOT_FOUND)
}, 30_000)

Expand Down
Loading

0 comments on commit e48f1e2

Please sign in to comment.