opportunity-service 0.0.536 → 0.0.542
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/OpportunityService.js +16 -20
- package/dist/OpportunityService.js.map +1 -1
- package/dist/api/dispute/check-vote-results.js +33 -0
- package/dist/api/dispute/check-vote-results.js.map +1 -0
- package/dist/api/dispute/connect.js +39 -0
- package/dist/api/dispute/connect.js.map +1 -0
- package/dist/api/dispute/ensure-entity-metadata.js +39 -0
- package/dist/api/dispute/ensure-entity-metadata.js.map +1 -0
- package/dist/api/dispute/launch-new-vote.js +94 -0
- package/dist/api/dispute/launch-new-vote.js.map +1 -0
- package/dist/api/dispute/submit-vote.js +41 -0
- package/dist/api/dispute/submit-vote.js.map +1 -0
- package/dist/api/dispute/util/wait-until-present.js +27 -0
- package/dist/api/dispute/util/wait-until-present.js.map +1 -0
- package/dist/api/{voting/connect.js → dispute/util/wait-until-started.js} +11 -21
- package/dist/api/dispute/util/wait-until-started.js.map +1 -0
- package/dist/api/exchange/create-dispute.js +1 -2
- package/dist/api/exchange/create-dispute.js.map +1 -1
- package/dist/api/exchange/create-task.js +1 -1
- package/dist/api/exchange/create-task.js.map +1 -1
- package/dist/api/identity/register-new-user.js +1 -1
- package/dist/api/identity/register-new-user.js.map +1 -1
- package/dist/api/index.js +31 -12
- package/dist/api/index.js.map +1 -1
- package/dist/api/internal/abis.js +2 -1
- package/dist/api/internal/abis.js.map +1 -1
- package/dist/api/internal/addresses.js +21 -4
- package/dist/api/internal/addresses.js.map +1 -1
- package/dist/api/other/create-eth-crypto-creds.js +20 -0
- package/dist/api/other/create-eth-crypto-creds.js.map +1 -0
- package/dist/api/provider/decrypt.js +18 -0
- package/dist/api/provider/decrypt.js.map +1 -0
- package/dist/api/provider/encrypt.js +18 -0
- package/dist/api/provider/encrypt.js.map +1 -0
- package/dist/api/util/encrypt-by-public-key.js +52 -0
- package/dist/api/util/encrypt-by-public-key.js.map +1 -0
- package/dist/api/util/parse-cipher.js +11 -0
- package/dist/api/util/parse-cipher.js.map +1 -0
- package/dist/api/util/stringify-cipher.js +5 -0
- package/dist/api/util/stringify-cipher.js.map +1 -0
- package/dist/blockchain/abi.json +779 -1
- package/dist/blockchain/addresses.json +15 -5
- package/dist/blockchain/bytecode.json +2 -2
- package/dist/constants.js +0 -1
- package/dist/constants.js.map +1 -1
- package/dist/modules/storage/OpportunityStorageProvider.js +71 -41
- package/dist/modules/storage/OpportunityStorageProvider.js.map +1 -1
- package/dist/sync/sync-jobs.js +17 -15
- package/dist/sync/sync-jobs.js.map +1 -1
- package/dist/sync/sync-markets.js +17 -15
- package/dist/sync/sync-markets.js.map +1 -1
- package/package.json +13 -2
- package/src/OpportunityService.ts +22 -24
- package/src/api/dispute/check-vote-results.ts +30 -0
- package/src/api/dispute/connect.ts +36 -0
- package/src/api/dispute/ensure-entity-metadata.ts +34 -0
- package/src/api/dispute/launch-new-vote.ts +116 -0
- package/src/api/dispute/submit-vote.ts +59 -0
- package/src/api/dispute/util/wait-until-present.ts +19 -0
- package/src/api/dispute/util/wait-until-started.ts +14 -0
- package/src/api/exchange/create-dispute.ts +1 -1
- package/src/api/exchange/create-task.ts +1 -1
- package/src/api/identity/register-new-user.ts +1 -1
- package/src/api/index.ts +31 -12
- package/src/api/internal/abis.ts +2 -1
- package/src/api/internal/addresses.ts +21 -4
- package/src/api/other/create-eth-crypto-creds.ts +14 -0
- package/src/api/provider/decrypt.ts +9 -0
- package/src/api/provider/encrypt.ts +9 -0
- package/src/api/util/encrypt-by-public-key.ts +47 -0
- package/src/api/util/parse-cipher.ts +11 -0
- package/src/api/util/stringify-cipher.ts +5 -0
- package/src/blockchain/abi.json +779 -1
- package/src/blockchain/addresses.json +17 -5
- package/src/blockchain/bytecode.json +2 -2
- package/src/constants.ts +0 -1
- package/src/modules/storage/OpportunityStorageProvider.ts +80 -23
- package/src/sync/sync-jobs.ts +2 -0
- package/src/sync/sync-markets.ts +3 -1
- package/src/types.ts +21 -1
- package/dist/api/voting/census.js +0 -38
- package/dist/api/voting/census.js.map +0 -1
- package/dist/api/voting/connect.js.map +0 -1
- package/dist/api/voting/entity.js +0 -39
- package/dist/api/voting/entity.js.map +0 -1
- package/dist/api/voting/process.js +0 -101
- package/dist/api/voting/process.js.map +0 -1
- package/src/api/voting/census.ts +0 -39
- package/src/api/voting/connect.ts +0 -34
- package/src/api/voting/entity.ts +0 -33
- package/src/api/voting/process.ts +0 -119
@@ -0,0 +1,116 @@
|
|
1
|
+
import { Wallet } from "@ethersproject/wallet"
|
2
|
+
import { CensusErc20Api } from "@vocdoni/census"
|
3
|
+
import { ProcessState, VotingApi, VotingOracleApi } from "@vocdoni/voting"
|
4
|
+
import { DVoteGateway, Erc20TokensApi, GatewayPool } from "@vocdoni/client"
|
5
|
+
import { ProcessMetadataTemplate } from "@vocdoni/data-models"
|
6
|
+
import { ProcessMode, ProcessContractParameters, ProcessEnvelopeType } from '@vocdoni/contract-wrappers'
|
7
|
+
import * as assert from 'assert'
|
8
|
+
import { waitUntilPresent } from "./util/wait-until-present"
|
9
|
+
|
10
|
+
export async function launchNewVote(
|
11
|
+
creatorWallet: Wallet,
|
12
|
+
tokenAddress: string,
|
13
|
+
gwPool: GatewayPool,
|
14
|
+
oracleClient: DVoteGateway,
|
15
|
+
disputeId: string,
|
16
|
+
employer: string,
|
17
|
+
worker: string
|
18
|
+
) {
|
19
|
+
console.log('Launching new vote...')
|
20
|
+
|
21
|
+
if (!await Erc20TokensApi.isRegistered(tokenAddress, gwPool)) {
|
22
|
+
await CensusErc20Api.registerTokenAuto(
|
23
|
+
tokenAddress,
|
24
|
+
creatorWallet,
|
25
|
+
gwPool
|
26
|
+
)
|
27
|
+
|
28
|
+
assert(await Erc20TokensApi.isRegistered(tokenAddress, gwPool))
|
29
|
+
}
|
30
|
+
|
31
|
+
const sourceBlockHeight = (await gwPool.provider.getBlockNumber()) - 1
|
32
|
+
const tokenInfo = await CensusErc20Api.getTokenInfo(tokenAddress, gwPool)
|
33
|
+
const proof = await CensusErc20Api.generateProof(tokenAddress, creatorWallet.address, tokenInfo.balanceMappingPosition, sourceBlockHeight, gwPool.provider)
|
34
|
+
|
35
|
+
if (!proof?.storageProof?.length) throw new Error("Invalid storage proof")
|
36
|
+
|
37
|
+
console.log("Preparing the new vote metadata")
|
38
|
+
const processMetadataPre = JSON.parse(JSON.stringify(ProcessMetadataTemplate)) // make a copy of the template
|
39
|
+
processMetadataPre.title.default = disputeId,
|
40
|
+
processMetadataPre.description.default = `Dispute: ${disputeId}`,
|
41
|
+
processMetadataPre.questions = [
|
42
|
+
{
|
43
|
+
title: { default: "Contract Dispute" },
|
44
|
+
description: { default: "Please choose one of the two participants" },
|
45
|
+
choices: [
|
46
|
+
{ title: { default: employer }, value: 0 },
|
47
|
+
{ title: { default: worker }, value: 1 },
|
48
|
+
]
|
49
|
+
}
|
50
|
+
]
|
51
|
+
processMetadataPre.meta.disputeId = disputeId;
|
52
|
+
|
53
|
+
const maxValue = processMetadataPre.questions.reduce((prev, cur) => {
|
54
|
+
const localMax = cur.choices.reduce((prev, cur) => prev > cur.value ? prev : cur.value, 0)
|
55
|
+
return localMax > prev ? localMax : prev
|
56
|
+
}, 0)
|
57
|
+
|
58
|
+
console.log("Getting the block height")
|
59
|
+
const currentBlock = await VotingApi.getBlockHeight(gwPool)
|
60
|
+
const startBlock = currentBlock + 15 //start after 15 blocks
|
61
|
+
const blockCount = 6 * 4 // 4m
|
62
|
+
|
63
|
+
const processParamsPre = {
|
64
|
+
mode: ProcessMode.make({ autoStart: true }),
|
65
|
+
envelopeType: ProcessEnvelopeType.make({}), // bit mask
|
66
|
+
metadata: processMetadataPre,
|
67
|
+
startBlock,
|
68
|
+
blockCount,
|
69
|
+
maxCount: 1,
|
70
|
+
maxValue,
|
71
|
+
maxTotalCost: 0,
|
72
|
+
costExponent: 10000, // 1.0000
|
73
|
+
maxVoteOverwrites: 1,
|
74
|
+
sourceBlockHeight,
|
75
|
+
tokenAddress,
|
76
|
+
paramsSignature: "0x0000000000000000000000000000000000000000000000000000000000000000"
|
77
|
+
}
|
78
|
+
|
79
|
+
const tokenDetails = {
|
80
|
+
balanceMappingPosition: tokenInfo.balanceMappingPosition,
|
81
|
+
storageHash: proof.storageHash,
|
82
|
+
storageProof: {
|
83
|
+
key: proof.storageProof[0].key,
|
84
|
+
value: proof.storageProof[0].value,
|
85
|
+
proof: proof.storageProof[0].proof
|
86
|
+
}
|
87
|
+
}
|
88
|
+
|
89
|
+
console.log("Creating the process")
|
90
|
+
const processId = await VotingOracleApi.newProcessErc20(
|
91
|
+
processParamsPre,
|
92
|
+
tokenDetails,
|
93
|
+
creatorWallet,
|
94
|
+
gwPool,
|
95
|
+
oracleClient
|
96
|
+
)
|
97
|
+
|
98
|
+
assert(processId)
|
99
|
+
console.log("Created process with process id: ", processId)
|
100
|
+
|
101
|
+
await waitUntilPresent(processId, gwPool)
|
102
|
+
|
103
|
+
console.log('Checking process params to ensure process was created...')
|
104
|
+
const processParams: ProcessState = await VotingApi.getProcessState(processId, gwPool)
|
105
|
+
|
106
|
+
assert.strictEqual(processParams?.entityAddress.toLowerCase(), creatorWallet.address.toLowerCase())
|
107
|
+
assert.strictEqual(processParams?.startBlock, processParamsPre?.startBlock, "SENT " + JSON.stringify(processParamsPre) + " GOT " + JSON.stringify(processParams))
|
108
|
+
assert.strictEqual(processParams?.blockCount, processParamsPre?.blockCount)
|
109
|
+
assert.strictEqual(processParams?.censusUri, processParamsPre?.censusUri)
|
110
|
+
|
111
|
+
const processMetadata = await VotingApi.getProcessMetadata(processId, gwPool)
|
112
|
+
console.log('Process created with metadata: ')
|
113
|
+
console.log(processMetadata)
|
114
|
+
|
115
|
+
return { processId, processParams, processMetadata }
|
116
|
+
}
|
@@ -0,0 +1,59 @@
|
|
1
|
+
import { Wallet } from "@ethersproject/wallet"
|
2
|
+
import { CensusErc20Api } from "@vocdoni/census"
|
3
|
+
import { ProcessState, VotingApi, VotingOracleApi } from "@vocdoni/voting"
|
4
|
+
import { DVoteGateway, Erc20TokensApi, GatewayPool } from "@vocdoni/client"
|
5
|
+
import { ProcessMetadata } from "@vocdoni/data-models"
|
6
|
+
import { Signer } from "@ethersproject/abstract-signer"
|
7
|
+
import { JsonRpcProvider } from "@ethersproject/providers"
|
8
|
+
import { assert } from "console"
|
9
|
+
|
10
|
+
export async function submitVote(
|
11
|
+
processId: string,
|
12
|
+
processParams: ProcessState,
|
13
|
+
processMetadata: ProcessMetadata,
|
14
|
+
accounts,
|
15
|
+
tokenAddress: string,
|
16
|
+
voter: Signer,
|
17
|
+
voterChoice: number[],
|
18
|
+
gwPool: GatewayPool) {
|
19
|
+
console.log("Launching votes")
|
20
|
+
|
21
|
+
const processKeys = processParams.envelopeType.hasEncryptedVotes ? await VotingApi.getProcessKeys(processId, gwPool) : null
|
22
|
+
const balanceMappingPosition = (await CensusErc20Api.getTokenInfo(tokenAddress, gwPool)).balanceMappingPosition
|
23
|
+
const voterAddress = await voter.getAddress()
|
24
|
+
|
25
|
+
const result = await CensusErc20Api.generateProof(
|
26
|
+
tokenAddress,
|
27
|
+
voterAddress,
|
28
|
+
balanceMappingPosition,
|
29
|
+
processParams.sourceBlockHeight,
|
30
|
+
gwPool.provider as JsonRpcProvider
|
31
|
+
)
|
32
|
+
|
33
|
+
const censusProof = result.storageProof[0]
|
34
|
+
|
35
|
+
console.log('Creating voter envelope')
|
36
|
+
const envelope = processParams.envelopeType.encryptedVotes ?
|
37
|
+
await VotingApi.packageSignedEnvelope({ censusOrigin: processParams.censusOrigin, votes: voterChoice, censusProof, processId, walletOrSigner: voter, processKeys }) :
|
38
|
+
await VotingApi.packageSignedEnvelope({ censusOrigin: processParams.censusOrigin, votes: voterChoice, censusProof, processId, walletOrSigner: voter })
|
39
|
+
|
40
|
+
console.log('Attempting to submit voter envelope: ' + voter)
|
41
|
+
await VotingApi.submitEnvelope(envelope, voter, gwPool)
|
42
|
+
|
43
|
+
// wait a bit
|
44
|
+
await new Promise(resolve => setTimeout(resolve, 11000))
|
45
|
+
|
46
|
+
const nullifier = VotingApi.getSignedVoteNullifier(voterAddress, processId)
|
47
|
+
const { block, date, registered } = await VotingApi.getEnvelopeStatus(processId, nullifier, gwPool)
|
48
|
+
|
49
|
+
if (!registered) {
|
50
|
+
console.log('Vote not submitted.. voter not registered...')
|
51
|
+
return false;
|
52
|
+
}
|
53
|
+
|
54
|
+
console.log('Voter envelope successfully submitted')
|
55
|
+
console.log('Block: ' + block)
|
56
|
+
console.log('Date: ' + date)
|
57
|
+
console.log('Registered: ' + registered)
|
58
|
+
return true
|
59
|
+
}
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import { VotingApi, VochainWaiter, ProcessState } from '@vocdoni/voting'
|
2
|
+
import * as assert from 'assert'
|
3
|
+
import { ProcessStatus } from 'dvote-solidity'
|
4
|
+
|
5
|
+
export async function waitUntilPresent(processId, gwPool) {
|
6
|
+
let attempts = 10
|
7
|
+
while (attempts >= 0) {
|
8
|
+
console.log("Waiting for process", processId, "to be created")
|
9
|
+
await VochainWaiter.wait(1, gwPool)
|
10
|
+
|
11
|
+
const state: ProcessState | void = await VotingApi.getProcessState(processId, gwPool).catch(error => console.log(error))
|
12
|
+
console.log('Attempted to get process state: ' + state)
|
13
|
+
if (state?.entityId) break
|
14
|
+
|
15
|
+
attempts--
|
16
|
+
}
|
17
|
+
|
18
|
+
if (attempts < 0) throw new Error("The process still does not exist on the Vochain after 10 blocks")
|
19
|
+
}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import { VotingApi, VochainWaiter } from '@vocdoni/voting'
|
2
|
+
import * as assert from 'assert'
|
3
|
+
import { ProcessStatus } from 'dvote-solidity'
|
4
|
+
|
5
|
+
export async function waitUntilStarted(processId, startBlock, gwPool) {
|
6
|
+
console.log('Waiting until block: ' + startBlock)
|
7
|
+
|
8
|
+
// start block
|
9
|
+
await VochainWaiter.waitUntil(startBlock, gwPool, { verbose: true })
|
10
|
+
|
11
|
+
console.log("Waiting for the process to be ready")
|
12
|
+
const state = await VotingApi.getProcessState(processId, gwPool)
|
13
|
+
assert.strictEqual(state.status, ProcessStatus.READY, "Should be ready but is not")
|
14
|
+
}
|
@@ -31,7 +31,7 @@ async function createDispute(data) : Promise<void> {
|
|
31
31
|
const disputeContractfactory = new ContractFactory(abi, bytecodeMap[Contracts.DISPUTE], opportunityService.getSignersInterface())
|
32
32
|
|
33
33
|
// Deploy an instance of the contract
|
34
|
-
const disputeContract = await disputeContractfactory.deploy(relationshipAddress,
|
34
|
+
const disputeContract = await disputeContractfactory.deploy(relationshipAddress, complaintMetadataPointer, complaintResponseMetadataPointer);
|
35
35
|
const txReceipt = await disputeContract.deployTransaction.wait()
|
36
36
|
console.log(txReceipt)
|
37
37
|
} catch(error) {
|
@@ -40,7 +40,7 @@ async function createTask(data) : Promise<void> {
|
|
40
40
|
console.log('d')
|
41
41
|
const contract = await new ethers.Contract(taskMarket, abi).connect(opportunityService.getSignersInterface());
|
42
42
|
console.log('e')
|
43
|
-
const txResponse = await contract.functions.createJob(taskOwner, ContractType.NORMAL, taskMetadataPointer, addressMap[
|
43
|
+
const txResponse = await contract.functions.createJob(taskOwner, ContractType.NORMAL, taskMetadataPointer, addressMap[opportunityService.getEthNetwork()][Contracts.DAI])
|
44
44
|
const txReceipt = await txResponse.wait()
|
45
45
|
console.log(txReceipt)
|
46
46
|
} catch(error) {
|
@@ -14,7 +14,7 @@ console.log('Registering new user: ' + msgSender)
|
|
14
14
|
|
15
15
|
|
16
16
|
const txResult = new Contract(
|
17
|
-
addressMap['UserRegistration'],
|
17
|
+
addressMap[opportunityService.getEthNetwork()]['UserRegistration'],
|
18
18
|
abiMap['UserRegistration']
|
19
19
|
).connect(opportunityService.getSignersInterface())
|
20
20
|
.registerNewUser({
|
package/src/api/index.ts
CHANGED
@@ -6,33 +6,46 @@ import { createTask } from "./exchange/create-task";
|
|
6
6
|
import { enterWorkRelationship } from "./exchange/enter-work-relationship";
|
7
7
|
import registerNewUser from "./identity/register-new-user";
|
8
8
|
import { createMarket } from "./market/create-market";
|
9
|
-
import { registerEntity } from "./voting/entity";
|
10
|
-
import { createVotingProcess } from "./voting/process";
|
11
9
|
|
12
10
|
import { abis, events, addresses } from './internal/index';
|
13
11
|
|
14
12
|
import { sendAsync } from './ethereum/sendAsync'
|
13
|
+
import { parseCypher } from "./util/parse-cipher";
|
14
|
+
import { stringifyCypher } from "./util/stringify-cipher";
|
15
|
+
import { decryptByPrivateKey, encryptByPublicKey } from "./util/encrypt-by-public-key";
|
16
|
+
import { createEthCryptoCreds } from "./other/create-eth-crypto-creds";
|
17
|
+
import { encrypt } from "./provider/encrypt";
|
18
|
+
import { decrypt } from "./provider/decrypt";
|
19
|
+
import { connectGateways, getOracleClient } from "./dispute/connect";
|
20
|
+
import { checkVoteResults } from "./dispute/check-vote-results";
|
21
|
+
import { ensureEntityMetadata } from "./dispute/ensure-entity-metadata";
|
22
|
+
import { launchNewVote } from "./dispute/launch-new-vote";
|
23
|
+
import { submitVote } from "./dispute/submit-vote";
|
15
24
|
|
16
25
|
function generateAPI() {
|
17
26
|
return {
|
27
|
+
crypto: {
|
28
|
+
encryptByPublicKey,
|
29
|
+
decryptByPrivateKey,
|
30
|
+
createEthCryptoCreds
|
31
|
+
},
|
18
32
|
internal: {
|
19
33
|
abis: abis,
|
20
34
|
addresses,
|
21
35
|
},
|
22
|
-
voting: {
|
23
|
-
entity: {
|
24
|
-
registerEntity
|
25
|
-
},
|
26
|
-
process: {
|
27
|
-
createVotingProcess
|
28
|
-
}
|
29
|
-
},
|
30
36
|
exchange: {
|
31
37
|
completeRelationship,
|
32
38
|
createTask,
|
33
39
|
enterWorkRelationship
|
34
40
|
},
|
35
|
-
dispute: {
|
41
|
+
dispute: {
|
42
|
+
connectGateways: connectGateways,
|
43
|
+
getOracleClient: getOracleClient,
|
44
|
+
checkVoteResults: checkVoteResults,
|
45
|
+
ensureEntityMetadata: ensureEntityMetadata,
|
46
|
+
launchNewVote: launchNewVote,
|
47
|
+
submitVote: submitVote
|
48
|
+
},
|
36
49
|
identity: {
|
37
50
|
registerNewUser
|
38
51
|
},
|
@@ -47,7 +60,13 @@ function generateAPI() {
|
|
47
60
|
}
|
48
61
|
},
|
49
62
|
provider: {
|
50
|
-
sendAsync
|
63
|
+
sendAsync,
|
64
|
+
encrypt,
|
65
|
+
decrypt
|
66
|
+
},
|
67
|
+
util: {
|
68
|
+
parseCypher,
|
69
|
+
stringifyCypher
|
51
70
|
}
|
52
71
|
}
|
53
72
|
}
|
package/src/api/internal/abis.ts
CHANGED
@@ -2,8 +2,25 @@ import opportunityService from '../../OpportunityService';
|
|
2
2
|
import * as addressMap from '../../blockchain/addresses.json';
|
3
3
|
|
4
4
|
export default {
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
5
|
+
"rinkeby": {
|
6
|
+
'MarketFactory': addressMap['rinkeby']['MarketFactory'],
|
7
|
+
'UserRegistration': addressMap['rinkeby']['UserRegistration'],
|
8
|
+
'UserSummaryFactory': addressMap['rinkeby']['UserSummaryFactory'],
|
9
|
+
'Dai': addressMap['rinkeby']['Dai'],
|
10
|
+
'Participation': addressMap['rinkeby']['Participation']
|
11
|
+
},
|
12
|
+
"mainnet": {
|
13
|
+
'MarketFactory': addressMap['mainnet']['MarketFactory'],
|
14
|
+
'UserRegistration': addressMap['mainnet']['UserRegistration'],
|
15
|
+
'UserSummaryFactory': addressMap['mainnet']['UserSummaryFactory'],
|
16
|
+
'Dai': addressMap['mainnet']['Dai'],
|
17
|
+
'Participation': addressMap['mainnet']['Participation']
|
18
|
+
},
|
19
|
+
"xdai": {
|
20
|
+
'MarketFactory': addressMap['xdai']['MarketFactory'],
|
21
|
+
'UserRegistration': addressMap['xdai']['UserRegistration'],
|
22
|
+
'UserSummaryFactory': addressMap['xdai']['UserSummaryFactory'],
|
23
|
+
'Dai': addressMap['xdai']['Dai'],
|
24
|
+
'Participation': addressMap['xdai']['Participation']
|
25
|
+
}
|
9
26
|
}
|
@@ -0,0 +1,14 @@
|
|
1
|
+
import EthCrypto from 'eth-crypto';
|
2
|
+
import jswallet from 'ethereumjs-wallet'
|
3
|
+
import opportunityService from '../..//OpportunityService';
|
4
|
+
import Web3 from 'web3'
|
5
|
+
|
6
|
+
async function createEthCryptoCreds(passphrase) {
|
7
|
+
const entropy = Buffer.from(passphrase, 'utf-8')
|
8
|
+
const identity = EthCrypto.createIdentity(entropy)
|
9
|
+
|
10
|
+
const { address, publicKey, privateKey } = identity
|
11
|
+
return identity
|
12
|
+
}
|
13
|
+
|
14
|
+
export { createEthCryptoCreds }
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import Web3 from 'web3'
|
2
|
+
import opportunityService from '../../OpportunityService'
|
3
|
+
|
4
|
+
export async function decrypt (keystore, passphrase) {
|
5
|
+
const web3 : Web3 = opportunityService.getDefaultProviderInterface()
|
6
|
+
|
7
|
+
const data = await web3.eth.accounts.decrypt(keystore, passphrase)
|
8
|
+
return data
|
9
|
+
}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
import Web3 from 'web3'
|
2
|
+
import opportunityService from '../../OpportunityService'
|
3
|
+
|
4
|
+
export async function encrypt(privKey, password) {
|
5
|
+
const web3 : Web3 = opportunityService.getDefaultProviderInterface()
|
6
|
+
|
7
|
+
const keystore = await web3.eth.accounts.encrypt(privKey, password);
|
8
|
+
return keystore
|
9
|
+
}
|
@@ -0,0 +1,47 @@
|
|
1
|
+
import EthCrypto from 'eth-crypto';
|
2
|
+
|
3
|
+
/**
|
4
|
+
*
|
5
|
+
* @param pubKey
|
6
|
+
* @param message
|
7
|
+
* @returns
|
8
|
+
*/
|
9
|
+
async function encryptByPublicKey(pubKey, message) {
|
10
|
+
const encrypted = await EthCrypto.encryptWithPublicKey(
|
11
|
+
pubKey, // publicKey
|
12
|
+
message// message
|
13
|
+
);
|
14
|
+
|
15
|
+
return encrypted
|
16
|
+
}
|
17
|
+
|
18
|
+
/*
|
19
|
+
{
|
20
|
+
iv: '02aeac54cb45283b427bd1a5028552c1',
|
21
|
+
ephemPublicKey: '044acf39ed83c304f19f41ea66615d7a6c0068d5fc48ee181f2fb1091...',
|
22
|
+
ciphertext: '5fbbcc1a44ee19f7499dbc39cfc4ce96',
|
23
|
+
mac: '96490b293763f49a371d3a2040a2d2cb57f246ee88958009fe3c7ef2a38264a1'
|
24
|
+
} // encrypted-data
|
25
|
+
*/
|
26
|
+
|
27
|
+
|
28
|
+
/**
|
29
|
+
*
|
30
|
+
* @param privKey
|
31
|
+
* @param encryptedData
|
32
|
+
*/
|
33
|
+
async function decryptByPrivateKey(privKey, encryptedData) {
|
34
|
+
const { iv, ephemPublicKey, ciphertext, mac } = encryptedData
|
35
|
+
const message = await EthCrypto.decryptWithPrivateKey(
|
36
|
+
privKey, // privateKey
|
37
|
+
{
|
38
|
+
iv,
|
39
|
+
ephemPublicKey,
|
40
|
+
ciphertext,
|
41
|
+
mac
|
42
|
+
} // encrypted-data
|
43
|
+
);
|
44
|
+
}
|
45
|
+
|
46
|
+
|
47
|
+
export { encryptByPublicKey, decryptByPrivateKey }
|
@@ -0,0 +1,11 @@
|
|
1
|
+
import EthCrypto from 'eth-crypto'
|
2
|
+
|
3
|
+
/* > {
|
4
|
+
iv: '02aeac54cb45283b427bd1a5028552c1',
|
5
|
+
ephemPublicKey: '044acf39ed83c304f19f41ea66615d7a6c0068d5fc48ee181f2fb1091...',
|
6
|
+
ciphertext: '5fbbcc1a44ee19f7499dbc39cfc4ce96',
|
7
|
+
mac: '96490b293763f49a371d3a2040a2d2cb57f246ee88958009fe3c7ef2a38264a1'
|
8
|
+
} */
|
9
|
+
export function parseCypher(str) {
|
10
|
+
return EthCrypto.cipher.parse(str);
|
11
|
+
}
|