@airgap/beacon-utils
Advanced tools
Comparing version 4.0.11-icp-icrc25-beta.0 to 4.0.11
@@ -11,14 +11,41 @@ "use strict"; | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.signMessage = exports.recipientString = exports.prefixPublicKey = exports.getAddressFromPublicKey = exports.openCryptobox = exports.sealCryptobox = exports.decryptCryptoboxPayload = exports.encryptCryptoboxPayload = exports.getKeypairFromSeed = exports.getHexHash = exports.toHex = exports.secretbox_MACBYTES = exports.secretbox_NONCEBYTES = void 0; | ||
const bs58check = require("bs58check"); | ||
const nacl_1 = require("@stablelib/nacl"); | ||
const random_1 = require("@stablelib/random"); | ||
const utf8_1 = require("@stablelib/utf8"); | ||
const blake2b_1 = require("@stablelib/blake2b"); | ||
const ed25519_1 = require("@stablelib/ed25519"); | ||
const ed25519_2 = require("@stablelib/ed25519"); | ||
const blake2b_2 = require("@stablelib/blake2b"); | ||
const bytes_1 = require("@stablelib/bytes"); | ||
const ed25519_3 = require("@stablelib/ed25519"); | ||
var bs58check = require("bs58check"); | ||
var nacl_1 = require("@stablelib/nacl"); | ||
var random_1 = require("@stablelib/random"); | ||
var utf8_1 = require("@stablelib/utf8"); | ||
var blake2b_1 = require("@stablelib/blake2b"); | ||
var ed25519_1 = require("@stablelib/ed25519"); | ||
var ed25519_2 = require("@stablelib/ed25519"); | ||
var blake2b_2 = require("@stablelib/blake2b"); | ||
var bytes_1 = require("@stablelib/bytes"); | ||
var ed25519_3 = require("@stablelib/ed25519"); | ||
exports.secretbox_NONCEBYTES = 24; // crypto_secretbox_NONCEBYTES | ||
@@ -43,7 +70,9 @@ exports.secretbox_MACBYTES = 16; // crypto_secretbox_MACBYTES | ||
function getHexHash(key) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (typeof key === 'string') { | ||
return toHex((0, blake2b_1.hash)((0, utf8_1.encode)(key), 32)); | ||
} | ||
return toHex((0, blake2b_1.hash)(key, 32)); | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
if (typeof key === 'string') { | ||
return [2 /*return*/, toHex((0, blake2b_1.hash)((0, utf8_1.encode)(key), 32))]; | ||
} | ||
return [2 /*return*/, toHex((0, blake2b_1.hash)(key, 32))]; | ||
}); | ||
}); | ||
@@ -58,4 +87,6 @@ } | ||
function getKeypairFromSeed(seed) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return (0, ed25519_1.generateKeyPairFromSeed)((0, blake2b_1.hash)((0, utf8_1.encode)(seed), 32)); | ||
return __awaiter(this, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, (0, ed25519_1.generateKeyPairFromSeed)((0, blake2b_1.hash)((0, utf8_1.encode)(seed), 32))]; | ||
}); | ||
}); | ||
@@ -71,9 +102,12 @@ } | ||
function encryptCryptoboxPayload(message, sharedKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const nonce = Buffer.from((0, random_1.randomBytes)(exports.secretbox_NONCEBYTES)); | ||
const combinedPayload = Buffer.concat([ | ||
nonce, | ||
Buffer.from((0, nacl_1.secretBox)(sharedKey, nonce, Buffer.from(message, 'utf8'))) | ||
]); | ||
return toHex(combinedPayload); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var nonce, combinedPayload; | ||
return __generator(this, function (_a) { | ||
nonce = Buffer.from((0, random_1.randomBytes)(exports.secretbox_NONCEBYTES)); | ||
combinedPayload = Buffer.concat([ | ||
nonce, | ||
Buffer.from((0, nacl_1.secretBox)(sharedKey, nonce, Buffer.from(message, 'utf8'))) | ||
]); | ||
return [2 /*return*/, toHex(combinedPayload)]; | ||
}); | ||
}); | ||
@@ -89,10 +123,13 @@ } | ||
function decryptCryptoboxPayload(payload, sharedKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const nonce = payload.slice(0, exports.secretbox_NONCEBYTES); | ||
const ciphertext = payload.slice(exports.secretbox_NONCEBYTES); | ||
const openBox = (0, nacl_1.openSecretBox)(sharedKey, nonce, ciphertext); | ||
if (!openBox) { | ||
throw new Error('Decryption failed'); | ||
} | ||
return Buffer.from(openBox).toString('utf8'); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var nonce, ciphertext, openBox; | ||
return __generator(this, function (_a) { | ||
nonce = payload.slice(0, exports.secretbox_NONCEBYTES); | ||
ciphertext = payload.slice(exports.secretbox_NONCEBYTES); | ||
openBox = (0, nacl_1.openSecretBox)(sharedKey, nonce, ciphertext); | ||
if (!openBox) { | ||
throw new Error('Decryption failed'); | ||
} | ||
return [2 /*return*/, Buffer.from(openBox).toString('utf8')]; | ||
}); | ||
}); | ||
@@ -108,10 +145,14 @@ } | ||
function sealCryptobox(payload, otherPublicKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const kxOtherPublicKey = (0, ed25519_2.convertPublicKeyToX25519)(Buffer.from(otherPublicKey)); // Secret bytes to scalar bytes | ||
const keypair = (0, nacl_1.generateKeyPair)(); | ||
const state = new blake2b_2.BLAKE2b(24); | ||
const nonce = state.update(keypair.publicKey, 32).update(kxOtherPublicKey, 32).digest(); | ||
const bytesPayload = typeof payload === 'string' ? (0, utf8_1.encode)(payload) : payload; | ||
const encryptedMessage = (0, nacl_1.box)(kxOtherPublicKey, keypair.secretKey, nonce, bytesPayload); | ||
return toHex((0, bytes_1.concat)(keypair.publicKey, encryptedMessage)); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var kxOtherPublicKey, keypair, state, nonce, bytesPayload, encryptedMessage; | ||
return __generator(this, function (_a) { | ||
kxOtherPublicKey = (0, ed25519_2.convertPublicKeyToX25519)(Buffer.from(otherPublicKey)) // Secret bytes to scalar bytes | ||
; | ||
keypair = (0, nacl_1.generateKeyPair)(); | ||
state = new blake2b_2.BLAKE2b(24); | ||
nonce = state.update(keypair.publicKey, 32).update(kxOtherPublicKey, 32).digest(); | ||
bytesPayload = typeof payload === 'string' ? (0, utf8_1.encode)(payload) : payload; | ||
encryptedMessage = (0, nacl_1.box)(kxOtherPublicKey, keypair.secretKey, nonce, bytesPayload); | ||
return [2 /*return*/, toHex((0, bytes_1.concat)(keypair.publicKey, encryptedMessage))]; | ||
}); | ||
}); | ||
@@ -128,15 +169,20 @@ } | ||
function openCryptobox(encryptedPayload, publicKey, privateKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const kxSelfPrivateKey = (0, ed25519_2.convertSecretKeyToX25519)(Buffer.from(privateKey)); // Secret bytes to scalar bytes | ||
const kxSelfPublicKey = (0, ed25519_2.convertPublicKeyToX25519)(Buffer.from(publicKey)); // Secret bytes to scalar bytes | ||
const bytesPayload = typeof encryptedPayload === 'string' ? (0, utf8_1.encode)(encryptedPayload) : encryptedPayload; | ||
const epk = bytesPayload.slice(0, 32); | ||
const ciphertext = bytesPayload.slice(32); | ||
const state = new blake2b_2.BLAKE2b(24); | ||
const nonce = state.update(epk, 32).update(kxSelfPublicKey, 32).digest(); | ||
const decryptedMessage2 = (0, nacl_1.openBox)(epk, kxSelfPrivateKey, nonce, ciphertext); | ||
if (!decryptedMessage2) { | ||
throw new Error('Decryption failed'); | ||
} | ||
return Buffer.from(decryptedMessage2).toString(); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var kxSelfPrivateKey, kxSelfPublicKey, bytesPayload, epk, ciphertext, state, nonce, decryptedMessage2; | ||
return __generator(this, function (_a) { | ||
kxSelfPrivateKey = (0, ed25519_2.convertSecretKeyToX25519)(Buffer.from(privateKey)) // Secret bytes to scalar bytes | ||
; | ||
kxSelfPublicKey = (0, ed25519_2.convertPublicKeyToX25519)(Buffer.from(publicKey)) // Secret bytes to scalar bytes | ||
; | ||
bytesPayload = typeof encryptedPayload === 'string' ? (0, utf8_1.encode)(encryptedPayload) : encryptedPayload; | ||
epk = bytesPayload.slice(0, 32); | ||
ciphertext = bytesPayload.slice(32); | ||
state = new blake2b_2.BLAKE2b(24); | ||
nonce = state.update(epk, 32).update(kxSelfPublicKey, 32).digest(); | ||
decryptedMessage2 = (0, nacl_1.openBox)(epk, kxSelfPrivateKey, nonce, ciphertext); | ||
if (!decryptedMessage2) { | ||
throw new Error('Decryption failed'); | ||
} | ||
return [2 /*return*/, Buffer.from(decryptedMessage2).toString()]; | ||
}); | ||
}); | ||
@@ -151,43 +197,44 @@ } | ||
function getAddressFromPublicKey(publicKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
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])) | ||
return __awaiter(this, void 0, void 0, function () { | ||
var prefixes, prefix, plainPublicKey, entries, index, _a, key, value, decoded, payload; | ||
return __generator(this, function (_b) { | ||
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])) | ||
} | ||
}; | ||
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 { | ||
entries = Object.entries(prefixes); | ||
for (index = 0; index < entries.length; index++) { | ||
_a = entries[index], key = _a[0], value = _a[1]; | ||
if (publicKey.startsWith(key) && publicKey.length === value.length) { | ||
prefix = value.prefix; | ||
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 = (0, blake2b_1.hash)(Buffer.from(plainPublicKey, 'hex'), 20); | ||
return bs58check.encode(Buffer.concat([prefix, Buffer.from(payload)])); | ||
if (!prefix || !plainPublicKey) { | ||
throw new Error("invalid publicKey: ".concat(publicKey)); | ||
} | ||
payload = (0, blake2b_1.hash)(Buffer.from(plainPublicKey, 'hex'), 20); | ||
return [2 /*return*/, bs58check.encode(Buffer.concat([prefix, Buffer.from(payload)]))]; | ||
}); | ||
}); | ||
@@ -202,8 +249,11 @@ } | ||
function prefixPublicKey(publicKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (publicKey.length !== 64) { | ||
return publicKey; | ||
} | ||
const payload = Buffer.from(publicKey, 'hex'); | ||
return bs58check.encode(Buffer.concat([new Uint8Array([13, 15, 37, 217]), Buffer.from(payload)])); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var payload; | ||
return __generator(this, function (_a) { | ||
if (publicKey.length !== 64) { | ||
return [2 /*return*/, publicKey]; | ||
} | ||
payload = Buffer.from(publicKey, 'hex'); | ||
return [2 /*return*/, bs58check.encode(Buffer.concat([new Uint8Array([13, 15, 37, 217]), Buffer.from(payload)]))]; | ||
}); | ||
}); | ||
@@ -219,32 +269,49 @@ } | ||
function recipientString(recipientHash, relayServer) { | ||
return `@${recipientHash}:${relayServer}`; | ||
return "@".concat(recipientHash, ":").concat(relayServer); | ||
} | ||
exports.recipientString = recipientString; | ||
const toBuffer = (message) => __awaiter(void 0, void 0, void 0, function* () { | ||
if (message.length % 2 !== 0) { | ||
return (0, utf8_1.encode)(message); | ||
} | ||
let adjustedMessage = message; | ||
if (message.startsWith('0x')) { | ||
adjustedMessage = message.slice(2); | ||
} | ||
const buffer = Buffer.from(adjustedMessage, 'hex'); | ||
if (buffer.length === adjustedMessage.length / 2) { | ||
return buffer; | ||
} | ||
return (0, utf8_1.encode)(message); | ||
}); | ||
const coinlibhash = (message, size = 32) => __awaiter(void 0, void 0, void 0, function* () { | ||
return (0, blake2b_1.hash)(message, size); | ||
}); | ||
const signMessage = (message, keypair) => __awaiter(void 0, void 0, void 0, function* () { | ||
const bufferMessage = yield toBuffer(message); | ||
const edsigPrefix = new Uint8Array([9, 245, 205, 134, 18]); | ||
const hash = yield coinlibhash(bufferMessage); | ||
const rawSignature = (0, ed25519_3.sign)(keypair.secretKey, hash); | ||
const signature = bs58check.encode(Buffer.concat([Buffer.from(edsigPrefix), Buffer.from(rawSignature)])); | ||
return signature; | ||
}); | ||
var toBuffer = function (message) { return __awaiter(void 0, void 0, void 0, function () { | ||
var adjustedMessage, buffer; | ||
return __generator(this, function (_a) { | ||
if (message.length % 2 !== 0) { | ||
return [2 /*return*/, (0, utf8_1.encode)(message)]; | ||
} | ||
adjustedMessage = message; | ||
if (message.startsWith('0x')) { | ||
adjustedMessage = message.slice(2); | ||
} | ||
buffer = Buffer.from(adjustedMessage, 'hex'); | ||
if (buffer.length === adjustedMessage.length / 2) { | ||
return [2 /*return*/, buffer]; | ||
} | ||
return [2 /*return*/, (0, utf8_1.encode)(message)]; | ||
}); | ||
}); }; | ||
var coinlibhash = function (message, size) { | ||
if (size === void 0) { size = 32; } | ||
return __awaiter(void 0, void 0, void 0, function () { | ||
return __generator(this, function (_a) { | ||
return [2 /*return*/, (0, blake2b_1.hash)(message, size)]; | ||
}); | ||
}); | ||
}; | ||
var signMessage = function (message, keypair) { return __awaiter(void 0, void 0, void 0, function () { | ||
var bufferMessage, edsigPrefix, hash, rawSignature, signature; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, toBuffer(message)]; | ||
case 1: | ||
bufferMessage = _a.sent(); | ||
edsigPrefix = new Uint8Array([9, 245, 205, 134, 18]); | ||
return [4 /*yield*/, coinlibhash(bufferMessage)]; | ||
case 2: | ||
hash = _a.sent(); | ||
rawSignature = (0, ed25519_3.sign)(keypair.secretKey, hash); | ||
signature = bs58check.encode(Buffer.concat([Buffer.from(edsigPrefix), Buffer.from(rawSignature)])); | ||
return [2 /*return*/, signature]; | ||
} | ||
}); | ||
}); }; | ||
exports.signMessage = signMessage; | ||
/* eslint-enable prefer-arrow/prefer-arrow-functions */ | ||
//# sourceMappingURL=crypto.js.map |
@@ -6,4 +6,4 @@ export declare enum ExposedPromiseStatus { | ||
} | ||
type Resolve<T> = (value: T) => void; | ||
type Reject<U> = (reason?: U) => void; | ||
declare type Resolve<T> = (value: T) => void; | ||
declare type Reject<U> = (reason?: U) => void; | ||
/** | ||
@@ -10,0 +10,0 @@ * Exposed promise allow you to create a promise and then resolve it later, from the outside |
@@ -9,4 +9,4 @@ "use strict"; | ||
ExposedPromiseStatus["REJECTED"] = "rejected"; | ||
})(ExposedPromiseStatus || (exports.ExposedPromiseStatus = ExposedPromiseStatus = {})); | ||
const notInitialized = () => { | ||
})(ExposedPromiseStatus = exports.ExposedPromiseStatus || (exports.ExposedPromiseStatus = {})); | ||
var notInitialized = function () { | ||
throw new Error('ExposedPromise not initialized yet.'); | ||
@@ -17,42 +17,25 @@ }; | ||
*/ | ||
class ExposedPromise { | ||
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() { | ||
var ExposedPromise = /** @class */ (function () { | ||
function ExposedPromise() { | ||
var _this = this; | ||
this._resolve = notInitialized; | ||
this._reject = notInitialized; | ||
this._status = ExposedPromiseStatus.PENDING; | ||
this._promise = new Promise((innerResolve, innerReject) => { | ||
this._resolve = (value) => { | ||
if (this.isSettled()) { | ||
this._promise = new Promise(function (innerResolve, innerReject) { | ||
_this._resolve = function (value) { | ||
if (_this.isSettled()) { | ||
return; | ||
} | ||
this._promiseResult = value; | ||
_this._promiseResult = value; | ||
innerResolve(value); | ||
this._status = ExposedPromiseStatus.RESOLVED; | ||
_this._status = ExposedPromiseStatus.RESOLVED; | ||
return; | ||
}; | ||
this._reject = (reason) => { | ||
if (this.isSettled()) { | ||
_this._reject = function (reason) { | ||
if (_this.isSettled()) { | ||
return; | ||
} | ||
this._promiseError = reason; | ||
_this._promiseError = reason; | ||
innerReject(reason); | ||
this._status = ExposedPromiseStatus.REJECTED; | ||
_this._status = ExposedPromiseStatus.REJECTED; | ||
return; | ||
@@ -62,26 +45,69 @@ }; | ||
} | ||
static resolve(value) { | ||
const promise = new ExposedPromise(); | ||
Object.defineProperty(ExposedPromise.prototype, "promise", { | ||
get: function () { | ||
return this._promise; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
Object.defineProperty(ExposedPromise.prototype, "resolve", { | ||
get: function () { | ||
return this._resolve; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
Object.defineProperty(ExposedPromise.prototype, "reject", { | ||
get: function () { | ||
return this._reject; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
Object.defineProperty(ExposedPromise.prototype, "status", { | ||
get: function () { | ||
return this._status; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
Object.defineProperty(ExposedPromise.prototype, "promiseResult", { | ||
get: function () { | ||
return this._promiseResult; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
Object.defineProperty(ExposedPromise.prototype, "promiseError", { | ||
get: function () { | ||
return this._promiseError; | ||
}, | ||
enumerable: false, | ||
configurable: true | ||
}); | ||
ExposedPromise.resolve = function (value) { | ||
var promise = new ExposedPromise(); | ||
promise.resolve(value); | ||
return promise; | ||
} | ||
static reject(reason) { | ||
const promise = new ExposedPromise(); | ||
}; | ||
ExposedPromise.reject = function (reason) { | ||
var promise = new ExposedPromise(); | ||
promise.reject(reason); | ||
return promise; | ||
} | ||
isPending() { | ||
}; | ||
ExposedPromise.prototype.isPending = function () { | ||
return this.status === ExposedPromiseStatus.PENDING; | ||
} | ||
isResolved() { | ||
}; | ||
ExposedPromise.prototype.isResolved = function () { | ||
return this.status === ExposedPromiseStatus.RESOLVED; | ||
} | ||
isRejected() { | ||
}; | ||
ExposedPromise.prototype.isRejected = function () { | ||
return this.status === ExposedPromiseStatus.REJECTED; | ||
} | ||
isSettled() { | ||
}; | ||
ExposedPromise.prototype.isSettled = function () { | ||
return this.isResolved() || this.isRejected(); | ||
} | ||
} | ||
}; | ||
return ExposedPromise; | ||
}()); | ||
exports.ExposedPromise = ExposedPromise; | ||
//# sourceMappingURL=exposed-promise.js.map |
@@ -11,6 +11,33 @@ "use strict"; | ||
}; | ||
var __generator = (this && this.__generator) || function (thisArg, body) { | ||
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; | ||
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; | ||
function verb(n) { return function (v) { return step([n, v]); }; } | ||
function step(op) { | ||
if (f) throw new TypeError("Generator is already executing."); | ||
while (_) try { | ||
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; | ||
if (y = 0, t) op = [op[0] & 2, t.value]; | ||
switch (op[0]) { | ||
case 0: case 1: t = op; break; | ||
case 4: _.label++; return { value: op[1], done: false }; | ||
case 5: _.label++; y = op[1]; op = [0]; continue; | ||
case 7: op = _.ops.pop(); _.trys.pop(); continue; | ||
default: | ||
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } | ||
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } | ||
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } | ||
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } | ||
if (t[2]) _.ops.pop(); | ||
_.trys.pop(); continue; | ||
} | ||
op = body.call(thisArg, _); | ||
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } | ||
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; | ||
} | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.generateGUID = void 0; | ||
/* eslint-disable prefer-arrow/prefer-arrow-functions */ | ||
const random_1 = require("@stablelib/random"); | ||
var random_1 = require("@stablelib/random"); | ||
/** | ||
@@ -20,9 +47,12 @@ * Generate a random GUID | ||
function generateGUID() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const buf = (0, random_1.randomBytes)(16); | ||
return [buf.slice(0, 4), buf.slice(4, 6), buf.slice(6, 8), buf.slice(8, 10), buf.slice(10, 16)] | ||
.map(function (subbuf) { | ||
return Buffer.from(subbuf).toString('hex'); | ||
}) | ||
.join('-'); | ||
return __awaiter(this, void 0, void 0, function () { | ||
var buf; | ||
return __generator(this, function (_a) { | ||
buf = (0, random_1.randomBytes)(16); | ||
return [2 /*return*/, [buf.slice(0, 4), buf.slice(4, 6), buf.slice(6, 8), buf.slice(8, 10), buf.slice(10, 16)] | ||
.map(function (subbuf) { | ||
return Buffer.from(subbuf).toString('hex'); | ||
}) | ||
.join('-')]; | ||
}); | ||
}); | ||
@@ -29,0 +59,0 @@ } |
@@ -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'; | ||
@@ -28,7 +37,9 @@ import { box, generateKeyPair, openBox, openSecretBox, secretBox } from '@stablelib/nacl'; | ||
*/ | ||
export async function getHexHash(key) { | ||
if (typeof key === 'string') { | ||
return toHex(hash(encode(key), 32)); | ||
} | ||
return toHex(hash(key, 32)); | ||
export function getHexHash(key) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (typeof key === 'string') { | ||
return toHex(hash(encode(key), 32)); | ||
} | ||
return toHex(hash(key, 32)); | ||
}); | ||
} | ||
@@ -40,4 +51,6 @@ /** | ||
*/ | ||
export async function getKeypairFromSeed(seed) { | ||
return generateKeyPairFromSeed(hash(encode(seed), 32)); | ||
export function getKeypairFromSeed(seed) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return generateKeyPairFromSeed(hash(encode(seed), 32)); | ||
}); | ||
} | ||
@@ -50,9 +63,11 @@ /** | ||
*/ | ||
export async function encryptCryptoboxPayload(message, sharedKey) { | ||
const nonce = Buffer.from(randomBytes(secretbox_NONCEBYTES)); | ||
const combinedPayload = Buffer.concat([ | ||
nonce, | ||
Buffer.from(secretBox(sharedKey, nonce, Buffer.from(message, 'utf8'))) | ||
]); | ||
return toHex(combinedPayload); | ||
export function encryptCryptoboxPayload(message, sharedKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const nonce = Buffer.from(randomBytes(secretbox_NONCEBYTES)); | ||
const combinedPayload = Buffer.concat([ | ||
nonce, | ||
Buffer.from(secretBox(sharedKey, nonce, Buffer.from(message, 'utf8'))) | ||
]); | ||
return toHex(combinedPayload); | ||
}); | ||
} | ||
@@ -65,10 +80,12 @@ /** | ||
*/ | ||
export async function decryptCryptoboxPayload(payload, sharedKey) { | ||
const nonce = payload.slice(0, secretbox_NONCEBYTES); | ||
const ciphertext = payload.slice(secretbox_NONCEBYTES); | ||
const openBox = openSecretBox(sharedKey, nonce, ciphertext); | ||
if (!openBox) { | ||
throw new Error('Decryption failed'); | ||
} | ||
return Buffer.from(openBox).toString('utf8'); | ||
export function decryptCryptoboxPayload(payload, sharedKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const nonce = payload.slice(0, secretbox_NONCEBYTES); | ||
const ciphertext = payload.slice(secretbox_NONCEBYTES); | ||
const openBox = openSecretBox(sharedKey, nonce, ciphertext); | ||
if (!openBox) { | ||
throw new Error('Decryption failed'); | ||
} | ||
return Buffer.from(openBox).toString('utf8'); | ||
}); | ||
} | ||
@@ -81,10 +98,12 @@ /** | ||
*/ | ||
export async function sealCryptobox(payload, otherPublicKey) { | ||
const kxOtherPublicKey = convertPublicKeyToX25519(Buffer.from(otherPublicKey)); // Secret bytes to scalar bytes | ||
const keypair = generateKeyPair(); | ||
const state = new BLAKE2b(24); | ||
const nonce = state.update(keypair.publicKey, 32).update(kxOtherPublicKey, 32).digest(); | ||
const bytesPayload = typeof payload === 'string' ? encode(payload) : payload; | ||
const encryptedMessage = box(kxOtherPublicKey, keypair.secretKey, nonce, bytesPayload); | ||
return toHex(concat(keypair.publicKey, encryptedMessage)); | ||
export function sealCryptobox(payload, otherPublicKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const kxOtherPublicKey = convertPublicKeyToX25519(Buffer.from(otherPublicKey)); // Secret bytes to scalar bytes | ||
const keypair = generateKeyPair(); | ||
const state = new BLAKE2b(24); | ||
const nonce = state.update(keypair.publicKey, 32).update(kxOtherPublicKey, 32).digest(); | ||
const bytesPayload = typeof payload === 'string' ? encode(payload) : payload; | ||
const encryptedMessage = box(kxOtherPublicKey, keypair.secretKey, nonce, bytesPayload); | ||
return toHex(concat(keypair.publicKey, encryptedMessage)); | ||
}); | ||
} | ||
@@ -98,15 +117,17 @@ /** | ||
*/ | ||
export async function openCryptobox(encryptedPayload, publicKey, privateKey) { | ||
const kxSelfPrivateKey = convertSecretKeyToX25519(Buffer.from(privateKey)); // Secret bytes to scalar bytes | ||
const kxSelfPublicKey = convertPublicKeyToX25519(Buffer.from(publicKey)); // Secret bytes to scalar bytes | ||
const bytesPayload = typeof encryptedPayload === 'string' ? encode(encryptedPayload) : encryptedPayload; | ||
const epk = bytesPayload.slice(0, 32); | ||
const ciphertext = bytesPayload.slice(32); | ||
const state = new BLAKE2b(24); | ||
const nonce = state.update(epk, 32).update(kxSelfPublicKey, 32).digest(); | ||
const decryptedMessage2 = openBox(epk, kxSelfPrivateKey, nonce, ciphertext); | ||
if (!decryptedMessage2) { | ||
throw new Error('Decryption failed'); | ||
} | ||
return Buffer.from(decryptedMessage2).toString(); | ||
export function openCryptobox(encryptedPayload, publicKey, privateKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const kxSelfPrivateKey = convertSecretKeyToX25519(Buffer.from(privateKey)); // Secret bytes to scalar bytes | ||
const kxSelfPublicKey = convertPublicKeyToX25519(Buffer.from(publicKey)); // Secret bytes to scalar bytes | ||
const bytesPayload = typeof encryptedPayload === 'string' ? encode(encryptedPayload) : encryptedPayload; | ||
const epk = bytesPayload.slice(0, 32); | ||
const ciphertext = bytesPayload.slice(32); | ||
const state = new BLAKE2b(24); | ||
const nonce = state.update(epk, 32).update(kxSelfPublicKey, 32).digest(); | ||
const decryptedMessage2 = openBox(epk, kxSelfPrivateKey, nonce, ciphertext); | ||
if (!decryptedMessage2) { | ||
throw new Error('Decryption failed'); | ||
} | ||
return Buffer.from(decryptedMessage2).toString(); | ||
}); | ||
} | ||
@@ -118,43 +139,45 @@ /** | ||
*/ | ||
export async function getAddressFromPublicKey(publicKey) { | ||
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* () { | ||
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 = hash(Buffer.from(plainPublicKey, 'hex'), 20); | ||
return bs58check.encode(Buffer.concat([prefix, Buffer.from(payload)])); | ||
if (!prefix || !plainPublicKey) { | ||
throw new Error(`invalid publicKey: ${publicKey}`); | ||
} | ||
const payload = hash(Buffer.from(plainPublicKey, 'hex'), 20); | ||
return bs58check.encode(Buffer.concat([prefix, Buffer.from(payload)])); | ||
}); | ||
} | ||
@@ -166,8 +189,10 @@ /** | ||
*/ | ||
export async function prefixPublicKey(publicKey) { | ||
if (publicKey.length !== 64) { | ||
return publicKey; | ||
} | ||
const payload = Buffer.from(publicKey, 'hex'); | ||
return bs58check.encode(Buffer.concat([new Uint8Array([13, 15, 37, 217]), Buffer.from(payload)])); | ||
export function prefixPublicKey(publicKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (publicKey.length !== 64) { | ||
return publicKey; | ||
} | ||
const payload = Buffer.from(publicKey, 'hex'); | ||
return bs58check.encode(Buffer.concat([new Uint8Array([13, 15, 37, 217]), Buffer.from(payload)])); | ||
}); | ||
} | ||
@@ -183,3 +208,3 @@ /** | ||
} | ||
const toBuffer = async (message) => { | ||
const toBuffer = (message) => __awaiter(void 0, void 0, void 0, function* () { | ||
if (message.length % 2 !== 0) { | ||
@@ -197,15 +222,15 @@ return encode(message); | ||
return encode(message); | ||
}; | ||
const coinlibhash = async (message, size = 32) => { | ||
}); | ||
const coinlibhash = (message, size = 32) => __awaiter(void 0, void 0, void 0, function* () { | ||
return hash(message, size); | ||
}; | ||
export const signMessage = async (message, keypair) => { | ||
const bufferMessage = await toBuffer(message); | ||
}); | ||
export const signMessage = (message, keypair) => __awaiter(void 0, void 0, void 0, function* () { | ||
const bufferMessage = yield toBuffer(message); | ||
const edsigPrefix = new Uint8Array([9, 245, 205, 134, 18]); | ||
const hash = await coinlibhash(bufferMessage); | ||
const hash = yield coinlibhash(bufferMessage); | ||
const rawSignature = sign(keypair.secretKey, hash); | ||
const signature = bs58check.encode(Buffer.concat([Buffer.from(edsigPrefix), Buffer.from(rawSignature)])); | ||
return signature; | ||
}; | ||
}); | ||
/* eslint-enable prefer-arrow/prefer-arrow-functions */ | ||
//# sourceMappingURL=crypto.js.map |
@@ -6,4 +6,4 @@ export declare enum ExposedPromiseStatus { | ||
} | ||
type Resolve<T> = (value: T) => void; | ||
type Reject<U> = (reason?: U) => void; | ||
declare type Resolve<T> = (value: T) => void; | ||
declare type Reject<U> = (reason?: U) => void; | ||
/** | ||
@@ -10,0 +10,0 @@ * Exposed promise allow you to create a promise and then resolve it later, from the outside |
@@ -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(); |
@@ -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()); | ||
}); | ||
}; | ||
/* eslint-disable prefer-arrow/prefer-arrow-functions */ | ||
@@ -6,11 +15,13 @@ import { randomBytes } from '@stablelib/random'; | ||
*/ | ||
export async function generateGUID() { | ||
const buf = randomBytes(16); | ||
return [buf.slice(0, 4), buf.slice(4, 6), buf.slice(6, 8), buf.slice(8, 10), buf.slice(10, 16)] | ||
.map(function (subbuf) { | ||
return Buffer.from(subbuf).toString('hex'); | ||
}) | ||
.join('-'); | ||
export function generateGUID() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const buf = randomBytes(16); | ||
return [buf.slice(0, 4), buf.slice(4, 6), buf.slice(6, 8), buf.slice(8, 10), buf.slice(10, 16)] | ||
.map(function (subbuf) { | ||
return Buffer.from(subbuf).toString('hex'); | ||
}) | ||
.join('-'); | ||
}); | ||
} | ||
/* eslint-enable prefer-arrow/prefer-arrow-functions */ | ||
//# sourceMappingURL=generate-uuid.js.map |
{ | ||
"name": "@airgap/beacon-utils", | ||
"version": "4.0.11-icp-icrc25-beta.0", | ||
"version": "4.0.11", | ||
"description": "This package contains utility functions that are used throughout the beacon-sdk", | ||
@@ -38,3 +38,3 @@ "author": "Andreas Gassmann <a.gassmann@papers.ch>", | ||
"@stablelib/ed25519": "^1.0.3", | ||
"@stablelib/nacl": "^1.0.4", | ||
"@stablelib/nacl": "^1.0.3", | ||
"@stablelib/random": "^1.0.2", | ||
@@ -44,3 +44,3 @@ "@stablelib/utf8": "^1.0.1", | ||
}, | ||
"gitHead": "d85c387cfeb2c0b2cb365871ff0c4c9be4bc61e4" | ||
"gitHead": "d383e5b73d41e886e02e6d1bfe31f304ac49824f" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
66869
1099
0
Updated@stablelib/nacl@^1.0.3