add vpn and delegates

This commit is contained in:
kandrusyak
2022-11-27 02:45:33 +03:00
parent ea4d8a4049
commit a555d18417
22 changed files with 17530 additions and 15505 deletions

View File

@@ -1,127 +1,200 @@
export const IdentityModuleSchema = {
$id: 'idntty/identity/module',
title: 'Identity module account schema',
type: 'object',
$id: "idntty/identity/module",
title: "Identity module account schema",
type: "object",
properties: {
features: {
fieldNumber: 1,
type: 'array',
type: "array",
maxItems: 256,
items: {
type: 'object',
required: ['label', 'value'],
type: "object",
required: ["label", "value"],
properties: {
label: { fieldNumber: 1, dataType: 'string'},
value: { fieldNumber: 2, dataType: 'bytes'},
}
}
label: { fieldNumber: 1, dataType: "string" },
value: { fieldNumber: 2, dataType: "bytes" },
},
},
},
verifications: {
fieldNumber: 2,
type: 'array',
type: "array",
items: {
type: 'object',
required: ['label', 'account', 'tx'],
type: "object",
required: ["label", "account", "tx"],
properties: {
label: { fieldNumber: 1, dataType: 'string'},
account: { fieldNumber: 2, dataType: 'bytes'},
tx: { fieldNumber: 3, dataType: 'bytes'},
}
}
label: { fieldNumber: 1, dataType: "string" },
account: { fieldNumber: 2, dataType: "bytes" },
tx: { fieldNumber: 3, dataType: "bytes" },
},
},
},
},
default: {features: [], verifications: []}
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'],
$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',
type: "array",
minItems: 1,
maxItems: 16,
items: {
type: 'object',
required: ['label','value'],
type: "object",
required: ["label", "value"],
properties: {
label: { fieldNumber: 1, dataType: 'string', maxLength: 16},
value: { fieldNumber: 2, dataType: 'bytes', maxLength: 32},
}
}
}
}
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'],
$id: "idntty/identity/removefeature",
title: "Asset schema to remove account features for identity module",
type: "object",
required: ["features"],
properties: {
features: {
fieldNumber: 1,
type: 'array',
type: "array",
minItems: 1,
maxItems: 16,
items: {
type: 'object',
required: ['label'],
type: "object",
required: ["label"],
properties: {
label: { fieldNumber: 1, dataType: 'string', maxLength: 16},
}
}
}
}
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'],
$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},
recipientAddress: {
fieldNumber: 1,
dataType: "bytes",
minLength: 20,
maxLength: 20,
},
features: {
fieldNumber: 2,
type: 'array',
type: "array",
minItems: 1,
maxItems: 16,
items: {
type: 'object',
required: ['label', 'value'],
type: "object",
required: ["label", "value"],
properties: {
label: { fieldNumber: 1, dataType: 'string', maxLength: 16},
value: { fieldNumber: 2, dataType: 'bytes', maxLength: 32},
}
}
}
}
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'],
$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},
recipientAddress: {
fieldNumber: 1,
dataType: "bytes",
minLength: 20,
maxLength: 20,
},
features: {
fieldNumber: 2,
type: 'array',
type: "array",
minItems: 1,
maxItems: 16,
items: {
type: 'object',
required: ['label'],
type: "object",
required: ["label"],
properties: {
label: { fieldNumber: 1, dataType: 'string', maxLength: 16},
}
}
}
}
label: { fieldNumber: 1, dataType: "string", maxLength: 16 },
},
},
},
},
};
export const voteDelegateAssetSchema = {
$id: "lisk/dpos/vote",
type: "object",
required: ["votes"],
properties: {
votes: {
type: "array",
minItems: 1,
maxItems: 20,
items: {
type: "object",
required: ["delegateAddress", "amount"],
properties: {
delegateAddress: {
dataType: "bytes",
fieldNumber: 1,
minLength: 20,
maxLength: 20,
},
amount: {
dataType: "sint64",
fieldNumber: 2,
},
},
},
fieldNumber: 1,
},
},
};
export const unlockDelegateAssetSchema = {
$id: "lisk/dpos/unlock",
type: "object",
required: ["unlockObjects"],
properties: {
unlockObjects: {
type: "array",
minItems: 1,
maxItems: 20,
items: {
type: "object",
required: ["delegateAddress", "amount", "unvoteHeight"],
properties: {
delegateAddress: {
dataType: "bytes",
fieldNumber: 1,
minLength: 20,
maxLength: 20,
},
amount: {
dataType: "uint64",
fieldNumber: 2,
},
unvoteHeight: {
dataType: "uint32",
fieldNumber: 3,
},
},
},
fieldNumber: 1,
},
},
};

View File

@@ -1,16 +1,18 @@
import resolveConfig from 'tailwindcss/resolveConfig';
import {cryptography, transactions} from '@liskhq/lisk-client';
import resolveConfig from "tailwindcss/resolveConfig";
import { cryptography, transactions } from "@liskhq/lisk-client";
import {
removeFeatureAssetSchema,
setFeatureAssetSchema,
unlockDelegateAssetSchema,
validateFeatureAssetSchema,
} from './Schemas';
import {statusMap} from "../shared/statusMap";
voteDelegateAssetSchema,
} from "./Schemas";
import { statusMap } from "../shared/statusMap";
export const tailwindConfig = () => {
// Tailwind config
return resolveConfig('./src/tailwind.config.js')
}
return resolveConfig("./src/tailwind.config.js");
};
export const hexToRGB = (h) => {
let r = 0;
@@ -28,179 +30,378 @@ export const hexToRGB = (h) => {
return `${+r},${+g},${+b}`;
};
export const formatValue = (value) => Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
maximumSignificantDigits: 3,
notation: 'compact',
}).format(value);
export const formatValue = (value) =>
Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
maximumSignificantDigits: 3,
notation: "compact",
}).format(value);
export const formatThousands = (value) => Intl.NumberFormat('en-US', {
maximumSignificantDigits: 3,
notation: 'compact',
}).format(value);
export const formatThousands = (value) =>
Intl.NumberFormat("en-US", {
maximumSignificantDigits: 3,
notation: "compact",
}).format(value);
export const encryptAccountData = (data = [], passPhrase = '', pubKey = '') => {
return data.filter(item=>item.status!==statusMap.blockchained).map((item) => {
let value = cryptography.encryptMessageWithPassphrase(item.seed + ":" + item.value, passPhrase, pubKey);
export const formatAddressBig = (address) =>
`${address.slice(0, 4)} ${address.slice(4, 8)} ****** ${address.slice(
address.length - 8,
address.length - 4
)} ${address.slice(address.length - 4)}`;
export const encryptAccountData = (data = [], passPhrase = "", pubKey = "") => {
return data
.filter((item) => item.status !== statusMap.blockchained)
.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 encryptSharedData = (data = [], passPhrase = "", pubKey = "") => {
return data.map((item) => {
let value = cryptography.encryptMessageWithPassphrase(
item.seed + ":" + item.value,
passPhrase,
Buffer.from(pubKey, "hex")
);
return {
label: item.key,
value: value.encryptedMessage,
value_nonce: value.nonce,
seed: item.seed,
}
})
}
export const encryptSharedData = (data = [], passPhrase = '', pubKey = '') => {
return data.map((item)=> {
let value = cryptography.encryptMessageWithPassphrase(item.seed + ":" + item.value, passPhrase, Buffer.from(pubKey, 'hex'));
return {
label: item.key,
value: value.encryptedMessage,
value_nonce: value.nonce,
}
})
}
};
});
};
export const hashAccountData = (data = [], oldData = [], hashMap = {}) => {
const accountMap = data.reduce((acc, item) => ({
...acc,
[item.label]: item.value
}), {})
const accountMap = data.reduce(
(acc, item) => ({
...acc,
[item.label]: item.value,
}),
{}
);
const oldAccountMap = oldData.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] && !data.find(elem=>elem.label===item.label))
return [...acc, {label: item.label}];
return acc
}, [])
if (
!accountMap[item.label] &&
!data.find((elem) => elem.label === item.label)
)
return [...acc, { label: item.label }];
return acc;
}, []);
const changed = data.reduce((acc, item) => {
if(!oldAccountMap[item.label])
return [ ...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 && item.status==='new')
return [ ...acc, item ]
const newValue = hashValue(item.value, item.seed).toString("hex");
if (oldValue !== newValue && item.status === "new") return [...acc, item];
return acc;
}, [])
}, []);
return [
removed,
changed.map((item) => {
const value = hashValue(item.value, item.seed);
return {
label: item.key,
value
}
})
]
}
value,
};
}),
];
};
export const jsonReplacer = (key, value) => {
if (key === 'big') {
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 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),) => {
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),
validate: (features, recipientAddress) => generateValidateTransaction(features, nonce, senderPublicKey, networkIdentifier, passPhrase, fee, recipientAddress),
}
}
update: (features) =>
generateSetTransaction(
features,
nonce,
senderPublicKey,
networkIdentifier,
passPhrase,
fee
),
remove: (features) =>
generateRemoveTransaction(
features,
nonce,
senderPublicKey,
networkIdentifier,
passPhrase,
fee
),
validate: (features, recipientAddress) =>
generateValidateTransaction(
features,
nonce,
senderPublicKey,
networkIdentifier,
passPhrase,
fee,
recipientAddress
),
vote: (delegateAddress, amount) =>
generateVoteTransaction(
nonce,
senderPublicKey,
networkIdentifier,
passPhrase,
fee,
delegateAddress,
amount
),
unlock: (delegateAddress, amount, unvoteHeight) =>
generateUnlockTransaction(
nonce,
senderPublicKey,
networkIdentifier,
passPhrase,
fee,
delegateAddress,
amount,
unvoteHeight
),
};
};
export const generateSetTransaction = (features, nonce = BigInt(0), senderPublicKey = '', networkIdentifier = '', passPhrase = '', fee = BigInt(500000)) => {
export const generateSetTransaction = (
features,
nonce = BigInt(0),
senderPublicKey = "",
networkIdentifier = "",
passPhrase = "",
fee = BigInt(500000)
) => {
const tx = {
"moduleID": 1001,
"assetID": 1,
moduleID: 1001,
assetID: 1,
nonce,
senderPublicKey,
fee,
"asset": {
features
asset: {
features,
},
}
};
if(features.length === 0)
return;
if (features.length === 0) return;
const signedTx = transactions.signTransaction(setFeatureAssetSchema,
tx, Buffer.from(networkIdentifier, "hex"),
passPhrase)
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 => ({
signedTx.asset.features = signedTx.asset.features.map((feature) => ({
...feature,
value: feature.value.toString("hex")
}))
value: feature.value.toString("hex"),
}));
delete signedTx.id;
return signedTx;
}
};
export const generateRemoveTransaction = (features, nonce = BigInt(0), senderPublicKey = '', networkIdentifier = '', passPhrase = '', fee = BigInt(500000)) => {
export const generateRemoveTransaction = (
features,
nonce = BigInt(0),
senderPublicKey = "",
networkIdentifier = "",
passPhrase = "",
fee = BigInt(500000)
) => {
const tx = {
"moduleID": 1001,
"assetID": 2,
moduleID: 1001,
assetID: 2,
nonce,
senderPublicKey,
fee,
"asset": {
features
asset: {
features,
},
}
const signedTx = transactions.signTransaction(removeFeatureAssetSchema,
tx, Buffer.from(networkIdentifier, "hex"),
passPhrase)
};
const signedTx = transactions.signTransaction(
removeFeatureAssetSchema,
tx,
Buffer.from(networkIdentifier, "hex"),
passPhrase
);
signedTx.senderPublicKey = signedTx.senderPublicKey.toString(
"hex");
signedTx.senderPublicKey = signedTx.senderPublicKey.toString("hex");
signedTx.signatures[0] = signedTx.signatures[0].toString("hex");
delete signedTx.id;
return signedTx;
}
};
const generateValidateTransaction = (features, nonce = BigInt(0), senderPublicKey = '', networkIdentifier = '', passPhrase = '', fee = BigInt(500000), recipientAddress='') => {
const generateValidateTransaction = (
features,
nonce = BigInt(0),
senderPublicKey = "",
networkIdentifier = "",
passPhrase = "",
fee = BigInt(500000),
recipientAddress = ""
) => {
const tx = {
"moduleID": 1001,
"assetID": 11,
moduleID: 1001,
assetID: 11,
nonce,
senderPublicKey: Buffer.from(senderPublicKey, "hex"),
fee,
"asset": {
asset: {
features,
recipientAddress: Buffer.from(recipientAddress, "hex")
recipientAddress: Buffer.from(recipientAddress, "hex"),
},
}
};
if(features.length === 0)
return;
if (features.length === 0) return;
const signedTx = transactions.signTransaction(validateFeatureAssetSchema,
tx, Buffer.from(networkIdentifier, "hex"),
passPhrase)
const signedTx = transactions.signTransaction(
validateFeatureAssetSchema,
tx,
Buffer.from(networkIdentifier, "hex"),
passPhrase
);
signedTx.senderPublicKey = signedTx.senderPublicKey.toString("hex");
signedTx.asset.recipientAddress = signedTx.asset.recipientAddress.toString("hex");
signedTx.asset.recipientAddress =
signedTx.asset.recipientAddress.toString("hex");
signedTx.signatures[0] = signedTx.signatures[0].toString("hex");
signedTx.asset.features = signedTx.asset.features.map(feature => ({
signedTx.asset.features = signedTx.asset.features.map((feature) => ({
...feature,
value: feature.value.toString("hex")
}))
value: feature.value.toString("hex"),
}));
delete signedTx.id;
return signedTx;
}
};
const generateVoteTransaction = (
nonce = BigInt(0),
senderPublicKey = "",
networkIdentifier = "",
passPhrase = "",
fee = BigInt(500000),
delegateAddress = "",
amount = 0n
) => {
const tx = {
moduleID: 5,
assetID: 1,
nonce,
senderPublicKey: Buffer.from(senderPublicKey, "hex"),
fee,
asset: {
votes: [
{
delegateAddress: Buffer.from(delegateAddress, "hex"),
amount: amount * 100000000n,
},
],
},
};
const signedTx = transactions.signTransaction(
voteDelegateAssetSchema,
tx,
Buffer.from(networkIdentifier, "hex"),
passPhrase
);
signedTx.senderPublicKey = signedTx.senderPublicKey.toString("hex");
signedTx.signatures[0] = signedTx.signatures[0].toString("hex");
signedTx.asset.votes = signedTx.asset.votes.map((vote) => ({
...vote,
delegateAddress: vote.delegateAddress.toString("hex"),
}));
delete signedTx.id;
return signedTx;
};
const generateUnlockTransaction = (
nonce = BigInt(0),
senderPublicKey = "",
networkIdentifier = "",
passPhrase = "",
fee = BigInt(500000),
delegateAddress = "",
amount = 0n,
unvoteHeight = 0
) => {
const tx = {
moduleID: 5,
assetID: 2,
nonce,
senderPublicKey: Buffer.from(senderPublicKey, "hex"),
fee,
asset: {
unlockObjects: [
{
delegateAddress: Buffer.from(delegateAddress, "hex"),
amount: amount * 100000000n,
unvoteHeight,
},
],
},
};
const signedTx = transactions.signTransaction(
unlockDelegateAssetSchema,
tx,
Buffer.from(networkIdentifier, "hex"),
passPhrase
);
signedTx.senderPublicKey = signedTx.senderPublicKey.toString("hex");
signedTx.signatures[0] = signedTx.signatures[0].toString("hex");
signedTx.asset.unlockObjects = signedTx.asset.unlockObjects.map((vote) => ({
...vote,
delegateAddress: vote.delegateAddress.toString("hex"),
}));
delete signedTx.id;
return signedTx;
};

31
src/utils/test.js Normal file
View File

@@ -0,0 +1,31 @@
import {cryptography, passphrase} from "@liskhq/lisk-client";
import sodium from 'sodium-universal';
let passphrase1 = passphrase.Mnemonic.generateMnemonic();
const keys = cryptography.getPrivateAndPublicKeyFromPassphrase(passphrase1);
console.log('IDNTTY Public & Private keys(hex):');
console.log('publicKey:', keys.publicKey.length, keys.publicKey.toString('hex'));
console.log('privateKey:', keys.privateKey.length, keys.privateKey.toString('hex'));
console.log('');
console.log('SODIUM Public & Private keys(hex):');
let pk = Buffer.alloc(sodium.crypto_sign_PUBLICKEYBYTES);
let sk = Buffer.alloc(sodium.crypto_sign_SECRETKEYBYTES);
let x25519_sk = Buffer.alloc(sodium.crypto_box_SECRETKEYBYTES);
let x25519_pk = Buffer.alloc(sodium.crypto_box_PUBLICKEYBYTES);
sodium.crypto_sign_seed_keypair(pk, sk, cryptography.hash(Buffer.from(passphrase1, 'UTF-8')));
sodium.crypto_sign_ed25519_sk_to_curve25519(x25519_sk, sk);
sodium.crypto_sign_ed25519_pk_to_curve25519(x25519_pk, pk)
console.log('Public & Private keys(hex):');
console.log('publicKey:', pk.length, pk.toString('hex'));
console.log('privateKey:', sk.length, sk.toString('hex'));
console.log('');
console.log('x25519_sk privateKey:', x25519_sk.length, x25519_sk.toString('Base64'));
console.log('x25519_pk publicKey:', x25519_pk.length, x25519_pk.toString('Base64'));