blockchain integration

This commit is contained in:
kandrusyak
2022-08-17 14:05:49 +03:00
parent 027bf0d405
commit f257066d5a
6 changed files with 370 additions and 50 deletions

127
src/utils/Schemas.js Normal file
View File

@@ -0,0 +1,127 @@
export const IdentityModuleSchema = {
$id: 'idntty/identity/module',
title: 'Identity module account schema',
type: 'object',
properties: {
features: {
fieldNumber: 1,
type: 'array',
maxItems: 256,
items: {
type: 'object',
required: ['label', 'value'],
properties: {
label: { fieldNumber: 1, dataType: 'string'},
value: { fieldNumber: 2, dataType: 'bytes'},
}
}
},
verifications: {
fieldNumber: 2,
type: 'array',
items: {
type: 'object',
required: ['label', 'account', 'tx'],
properties: {
label: { fieldNumber: 1, dataType: 'string'},
account: { fieldNumber: 2, dataType: 'bytes'},
tx: { fieldNumber: 3, dataType: 'bytes'},
}
}
},
},
default: {features: [], verifications: []}
};
export const setFeatureAssetSchema = {
$id: 'idntty/identity/setfeature',
title: 'Asset schema to set or update account features for identity module',
type: 'object',
required: ['features'],
properties: {
features: {
fieldNumber: 1,
type: 'array',
minItems: 1,
maxItems: 16,
items: {
type: 'object',
required: ['label','value'],
properties: {
label: { fieldNumber: 1, dataType: 'string', maxLength: 16},
value: { fieldNumber: 2, dataType: 'bytes', maxLength: 32},
}
}
}
}
};
export const removeFeatureAssetSchema = {
$id: 'idntty/identity/removefeature',
title: 'Asset schema to remove account features for identity module',
type: 'object',
required: ['features'],
properties: {
features: {
fieldNumber: 1,
type: 'array',
minItems: 1,
maxItems: 16,
items: {
type: 'object',
required: ['label'],
properties: {
label: { fieldNumber: 1, dataType: 'string', maxLength: 16},
}
}
}
}
};
export const validateFeatureAssetSchema = {
$id: 'idntty/identity/validatefeature',
title: 'Asset schema to validate account features for identity module',
type: 'object',
required: ['recipientAddress','features'],
properties: {
recipientAddress: {fieldNumber: 1, dataType: 'bytes', minLength: 20, maxLength: 20},
features: {
fieldNumber: 2,
type: 'array',
minItems: 1,
maxItems: 16,
items: {
type: 'object',
required: ['label', 'value'],
properties: {
label: { fieldNumber: 1, dataType: 'string', maxLength: 16},
value: { fieldNumber: 2, dataType: 'bytes', maxLength: 32},
}
}
}
}
};
export const invalidateFeatureAssetSchema = {
$id: 'idntty/identity/invalidatefeature',
title: 'Asset schema to invalidate account features for identity module',
type: 'object',
required: ['recipientAddress','features'],
properties: {
recipientAddress: {fieldNumber: 1, dataType: 'bytes', minLength: 20, maxLength: 20},
features: {
fieldNumber: 2,
type: 'array',
minItems: 1,
maxItems: 16,
items: {
type: 'object',
required: ['label'],
properties: {
label: { fieldNumber: 1, dataType: 'string', maxLength: 16},
}
}
}
}
};

View File

@@ -1,4 +1,6 @@
import resolveConfig from 'tailwindcss/resolveConfig';
import {cryptography, transactions} from '@liskhq/lisk-client';
import {removeFeatureAssetSchema, setFeatureAssetSchema} from './Schemas';
export const tailwindConfig = () => {
// Tailwind config
@@ -32,3 +34,126 @@ export const formatThousands = (value) => Intl.NumberFormat('en-US', {
maximumSignificantDigits: 3,
notation: 'compact',
}).format(value);
export const encryptAccountData = (data = [], passPhrase = '', pubKey = '') => {
return data.map((item) => {
let value = cryptography.encryptMessageWithPassphrase(item.seed + ":" + item.value, passPhrase, pubKey);
return {
label: item.key,
value: value.encryptedMessage,
value_nonce: value.nonce,
seed: item.seed,
}
})
}
export const hashAccountData = (data = [], oldData = [], hashMap = {}) => {
const accountMap = data.reduce((acc, item) => ({
...acc,
[item.label]: item.value
}), {})
const oldAccountMap = oldData.reduce((acc, item) => ({
...acc,
[item.label]: item.value
}), {})
const removed = oldData.reduce((acc, item) => {
if(!accountMap[item.label])
return [...acc, {label: item.label}];
return acc
}, [])
const changed = data.reduce((acc, item) => {
if(!oldAccountMap[item.label])
return [ ...acc, item ]
const oldValue = hashMap[item.key];
const newValue = hashValue(item.value, item.seed).toString('hex')
if(oldValue !== newValue)
return [ ...acc, item ]
return acc;
}, [])
return [
removed,
changed.map((item) => {
const value = hashValue(item.value, item.seed);
return {
label: item.key,
value
}
})
]
}
export const jsonReplacer = (key, value) => {
if (key === 'big') {
return value.toString();
}
return value;
}
export const hashValue = (value = '', seed = '') => cryptography.hash(Buffer.concat([Buffer.from(seed, 'utf8'), cryptography.hash(value, 'utf8')]));
export const generateTransaction = (nonce = BigInt(0), senderPublicKey = '', networkIdentifier = '', passPhrase = '', fee = BigInt(500000),) => {
return {
update: (features) => generateSetTransaction(features, nonce, senderPublicKey, networkIdentifier, passPhrase, fee),
remove: (features) => generateRemoveTransaction(features, nonce, senderPublicKey, networkIdentifier, passPhrase, fee),
}
}
export const generateSetTransaction = (features, nonce = BigInt(0), senderPublicKey = '', networkIdentifier = '', passPhrase = '', fee = BigInt(500000)) => {
const tx = {
"moduleID": 1001,
"assetID": 1,
nonce,
senderPublicKey,
fee,
"asset": {
features
},
}
if(features.length === 0)
return;
const signedTx = transactions.signTransaction(setFeatureAssetSchema,
tx, Buffer.from(networkIdentifier, "hex"),
passPhrase)
signedTx.senderPublicKey = signedTx.senderPublicKey.toString("hex");
signedTx.signatures[0] = signedTx.signatures[0].toString("hex");
signedTx.asset.features = signedTx.asset.features.map(feature => ({
...feature,
value: feature.value.toString("hex")
}))
delete signedTx.id;
return signedTx;
}
export const generateRemoveTransaction = (features, nonce = BigInt(0), senderPublicKey = '', networkIdentifier = '', passPhrase = '', fee = BigInt(500000)) => {
const tx = {
"moduleID": 1001,
"assetID": 2,
nonce,
senderPublicKey,
fee,
"asset": {
features
},
}
const signedTx = transactions.signTransaction(removeFeatureAssetSchema,
tx, Buffer.from(networkIdentifier, "hex"),
passPhrase)
signedTx.senderPublicKey = signedTx.senderPublicKey.toString(
"hex");
signedTx.signatures[0] = signedTx.signatures[0].toString("hex");
delete signedTx.id;
return signedTx;
}