mirror of
https://github.com/amark/gun.git
synced 2025-07-06 12:52:32 +00:00
Reset unnecessary prettier changes
This commit is contained in:
parent
7b583f3b41
commit
7d7b11f9b8
11
sea.js
11
sea.js
@ -64,11 +64,6 @@
|
|||||||
(_, i) => String.fromCharCode(this[ i + start])
|
(_, i) => String.fromCharCode(this[ i + start])
|
||||||
).join('')
|
).join('')
|
||||||
}
|
}
|
||||||
|
|
||||||
function btoa(b) {
|
|
||||||
return new Buffer(b).toString('base64');
|
|
||||||
};
|
|
||||||
|
|
||||||
if (enc === 'base64') {
|
if (enc === 'base64') {
|
||||||
return btoa(this)
|
return btoa(this)
|
||||||
}
|
}
|
||||||
@ -96,9 +91,6 @@
|
|||||||
}
|
}
|
||||||
const input = arguments[0]
|
const input = arguments[0]
|
||||||
let buf
|
let buf
|
||||||
function atob(a) {
|
|
||||||
return new Buffer(a, 'base64').toString('binary');
|
|
||||||
};
|
|
||||||
if (typeof input === 'string') {
|
if (typeof input === 'string') {
|
||||||
const enc = arguments[1] || 'utf8'
|
const enc = arguments[1] || 'utf8'
|
||||||
if (enc === 'hex') {
|
if (enc === 'hex') {
|
||||||
@ -182,7 +174,7 @@
|
|||||||
random: (len) => Buffer.from(crypto.randomBytes(len))
|
random: (len) => Buffer.from(crypto.randomBytes(len))
|
||||||
});
|
});
|
||||||
//try{
|
//try{
|
||||||
const { Crypto: WebCrypto } = USE('@peculiar/webcrypto', 1);
|
const WebCrypto = USE('node-webcrypto-ossl', 1);
|
||||||
api.ossl = api.subtle = new WebCrypto({directory: 'ossl'}).subtle // ECDH
|
api.ossl = api.subtle = new WebCrypto({directory: 'ossl'}).subtle // ECDH
|
||||||
//}catch(e){
|
//}catch(e){
|
||||||
//console.log("node-webcrypto-ossl is optionally needed for ECDH, please install if needed.");
|
//console.log("node-webcrypto-ossl is optionally needed for ECDH, please install if needed.");
|
||||||
@ -312,6 +304,7 @@
|
|||||||
|
|
||||||
//SEA.pair = async (data, proof, cb) => { try {
|
//SEA.pair = async (data, proof, cb) => { try {
|
||||||
SEA.pair = SEA.pair || (async (cb, opt) => { try {
|
SEA.pair = SEA.pair || (async (cb, opt) => { try {
|
||||||
|
|
||||||
var ecdhSubtle = shim.ossl || shim.subtle;
|
var ecdhSubtle = shim.ossl || shim.subtle;
|
||||||
// First: ECDSA keys for signing/verifying...
|
// First: ECDSA keys for signing/verifying...
|
||||||
var sa = await shim.subtle.generateKey(S.ecdsa.pair, true, [ 'sign', 'verify' ])
|
var sa = await shim.subtle.generateKey(S.ecdsa.pair, true, [ 'sign', 'verify' ])
|
||||||
|
47
sea/array.js
47
sea/array.js
@ -1,29 +1,24 @@
|
|||||||
// This is Array extended to have .toString(['utf8'|'hex'|'base64'])
|
|
||||||
function SeaArray() {}
|
|
||||||
Object.assign(SeaArray, { from: Array.from });
|
|
||||||
SeaArray.prototype = Object.create(Array.prototype);
|
|
||||||
SeaArray.prototype.toString = function(enc, start, end) {
|
|
||||||
enc = enc || "utf8";
|
|
||||||
start = start || 0;
|
|
||||||
const length = this.length;
|
|
||||||
if (enc === "hex") {
|
|
||||||
const buf = new Uint8Array(this);
|
|
||||||
return [...Array(((end && end + 1) || length) - start).keys()]
|
|
||||||
.map(i => buf[i + start].toString(16).padStart(2, "0"))
|
|
||||||
.join("");
|
|
||||||
}
|
|
||||||
if (enc === "utf8") {
|
|
||||||
return Array.from({ length: (end || length) - start }, (_, i) =>
|
|
||||||
String.fromCharCode(this[i + start])
|
|
||||||
).join("");
|
|
||||||
}
|
|
||||||
|
|
||||||
function btoa(b) {
|
// This is Array extended to have .toString(['utf8'|'hex'|'base64'])
|
||||||
return new Buffer(b).toString("base64");
|
function SeaArray() {}
|
||||||
|
Object.assign(SeaArray, { from: Array.from })
|
||||||
|
SeaArray.prototype = Object.create(Array.prototype)
|
||||||
|
SeaArray.prototype.toString = function(enc, start, end) { enc = enc || 'utf8'; start = start || 0;
|
||||||
|
const length = this.length
|
||||||
|
if (enc === 'hex') {
|
||||||
|
const buf = new Uint8Array(this)
|
||||||
|
return [ ...Array(((end && (end + 1)) || length) - start).keys()]
|
||||||
|
.map((i) => buf[ i + start ].toString(16).padStart(2, '0')).join('')
|
||||||
}
|
}
|
||||||
|
if (enc === 'utf8') {
|
||||||
|
return Array.from(
|
||||||
|
{ length: (end || length) - start },
|
||||||
|
(_, i) => String.fromCharCode(this[ i + start])
|
||||||
|
).join('')
|
||||||
|
}
|
||||||
|
if (enc === 'base64') {
|
||||||
|
return btoa(this)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
module.exports = SeaArray;
|
||||||
|
|
||||||
if (enc === "base64") {
|
|
||||||
return btoa(this);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
module.exports = SeaArray;
|
|
||||||
|
125
sea/buffer.js
125
sea/buffer.js
@ -1,95 +1,78 @@
|
|||||||
// This is Buffer implementation used in SEA. Functionality is mostly
|
|
||||||
// compatible with NodeJS 'safe-buffer' and is used for encoding conversions
|
// This is Buffer implementation used in SEA. Functionality is mostly
|
||||||
// between binary and 'hex' | 'utf8' | 'base64'
|
// compatible with NodeJS 'safe-buffer' and is used for encoding conversions
|
||||||
// See documentation and validation for safe implementation in:
|
// between binary and 'hex' | 'utf8' | 'base64'
|
||||||
// https://github.com/feross/safe-buffer#update
|
// See documentation and validation for safe implementation in:
|
||||||
var SeaArray = require("./array");
|
// https://github.com/feross/safe-buffer#update
|
||||||
function SafeBuffer(...props) {
|
var SeaArray = require('./array');
|
||||||
console.warn("new SafeBuffer() is depreciated, please use SafeBuffer.from()");
|
function SafeBuffer(...props) {
|
||||||
return SafeBuffer.from(...props);
|
console.warn('new SafeBuffer() is depreciated, please use SafeBuffer.from()')
|
||||||
}
|
return SafeBuffer.from(...props)
|
||||||
SafeBuffer.prototype = Object.create(Array.prototype);
|
}
|
||||||
Object.assign(SafeBuffer, {
|
SafeBuffer.prototype = Object.create(Array.prototype)
|
||||||
|
Object.assign(SafeBuffer, {
|
||||||
// (data, enc) where typeof data === 'string' then enc === 'utf8'|'hex'|'base64'
|
// (data, enc) where typeof data === 'string' then enc === 'utf8'|'hex'|'base64'
|
||||||
from() {
|
from() {
|
||||||
if (!Object.keys(arguments).length) {
|
if (!Object.keys(arguments).length) {
|
||||||
throw new TypeError(
|
throw new TypeError('First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object.')
|
||||||
"First argument must be a string, Buffer, ArrayBuffer, Array, or array-like object."
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
const input = arguments[0];
|
const input = arguments[0]
|
||||||
let buf;
|
let buf
|
||||||
function atob(a) {
|
if (typeof input === 'string') {
|
||||||
return new Buffer(a, "base64").toString("binary");
|
const enc = arguments[1] || 'utf8'
|
||||||
}
|
if (enc === 'hex') {
|
||||||
if (typeof input === "string") {
|
const bytes = input.match(/([\da-fA-F]{2})/g)
|
||||||
const enc = arguments[1] || "utf8";
|
.map((byte) => parseInt(byte, 16))
|
||||||
if (enc === "hex") {
|
|
||||||
const bytes = input
|
|
||||||
.match(/([\da-fA-F]{2})/g)
|
|
||||||
.map(byte => parseInt(byte, 16));
|
|
||||||
if (!bytes || !bytes.length) {
|
if (!bytes || !bytes.length) {
|
||||||
throw new TypeError("Invalid first argument for type 'hex'.");
|
throw new TypeError('Invalid first argument for type \'hex\'.')
|
||||||
}
|
}
|
||||||
buf = SeaArray.from(bytes);
|
buf = SeaArray.from(bytes)
|
||||||
} else if (enc === "utf8") {
|
} else if (enc === 'utf8') {
|
||||||
const length = input.length;
|
const length = input.length
|
||||||
const words = new Uint16Array(length);
|
const words = new Uint16Array(length)
|
||||||
Array.from(
|
Array.from({ length: length }, (_, i) => words[i] = input.charCodeAt(i))
|
||||||
{ length: length },
|
buf = SeaArray.from(words)
|
||||||
(_, i) => (words[i] = input.charCodeAt(i))
|
} else if (enc === 'base64') {
|
||||||
);
|
const dec = atob(input)
|
||||||
buf = SeaArray.from(words);
|
const length = dec.length
|
||||||
} else if (enc === "base64") {
|
const bytes = new Uint8Array(length)
|
||||||
const dec = atob(input);
|
Array.from({ length: length }, (_, i) => bytes[i] = dec.charCodeAt(i))
|
||||||
const length = dec.length;
|
buf = SeaArray.from(bytes)
|
||||||
const bytes = new Uint8Array(length);
|
} else if (enc === 'binary') {
|
||||||
Array.from(
|
buf = SeaArray.from(input)
|
||||||
{ length: length },
|
|
||||||
(_, i) => (bytes[i] = dec.charCodeAt(i))
|
|
||||||
);
|
|
||||||
buf = SeaArray.from(bytes);
|
|
||||||
} else if (enc === "binary") {
|
|
||||||
buf = SeaArray.from(input);
|
|
||||||
} else {
|
} else {
|
||||||
console.info("SafeBuffer.from unknown encoding: " + enc);
|
console.info('SafeBuffer.from unknown encoding: '+enc)
|
||||||
}
|
}
|
||||||
return buf;
|
return buf
|
||||||
}
|
}
|
||||||
const byteLength = input.byteLength; // what is going on here? FOR MARTTI
|
const byteLength = input.byteLength // what is going on here? FOR MARTTI
|
||||||
const length = input.byteLength ? input.byteLength : input.length;
|
const length = input.byteLength ? input.byteLength : input.length
|
||||||
if (length) {
|
if (length) {
|
||||||
let buf;
|
let buf
|
||||||
if (input instanceof ArrayBuffer) {
|
if (input instanceof ArrayBuffer) {
|
||||||
buf = new Uint8Array(input);
|
buf = new Uint8Array(input)
|
||||||
}
|
}
|
||||||
return SeaArray.from(buf || input);
|
return SeaArray.from(buf || input)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// This is 'safe-buffer.alloc' sans encoding support
|
// This is 'safe-buffer.alloc' sans encoding support
|
||||||
alloc(length, fill = 0 /*, enc*/) {
|
alloc(length, fill = 0 /*, enc*/ ) {
|
||||||
return SeaArray.from(
|
return SeaArray.from(new Uint8Array(Array.from({ length: length }, () => fill)))
|
||||||
new Uint8Array(Array.from({ length: length }, () => fill))
|
|
||||||
);
|
|
||||||
},
|
},
|
||||||
// This is normal UNSAFE 'buffer.alloc' or 'new Buffer(length)' - don't use!
|
// This is normal UNSAFE 'buffer.alloc' or 'new Buffer(length)' - don't use!
|
||||||
allocUnsafe(length) {
|
allocUnsafe(length) {
|
||||||
return SeaArray.from(new Uint8Array(Array.from({ length: length })));
|
return SeaArray.from(new Uint8Array(Array.from({ length : length })))
|
||||||
},
|
},
|
||||||
// This puts together array of array like members
|
// This puts together array of array like members
|
||||||
concat(arr) {
|
concat(arr) { // octet array
|
||||||
// octet array
|
|
||||||
if (!Array.isArray(arr)) {
|
if (!Array.isArray(arr)) {
|
||||||
throw new TypeError(
|
throw new TypeError('First argument must be Array containing ArrayBuffer or Uint8Array instances.')
|
||||||
"First argument must be Array containing ArrayBuffer or Uint8Array instances."
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
return SeaArray.from(
|
return SeaArray.from(arr.reduce((ret, item) => ret.concat(Array.from(item)), []))
|
||||||
arr.reduce((ret, item) => ret.concat(Array.from(item)), [])
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
});
|
})
|
||||||
SafeBuffer.prototype.from = SafeBuffer.from;
|
SafeBuffer.prototype.from = SafeBuffer.from
|
||||||
SafeBuffer.prototype.toString = SeaArray.prototype.toString;
|
SafeBuffer.prototype.toString = SeaArray.prototype.toString
|
||||||
|
|
||||||
|
module.exports = SafeBuffer;
|
||||||
|
|
||||||
module.exports = SafeBuffer;
|
|
||||||
|
43
sea/shim.js
43
sea/shim.js
@ -1,39 +1,36 @@
|
|||||||
const SEA = require("./root");
|
|
||||||
const Buffer = require("./buffer");
|
|
||||||
const api = { Buffer: Buffer };
|
|
||||||
var o = {};
|
|
||||||
|
|
||||||
if (SEA.window) {
|
const SEA = require('./root')
|
||||||
|
const Buffer = require('./buffer')
|
||||||
|
const api = {Buffer: Buffer}
|
||||||
|
var o = {};
|
||||||
|
|
||||||
|
if(SEA.window){
|
||||||
api.crypto = window.crypto || window.msCrypto;
|
api.crypto = window.crypto || window.msCrypto;
|
||||||
api.subtle = (api.crypto || o).subtle || (api.crypto || o).webkitSubtle;
|
api.subtle = (api.crypto||o).subtle || (api.crypto||o).webkitSubtle;
|
||||||
api.TextEncoder = window.TextEncoder;
|
api.TextEncoder = window.TextEncoder;
|
||||||
api.TextDecoder = window.TextDecoder;
|
api.TextDecoder = window.TextDecoder;
|
||||||
api.random = len =>
|
api.random = (len) => Buffer.from(api.crypto.getRandomValues(new Uint8Array(Buffer.alloc(len))))
|
||||||
Buffer.from(api.crypto.getRandomValues(new Uint8Array(Buffer.alloc(len))));
|
}
|
||||||
}
|
if(!api.crypto){try{
|
||||||
if (!api.crypto) {
|
var crypto = require('crypto', 1);
|
||||||
try {
|
const { TextEncoder, TextDecoder } = require('text-encoding', 1)
|
||||||
var crypto = require("crypto", 1);
|
|
||||||
const { TextEncoder, TextDecoder } = require("text-encoding", 1);
|
|
||||||
Object.assign(api, {
|
Object.assign(api, {
|
||||||
crypto,
|
crypto,
|
||||||
//subtle,
|
//subtle,
|
||||||
TextEncoder,
|
TextEncoder,
|
||||||
TextDecoder,
|
TextDecoder,
|
||||||
random: len => Buffer.from(crypto.randomBytes(len))
|
random: (len) => Buffer.from(crypto.randomBytes(len))
|
||||||
});
|
});
|
||||||
//try{
|
//try{
|
||||||
const { Crypto: WebCrypto } = require("@peculiar/webcrypto", 1);
|
const WebCrypto = require('node-webcrypto-ossl', 1);
|
||||||
api.ossl = api.subtle = new WebCrypto({ directory: "ossl" }).subtle; // ECDH
|
api.ossl = api.subtle = new WebCrypto({directory: 'ossl'}).subtle // ECDH
|
||||||
//}catch(e){
|
//}catch(e){
|
||||||
//console.log("node-webcrypto-ossl is optionally needed for ECDH, please install if needed.");
|
//console.log("node-webcrypto-ossl is optionally needed for ECDH, please install if needed.");
|
||||||
//}
|
//}
|
||||||
} catch (e) {
|
}catch(e){
|
||||||
console.log(
|
console.log("node-webcrypto-ossl and text-encoding may not be included by default, please add it to your package.json!");
|
||||||
"node-webcrypto-ossl and text-encoding may not be included by default, please add it to your package.json!"
|
|
||||||
);
|
|
||||||
OSSL_WEBCRYPTO_OR_TEXT_ENCODING_NOT_INSTALLED;
|
OSSL_WEBCRYPTO_OR_TEXT_ENCODING_NOT_INSTALLED;
|
||||||
}
|
}}
|
||||||
}
|
|
||||||
|
module.exports = api
|
||||||
|
|
||||||
module.exports = api;
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user