Skip to content

Commit c997705

Browse files
committed
Viat Wallet new methods
UWProfile new methods Patched file methods encrypt profiles & wallets demo updates profile example code comments
1 parent 453b262 commit c997705

File tree

18 files changed

+610
-557
lines changed

18 files changed

+610
-557
lines changed

UWProfile/index.js

Lines changed: 104 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,37 @@
11
import * as dilithium from '../utilities/cryptoMiddleware/dilithium.js';
22
import * as ed25519 from '../utilities/cryptoMiddleware/ed25519.js';
3+
import {
4+
currentPath,
5+
hasDot,
6+
isBuffer,
7+
isString
8+
} from '@universalweb/acid';
39
import { decode, encode } from '#utilities/serialize';
4-
import { blake3Hash } from '../utilities/cryptoMiddleware/blake3.js';
10+
import { keychainGet, keychainSave } from '../utilities/certificate/keychain.js';
11+
import { read, readStructured, write } from '../utilities/file.js';
12+
import { blake3 } from '@noble/hashes/blake3';
513
import { currentCertificateVersion } from '../defaults.js';
6-
import { isBuffer } from '@universalweb/acid';
7-
import { write } from '../utilities/file.js';
14+
import { x25519_kyber768Half_xchacha20 } from '../utilities/cryptoMiddleware/x25519_Kyber768Half_xChaCha.js';
815
const defaultEncryptionAlgorithm = 1;
916
const defaultSignatureAlgorithm = 1;
17+
const dirname = currentPath(import.meta);
1018
export class UWProfile {
11-
constructor(config = {}) {
19+
constructor(config = {}, optionalArg) {
1220
if (config === false) {
1321
return this;
1422
}
15-
return this.initialize(config);
23+
return this.initialize(config, optionalArg);
1624
}
17-
async initialize(config) {
18-
if (isBuffer(config)) {
19-
this.importFromBinary(config);
20-
} else if (config.publicKey || config.privateKey) {
25+
async initialize(config, optionalArg) {
26+
if (isString(config)) {
27+
if (config.includes('/') || config.includes('\\') || hasDot(config)) {
28+
await this.importFile(config, optionalArg);
29+
} else {
30+
await this.importFromKeychain(config, optionalArg);
31+
}
32+
} else if (isBuffer(config)) {
33+
await this.importFromBinary(config, optionalArg);
34+
} else if (config?.publicKey || config?.privateKey) {
2135
const {
2236
version,
2337
publicKey,
@@ -37,19 +51,22 @@ export class UWProfile {
3751
}
3852
return this;
3953
}
40-
async generate(config) {
41-
const {
42-
signatureAlgorithm = defaultSignatureAlgorithm,
43-
encryptionAlgorithm = defaultEncryptionAlgorithm,
44-
version,
45-
} = config;
46-
this.version = version || currentCertificateVersion;
54+
async generateSignatureKeypair() {
55+
this.version = currentCertificateVersion;
4756
const ed25519NewKeypair = await ed25519.signatureKeypair();
4857
const dilithiumNewKeypair = await dilithium.signatureKeypair();
4958
console.log(ed25519NewKeypair.publicKey, ed25519NewKeypair.privateKey);
5059
this.publicKey = Buffer.concat([ed25519NewKeypair.publicKey, dilithiumNewKeypair.publicKey]);
5160
this.privateKey = Buffer.concat([ed25519NewKeypair.privateKey, dilithiumNewKeypair.privateKey]);
5261
}
62+
async generateEncryptionKeypair() {
63+
const encryptionkeypair = await x25519_kyber768Half_xchacha20.keypair();
64+
this.encryptionkeypair = encryptionkeypair;
65+
}
66+
async generate(config) {
67+
await this.generateSignatureKeypair();
68+
await this.generateEncryptionKeypair();
69+
}
5370
get ed25519PublicKey() {
5471
return this.publicKey.slice(0, 32);
5572
}
@@ -79,43 +96,91 @@ export class UWProfile {
7996
return (ed25519Verify === dilithiumVerify) ? ed25519Verify : false;
8097
}
8198
async hash(message) {
82-
const hashedMessage = blake3Hash.hash(message);
99+
const hashedMessage = blake3(message);
83100
return hashedMessage;
84101
}
85-
async importFromBinary(data) {
86-
const decodedData = decode(data);
87-
this.version = decodedData.version;
88-
this.publicKey = decodedData.publicKey;
89-
this.privateKey = decodedData.privateKey;
102+
async importFromBinary(data, encryptionKey) {
103+
const password = (isString(encryptionKey)) ? await this.hash(Buffer.from(encryptionKey)) : encryptionKey;
104+
const decodedData = (password) ? await this.decryptBinary(data, password) : decode(data);
105+
await this.importFromObject(decodedData);
106+
return this;
107+
}
108+
async importFromObject(decodedData, encryptionKey) {
109+
const password = (isString(encryptionKey)) ? await this.hash(Buffer.from(encryptionKey)) : encryptionKey;
110+
const data = (password) ? await this.decryptBinary(decodedData.encrypted, password) : decodedData;
111+
this.version = data.version;
112+
this.publicKey = data.publicKey;
113+
this.privateKey = data.privateKey;
114+
this.encryptionKeypair = data.encryptionKeypair;
90115
return this;
91116
}
92-
async exportAsBinary() {
117+
async decryptBinary(encryptedObject, encryptionPassword) {
118+
const decrypted = await x25519_kyber768Half_xchacha20.decrypt(encryptedObject, encryptionPassword);
119+
return decode(decrypted);
120+
}
121+
async exportBinary(encryptionKey) {
93122
const data = {
94123
version: this.version,
95124
publicKey: this.publicKey,
96125
privateKey: this.privateKey,
126+
encryptionKeypair: this.encryptionkeypair
97127
};
98128
const dataEncoded = encode(data);
129+
if (encryptionKey) {
130+
const password = (isString(encryptionKey)) ? await this.hash(Buffer.from(encryptionKey)) : encryptionKey;
131+
console.log(password);
132+
const encryptedData = await x25519_kyber768Half_xchacha20.encrypt(dataEncoded, password);
133+
const encryptedObject = {
134+
encrypted: encryptedData,
135+
};
136+
return encode(encryptedObject);
137+
}
99138
return dataEncoded;
100139
}
101-
async saveToFile(fileLocation, fileName) {
102-
const dataEncoded = await this.exportAsBinary();
140+
async saveToFile(fileName, fileLocation, encryptionPassword) {
141+
const binaryData = await this.exportBinary(encryptionPassword);
103142
const fullPath = `${fileLocation}/${fileName}`;
104-
write(fullPath, dataEncoded, 'binary', true);
143+
return write(fullPath, binaryData, 'binary', true);
144+
}
145+
async importFile(filePath, encryptionPassword) {
146+
const data = await readStructured(filePath);
147+
if (data) {
148+
return this.importFromObject(data, encryptionPassword);
149+
}
150+
console.log('Error Importing Profile', filePath);
151+
return false;
152+
}
153+
async saveToKeychain(accountName = 'UWProfile', encryptionPassword) {
154+
const binaryData = await this.exportBinary(encryptionPassword);
155+
const config = {
156+
account: this.accountName || accountName,
157+
password: binaryData,
158+
};
159+
console.log('Profile Size', binaryData.length);
160+
return keychainSave(config);
161+
}
162+
async importFromKeychain(accountName = 'UWProfile', encryptionPassword) {
163+
const keychainObject = await keychainGet(this.accountName || accountName);
164+
await this.importFromObject(keychainObject, encryptionPassword);
105165
}
106166
}
107-
export async function uwProfile(config) {
108-
const source = new UWProfile(config);
167+
export async function uwProfile(config, optionalArg) {
168+
const source = new UWProfile(config, optionalArg);
109169
return source;
110170
}
111-
const exampleProfileExample = await uwProfile();
112-
console.log(await exampleProfileExample);
113-
console.log(`Version: ${exampleProfileExample.version}`);
114-
console.log(`Public Key Size: ${exampleProfileExample.publicKey.length}`);
115-
console.log(`Private Key Size: ${exampleProfileExample.privateKey.length}`);
116-
console.log(exampleProfileExample.ed25519PublicKey);
117-
console.log(exampleProfileExample.ed25519PrivateKey);
118-
const messageExample = Buffer.from('Hello, World!');
119-
const sig = await exampleProfileExample.sign(messageExample);
120-
console.log(sig);
121-
console.log(await exampleProfileExample.verifySignature(sig, messageExample));
171+
// const exampleProfileExample = await uwProfile();
172+
// const encryptionPasswordExample = 'password';
173+
// console.log(await exampleProfileExample);
174+
// console.log(`Version: ${exampleProfileExample.version}`);
175+
// console.log(`Public Key Size: ${exampleProfileExample.publicKey.length}`);
176+
// console.log(`Private Key Size: ${exampleProfileExample.privateKey.length}`);
177+
// console.log(exampleProfileExample.ed25519PublicKey);
178+
// console.log(exampleProfileExample.ed25519PrivateKey);
179+
// const messageExample = Buffer.from('Hello, World!');
180+
// const sig = await exampleProfileExample.sign(messageExample);
181+
// console.log(sig);
182+
// console.log(await exampleProfileExample.verifySignature(sig, messageExample));
183+
// await exampleProfileExample.saveToKeychain('exampleUWProfile', encryptionPasswordExample);
184+
// await exampleProfileExample.saveToFile('profile.cert', `${dirname}/../profiles`, encryptionPasswordExample);
185+
// console.log(await uwProfile('exampleUWProfile', encryptionPasswordExample));
186+
// console.log(await uwProfile(`${dirname}/../profiles/profile.cert`, encryptionPasswordExample));

eslint.config.js

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -166,18 +166,7 @@ export default [
166166
}
167167
],
168168
'@stylistic/no-confusing-arrow': 'error',
169-
'@stylistic/no-extra-parens': [
170-
'error',
171-
'all',
172-
{
173-
conditionalAssign: false,
174-
enforceForSequenceExpressions: false,
175-
nestedBinaryExpressions: false,
176-
returnAssign: false,
177-
ternaryOperandBinaryExpressions: false,
178-
enforceForFunctionPrototypeMethods: false,
179-
}
180-
],
169+
'@stylistic/no-extra-parens': 'off',
181170
'@stylistic/no-extra-semi': 'error',
182171
'@stylistic/no-floating-decimal': 'error',
183172
'@stylistic/no-mixed-operators': 'error',

0 commit comments

Comments
 (0)