@airgap/beacon-utils
Advanced tools
Comparing version 0.0.1-beta.11 to 0.0.1-beta.12
@@ -0,1 +1,10 @@ | ||
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { | ||
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } | ||
return new (P || (P = Promise))(function (resolve, reject) { | ||
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } | ||
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } | ||
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } | ||
step((generator = generator.apply(thisArg, _arguments || [])).next()); | ||
}); | ||
}; | ||
import * as bs58check from 'bs58check'; | ||
@@ -18,5 +27,7 @@ import { ready, crypto_generichash, crypto_sign_seed_keypair, from_string, randombytes_buf, crypto_secretbox_NONCEBYTES, crypto_secretbox_easy, crypto_secretbox_open_easy, crypto_sign_ed25519_pk_to_curve25519, crypto_sign_ed25519_sk_to_curve25519, crypto_box_seal, crypto_box_seal_open } from 'libsodium-wrappers'; | ||
*/ | ||
export async function getHexHash(key) { | ||
await ready; | ||
return toHex(crypto_generichash(32, key)); | ||
export function getHexHash(key) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield ready; | ||
return toHex(crypto_generichash(32, key)); | ||
}); | ||
} | ||
@@ -28,5 +39,7 @@ /** | ||
*/ | ||
export async function getKeypairFromSeed(seed) { | ||
await ready; | ||
return crypto_sign_seed_keypair(crypto_generichash(32, from_string(seed))); | ||
export function getKeypairFromSeed(seed) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield ready; | ||
return crypto_sign_seed_keypair(crypto_generichash(32, from_string(seed))); | ||
}); | ||
} | ||
@@ -39,10 +52,12 @@ /** | ||
*/ | ||
export async function encryptCryptoboxPayload(message, sharedKey) { | ||
await ready; | ||
const nonce = Buffer.from(randombytes_buf(crypto_secretbox_NONCEBYTES)); | ||
const combinedPayload = Buffer.concat([ | ||
nonce, | ||
Buffer.from(crypto_secretbox_easy(Buffer.from(message, 'utf8'), nonce, sharedKey)) | ||
]); | ||
return toHex(combinedPayload); | ||
export function encryptCryptoboxPayload(message, sharedKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield ready; | ||
const nonce = Buffer.from(randombytes_buf(crypto_secretbox_NONCEBYTES)); | ||
const combinedPayload = Buffer.concat([ | ||
nonce, | ||
Buffer.from(crypto_secretbox_easy(Buffer.from(message, 'utf8'), nonce, sharedKey)) | ||
]); | ||
return toHex(combinedPayload); | ||
}); | ||
} | ||
@@ -55,7 +70,9 @@ /** | ||
*/ | ||
export async function decryptCryptoboxPayload(payload, sharedKey) { | ||
await ready; | ||
const nonce = payload.slice(0, crypto_secretbox_NONCEBYTES); | ||
const ciphertext = payload.slice(crypto_secretbox_NONCEBYTES); | ||
return Buffer.from(crypto_secretbox_open_easy(ciphertext, nonce, sharedKey)).toString('utf8'); | ||
export function decryptCryptoboxPayload(payload, sharedKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield ready; | ||
const nonce = payload.slice(0, crypto_secretbox_NONCEBYTES); | ||
const ciphertext = payload.slice(crypto_secretbox_NONCEBYTES); | ||
return Buffer.from(crypto_secretbox_open_easy(ciphertext, nonce, sharedKey)).toString('utf8'); | ||
}); | ||
} | ||
@@ -68,7 +85,9 @@ /** | ||
*/ | ||
export async function sealCryptobox(payload, publicKey) { | ||
await ready; | ||
const kxSelfPublicKey = crypto_sign_ed25519_pk_to_curve25519(Buffer.from(publicKey)); // Secret bytes to scalar bytes | ||
const encryptedMessage = crypto_box_seal(payload, kxSelfPublicKey); | ||
return toHex(encryptedMessage); | ||
export function sealCryptobox(payload, publicKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield ready; | ||
const kxSelfPublicKey = crypto_sign_ed25519_pk_to_curve25519(Buffer.from(publicKey)); // Secret bytes to scalar bytes | ||
const encryptedMessage = crypto_box_seal(payload, kxSelfPublicKey); | ||
return toHex(encryptedMessage); | ||
}); | ||
} | ||
@@ -82,8 +101,10 @@ /** | ||
*/ | ||
export async function openCryptobox(encryptedPayload, publicKey, privateKey) { | ||
await ready; | ||
const kxSelfPrivateKey = crypto_sign_ed25519_sk_to_curve25519(Buffer.from(privateKey)); // Secret bytes to scalar bytes | ||
const kxSelfPublicKey = crypto_sign_ed25519_pk_to_curve25519(Buffer.from(publicKey)); // Secret bytes to scalar bytes | ||
const decryptedMessage = crypto_box_seal_open(encryptedPayload, kxSelfPublicKey, kxSelfPrivateKey); | ||
return Buffer.from(decryptedMessage).toString(); | ||
export function openCryptobox(encryptedPayload, publicKey, privateKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield ready; | ||
const kxSelfPrivateKey = crypto_sign_ed25519_sk_to_curve25519(Buffer.from(privateKey)); // Secret bytes to scalar bytes | ||
const kxSelfPublicKey = crypto_sign_ed25519_pk_to_curve25519(Buffer.from(publicKey)); // Secret bytes to scalar bytes | ||
const decryptedMessage = crypto_box_seal_open(encryptedPayload, kxSelfPublicKey, kxSelfPrivateKey); | ||
return Buffer.from(decryptedMessage).toString(); | ||
}); | ||
} | ||
@@ -95,44 +116,46 @@ /** | ||
*/ | ||
export async function getAddressFromPublicKey(publicKey) { | ||
await ready; | ||
const prefixes = { | ||
// tz1... | ||
edpk: { | ||
length: 54, | ||
prefix: Buffer.from(new Uint8Array([6, 161, 159])) | ||
}, | ||
// tz2... | ||
sppk: { | ||
length: 55, | ||
prefix: Buffer.from(new Uint8Array([6, 161, 161])) | ||
}, | ||
// tz3... | ||
p2pk: { | ||
length: 55, | ||
prefix: Buffer.from(new Uint8Array([6, 161, 164])) | ||
export function getAddressFromPublicKey(publicKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
yield ready; | ||
const prefixes = { | ||
// tz1... | ||
edpk: { | ||
length: 54, | ||
prefix: Buffer.from(new Uint8Array([6, 161, 159])) | ||
}, | ||
// tz2... | ||
sppk: { | ||
length: 55, | ||
prefix: Buffer.from(new Uint8Array([6, 161, 161])) | ||
}, | ||
// tz3... | ||
p2pk: { | ||
length: 55, | ||
prefix: Buffer.from(new Uint8Array([6, 161, 164])) | ||
} | ||
}; | ||
let prefix; | ||
let plainPublicKey; | ||
if (publicKey.length === 64) { | ||
prefix = prefixes.edpk.prefix; | ||
plainPublicKey = publicKey; | ||
} | ||
}; | ||
let prefix; | ||
let plainPublicKey; | ||
if (publicKey.length === 64) { | ||
prefix = prefixes.edpk.prefix; | ||
plainPublicKey = publicKey; | ||
} | ||
else { | ||
const entries = Object.entries(prefixes); | ||
for (let index = 0; index < entries.length; index++) { | ||
const [key, value] = entries[index]; | ||
if (publicKey.startsWith(key) && publicKey.length === value.length) { | ||
prefix = value.prefix; | ||
const decoded = bs58check.decode(publicKey); | ||
plainPublicKey = decoded.slice(key.length, decoded.length).toString('hex'); | ||
break; | ||
else { | ||
const entries = Object.entries(prefixes); | ||
for (let index = 0; index < entries.length; index++) { | ||
const [key, value] = entries[index]; | ||
if (publicKey.startsWith(key) && publicKey.length === value.length) { | ||
prefix = value.prefix; | ||
const decoded = bs58check.decode(publicKey); | ||
plainPublicKey = decoded.slice(key.length, decoded.length).toString('hex'); | ||
break; | ||
} | ||
} | ||
} | ||
} | ||
if (!prefix || !plainPublicKey) { | ||
throw new Error(`invalid publicKey: ${publicKey}`); | ||
} | ||
const payload = crypto_generichash(20, Buffer.from(plainPublicKey, 'hex')); | ||
return bs58check.encode(Buffer.concat([prefix, Buffer.from(payload)])); | ||
if (!prefix || !plainPublicKey) { | ||
throw new Error(`invalid publicKey: ${publicKey}`); | ||
} | ||
const payload = crypto_generichash(20, Buffer.from(plainPublicKey, 'hex')); | ||
return bs58check.encode(Buffer.concat([prefix, Buffer.from(payload)])); | ||
}); | ||
} | ||
@@ -139,0 +162,0 @@ /** |
@@ -14,27 +14,6 @@ export var ExposedPromiseStatus; | ||
export class ExposedPromise { | ||
_promise; | ||
_resolve = notInitialized; | ||
_reject = notInitialized; | ||
_status = ExposedPromiseStatus.PENDING; | ||
_promiseResult; | ||
_promiseError; | ||
get promise() { | ||
return this._promise; | ||
} | ||
get resolve() { | ||
return this._resolve; | ||
} | ||
get reject() { | ||
return this._reject; | ||
} | ||
get status() { | ||
return this._status; | ||
} | ||
get promiseResult() { | ||
return this._promiseResult; | ||
} | ||
get promiseError() { | ||
return this._promiseError; | ||
} | ||
constructor() { | ||
this._resolve = notInitialized; | ||
this._reject = notInitialized; | ||
this._status = ExposedPromiseStatus.PENDING; | ||
this._promise = new Promise((innerResolve, innerReject) => { | ||
@@ -61,2 +40,20 @@ this._resolve = (value) => { | ||
} | ||
get promise() { | ||
return this._promise; | ||
} | ||
get resolve() { | ||
return this._resolve; | ||
} | ||
get reject() { | ||
return this._reject; | ||
} | ||
get status() { | ||
return this._status; | ||
} | ||
get promiseResult() { | ||
return this._promiseResult; | ||
} | ||
get promiseError() { | ||
return this._promiseError; | ||
} | ||
static resolve(value) { | ||
@@ -63,0 +60,0 @@ const promise = new ExposedPromise(); |
{ | ||
"name": "@airgap/beacon-utils", | ||
"version": "0.0.1-beta.11", | ||
"version": "0.0.1-beta.12", | ||
"description": "> TODO: description", | ||
@@ -44,3 +44,3 @@ "author": "Andreas Gassmann <andreas@andreasgassmann.ch>", | ||
}, | ||
"gitHead": "9802e63e467d2d0026766d2c2367d4791611ffa1" | ||
"gitHead": "7a149789e4ddde39790bffd633daa0775c86271f" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
48205
854