Skip to content

Commit

Permalink
Merge pull request #189 from alastria/feature/178-UpdatePublicKeyRegi…
Browse files Browse the repository at this point in the history
…stry

Resolve issue #178. Duplicate functions to use publicKeys as a hash.
  • Loading branch information
rober12 authored May 16, 2024
2 parents 29ec49d + 31254ab commit 1654e33
Show file tree
Hide file tree
Showing 11 changed files with 475 additions and 256 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v1
with:
node-version: '14.x'
node-version: '18.x'
- name: Install dependencies
run: npm install
env:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@ build/Release
# Dependency directories
node_modules/
alastriaID-truffle-contracts
alastriaID-hardhat-contracts
jspm_packages/
package-lock.json
src/config.ts

# Typescript v1 declaration files
typings/
Expand Down
462 changes: 244 additions & 218 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"types": "dist/index.d.ts",
"scripts": {
"postinstall": "run-s build",
"compile": "git clone --branch develop https://github.com/alastria/alastriaID-truffle-contracts.git && npm install --prefix alastriaID-truffle-contracts && npm run compile --prefix alastriaID-truffle-contracts && node src/configFile.js && tsc",
"clean": "rimraf dist alastriaID-truffle-contracts",
"compile": "git clone --branch v2.2 https://github.com/alastria/alastriaID-hardhat-contracts.git && npm install --prefix alastriaID-hardhat-contracts && npm run compile --prefix alastriaID-hardhat-contracts && node src/configFile.js && tsc",
"clean": "rimraf dist alastriaID-hardhat-contracts",
"build": "run-s clean compile",
"test": "mocha -r ts-node/register test/**/*.ts",
"lint": "eslint 'src/**/*.{js,ts,tsx}'",
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts

Large diffs are not rendered by default.

58 changes: 43 additions & 15 deletions src/configFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,56 @@
const fs = require('fs')
const path = require('path')

const contractsABIPath = '../alastriaID-truffle-contracts/build/contracts'
const contractsABIPath = '../alastriaID-hardhat-contracts/artifacts/contracts'
const _contractsAbi = {}
const abi = {}

// Process the abi dir for getting an object with all the abi functions
fs.readdirSync(path.join(__dirname, `${contractsABIPath}`)).forEach((file) => {
const abi = {}
const abiFile = JSON.parse(
fs.readFileSync(path.join(__dirname, `${contractsABIPath}`, file), 'utf8')
)
abiFile.abi.forEach((element) => {
if (element.type === 'constructor') {
abi.constructor = element
} else {
if (element.name) {
abi[element.name] = element
fs.readdirSync(path.join(__dirname, `${contractsABIPath}`)).forEach((folder) => {
fs.readdirSync(path.join(__dirname, `${contractsABIPath}/${folder}`)).forEach((folderSol) => {
fs.readdirSync(path.join(__dirname, `${contractsABIPath}/${folder}/${folderSol}`)).forEach((file) => {
if(folderSol === 'upgradeability') {
fs.readdirSync(path.join(__dirname, `${contractsABIPath}/${folder}/${folderSol}`)).forEach((folderUpgrade) => {
fs.readdirSync(path.join(__dirname, `${contractsABIPath}/${folder}/${folderSol}/${folderUpgrade}`)).forEach((fileUpgrade) => {
if (!fileUpgrade.endsWith('.dbg.json')) {
const abiFile = JSON.parse(
fs.readFileSync(path.join(__dirname, `${contractsABIPath}/${folder}/${folderSol}/${folderUpgrade}`, fileUpgrade), 'utf8')
)
abiFile.abi.forEach((element) => {
if (element.type === 'constructor') {
abi.constructor = element
} else {
if (element.name) {
abi[element.name] = element
}
}
})
_contractsAbi[abiFile.contractName] = abi
}
})
})
} else {
if (!file.endsWith('.dbg.json')) {
const abiFile = JSON.parse(
fs.readFileSync(path.join(__dirname, `${contractsABIPath}/${folder}/${folderSol}`, file), 'utf8')
)
abiFile.abi.forEach((element) => {
if (element.type === 'constructor') {
abi.constructor = element
} else {
if (element.name) {
abi[element.name] = element
}
}
})
_contractsAbi[abiFile.contractName] = abi
}
}
}
})
})
_contractsAbi[abiFile.contractName] = abi
})

const contractsAddressesPath = '../alastriaID-truffle-contracts'
const contractsAddressesPath = '../alastriaID-hardhat-contracts'
const contractsInfo = JSON.parse(
fs.readFileSync(path.join(__dirname, `${contractsAddressesPath}`, 'addresses.json'), 'utf8')
)
Expand Down
22 changes: 11 additions & 11 deletions src/txFactory/credentialRegistryTransactionFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,17 +84,17 @@ export function getSubjectCredentialStatus(
* @param web3 ethereum connection
* @param didSubject subject to recover credential list
*/
//export function getSubjectCredentialList(web3, didSubject) {
//const subjectAddr = AIdUtils.getProxyAddress(didSubject)
//const transaction = Object.assign({}, config.basicTransaction)
//transaction.data = web3.eth.abi.encodeFunctionCall(
//config.contractsAbi.AlastriaCredentialRegistry.getSubjectCredentialList,
//[subjectAddr]
//)
//transaction.to = config.alastriaCredentialRegistry
//transaction.gasLimit = 600000
//return transaction
//}
// export function getSubjectCredentialList(web3, didSubject) {
// const subjectAddr = AIdUtils.getProxyAddress(didSubject)
// const transaction = Object.assign({}, config.basicTransaction)
// transaction.data = web3.eth.abi.encodeFunctionCall(
// config.contractsAbi.AlastriaCredentialRegistry.getSubjectCredentialList,
// [subjectAddr]
// )
// transaction.to = config.alastriaCredentialRegistry
// transaction.gasLimit = 600000
// return transaction
// }

/**
* function updateCredentialStatus(web3, issuerCredentialHash, status)
Expand Down
22 changes: 21 additions & 1 deletion src/txFactory/identityManagerTransactionFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export function prepareAlastriaID(web3, signAddress) {
}

/**
* function createAlastriaIdentity(bytes publicKeyData) public validAddress(msg.sender) isOnTimeToLiveAndIsFromCaller(msg.sender)
* THIS METHOD WILL BE DEPREATED
* @param web3
* @param publicKey publicKey is a String
*/
Expand All @@ -57,6 +57,26 @@ export function createAlastriaIdentity(web3, publicKey) {
return transaction
}

/**
* @param web3
* @param publicKeyHash
*/
export function createAlastriaIdentityHash(web3, publicKeyHash) {
const transaction = Object.assign({}, config.basicTransaction)
transaction.gasLimit = 600000
const publicKeyCallData = web3.eth.abi.encodeFunctionCall(
config.contractsAbi.AlastriaPublicKeyRegistry.addPublicKey,
[publicKeyHash]
)
transaction.data = web3.eth.abi.encodeFunctionCall(
config.contractsAbi.AlastriaIdentityManager.createAlastriaIdentity,
[publicKeyCallData]
)
transaction.to = config.alastriaIdentityManager
return transaction
}

/**
* AlastriaIdentityIssuer.sol
* function addIdentityIssuer(address _identityIssuer, Eidas.EidasLevel _level) public alLeastLow(_level) notIdentityIssuer(_identityIssuer)
Expand Down
4 changes: 2 additions & 2 deletions src/txFactory/presentationRegistryTransactionFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function getSubjectPresentationStatus(
* @param web3
* @param didSubject
*/
/**export function getSubjectPresentationList(web3, didSubject) {
/** export function getSubjectPresentationList(web3, didSubject) {
const subjectAddr = AIdUtils.getProxyAddress(didSubject)
const transaction = Object.assign({}, config.basicTransaction)
transaction.data = web3.eth.abi.encodeFunctionCall(
Expand All @@ -75,7 +75,7 @@ export function getSubjectPresentationStatus(
transaction.to = config.alastriaPresentationRegistry
transaction.gasLimit = 600000
return transaction
}*/
} */

/**
* Receiver functions
Expand Down
133 changes: 131 additions & 2 deletions src/txFactory/publicKeyRegistryTransactionFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { PublicKeyStatus } from '../interfaces'
import { AddressUtils } from '../utils/AddressUtils'

/**
* THIS METHOD WILL BE DEPREATED
* function addKey(string memory publicKey, address subject) public
* @param web3
* @param publicKey
Expand All @@ -22,7 +23,23 @@ export function addKey(web3, publicKey) {
}

/**
* function revokePublicKey(string memory publicKey) public
* @param web3
* @param publicKeyHash
*/
export function addPublicKey(web3, publicKeyHash) {
const transaction = Object.assign({}, config.basicTransaction)
const delegatedData = web3.eth.abi.encodeFunctionCall(
config.contractsAbi.AlastriaPublicKeyRegistry.addPublicKey,
[publicKeyHash]
)
transaction.data = delegated(web3, delegatedData)
transaction.to = config.alastriaIdentityManager
transaction.gasLimit = 600000
return transaction
}

/**
* THIS METHOD WILL BE DEPREATED
* @param web3
* @param publicKey
*/
Expand All @@ -39,7 +56,23 @@ export function revokePublicKey(web3, publicKey) {
}

/**
* function deletePublicKey(string memory publicKey) public
* @param web3
* @param publicKeyHash
*/
export function revokePublicKeyHash(web3, publicKeyHash) {
const transaction = Object.assign({}, config.basicTransaction)
const delegatedData = web3.eth.abi.encodeFunctionCall(
config.contractsAbi.AlastriaPublicKeyRegistry.revokePublicKey,
[publicKeyHash]
)
transaction.data = delegated(web3, delegatedData)
transaction.to = config.alastriaIdentityManager
transaction.gasLimit = 600000
return transaction
}

/**
* THIS METHOD WILL BE DEPREATED
* @param web3
* @param publicKey
*/
Expand All @@ -55,6 +88,22 @@ export function deletePublicKey(web3, publicKey) {
return transaction
}

/**
* @param web3
* @param publicKeyHash
*/
export function deletePublicKeyHash(web3, publicKeyHash) {
const transaction = Object.assign({}, config.basicTransaction)
const delegatedData = web3.eth.abi.encodeFunctionCall(
config.contractsAbi.AlastriaPublicKeyRegistry.deletePublicKey,
[publicKeyHash]
)
transaction.data = delegated(web3, delegatedData)
transaction.to = config.alastriaIdentityManager
transaction.gasLimit = 600000
return transaction
}

/**
* function getCurrentPublicKey(address subject) view public validAddress(subject) returns (string)
* @param web3
Expand All @@ -73,6 +122,7 @@ export function getCurrentPublicKey(web3, did) {
}

/**
* THIS METHOD WILL BE DEPREATED
* @param web3
* @param did
* @param publicKey
Expand All @@ -90,6 +140,24 @@ export function getPublicKeyStatus(web3, did, publicKey) {
}

/**
* @param web3
* @param did
* @param publicKeyHash
*/
export function getPublicKeyStatusHash(web3, did, publicKeyHash) {
const subjectAddr = AIdUtils.getProxyAddress(did)
const transaction = Object.assign({}, config.basicTransaction)
transaction.data = web3.eth.abi.encodeFunctionCall(
config.contractsAbi.AlastriaPublicKeyRegistry.getPublicKeyStatus,
[subjectAddr, publicKeyHash]
)
transaction.to = config.alastriaPublicKeyRegistry
transaction.gasLimit = 600000
return transaction
}

/**
* THIS METHOD WILL BE DEPREATED
* @param web3
* @param did
* @param publicKey
Expand Down Expand Up @@ -119,6 +187,36 @@ export function getPublicKeyStatusDecodedAsJSON(
}

/**
* @param web3
* @param did
* @param publicKeyHash
*/
export function getPublicKeyStatusDecodedAsJSONHash(
web3,
did,
publicKeyHash
): Promise<PublicKeyStatus> {
const publicKeyStatusTx = getPublicKeyStatus(web3, did, publicKeyHash)

return new Promise((resolve) => {
web3.eth.call(publicKeyStatusTx).then((data) => {
const publicKeyStatusDecoded = web3.eth.abi.decodeParameters(
['bool', 'uint8', 'uint', 'uint'],
data
)
const publicKeyStatusDecodedAsJSON = {
exists: publicKeyStatusDecoded['0'],
status: publicKeyStatusDecoded['1'],
startDate: parseInt(publicKeyStatusDecoded['2']),
endDate: parseInt(publicKeyStatusDecoded['3'])
}
resolve(publicKeyStatusDecodedAsJSON)
})
})
}

/**
* THIS METHOD WILL BE DEPREATED
* @param web3
* @param did
* @param publicKey
Expand Down Expand Up @@ -149,6 +247,37 @@ export function isPublicKeyValidForDate(web3, did, publicKey, date) {
})
}

/**
* @param web3
* @param did
* @param publicKeyHash
* @param date in milliseconds
*/
export function isPublicKeyValidForDateHash(web3, did, publicKeyHash, date) {
publicKeyHash = AddressUtils.getAddressWithHexPrefix(publicKeyHash)
return new Promise((resolve, reject) => {
transactionFactory.publicKeyRegistry
.getPublicKeyStatusDecodedAsJSON(web3, did, publicKeyHash)
.then((publicKeyStatusAsJSON) => {
const existsPublicKey = publicKeyStatusAsJSON.exists

if (existsPublicKey) {
const isUserDateBetweenDates = _isUserDateBetweeenDates(
date,
publicKeyStatusAsJSON.startDate,
publicKeyStatusAsJSON.endDate
)
resolve(isUserDateBetweenDates)
} else {
reject(new Error('Public key does not exist'))
}
})
.catch(() => {
reject(new Error('Unresolved error'))
})
})
}

/**
* @param userDate in milliseconds
* @param publicKeyStartDate in milliseconds
Expand Down
Loading

0 comments on commit 1654e33

Please sign in to comment.