@peerbit/crypto
Advanced tools
Comparing version 2.2.0 to 2.3.0-2b760c3
131
package.json
{ | ||
"name": "@peerbit/crypto", | ||
"version": "2.2.0", | ||
"description": "Crypto fn", | ||
"type": "module", | ||
"sideEffects": false, | ||
"module": "lib/esm/index.js", | ||
"types": "lib/esm/index.d.ts", | ||
"exports": { | ||
"import": "./lib/esm/index.js" | ||
}, | ||
"files": [ | ||
"lib", | ||
"src", | ||
"!src/**/__tests__", | ||
"!lib/**/__tests__", | ||
"!src/**/__benchmark__", | ||
"!lib/**/__benchmark__", | ||
"LICENSE" | ||
], | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"browser": { | ||
"./lib/esm/hash.js": "./lib/esm/hash-browser.js", | ||
"./hash.js": "./lib/esm/hash-browser.js", | ||
"./lib/esm/random.js": "./lib/esm/random-browser.js", | ||
"./random.js": "./lib/esm/random-browser.js", | ||
"./lib/esm/ed25519-sign.js": "./lib/esm/ed25519-sign-browser.js", | ||
"./ed25519-sign.js": "./lib/esm/ed25519-sign-browser.js" | ||
}, | ||
"scripts": { | ||
"clean": "shx rm -rf lib/*", | ||
"build": "yarn clean && tsc -p tsconfig.json", | ||
"test": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.ts --runInBand --forceExit", | ||
"test:unit": "node ../../../node_modules/.bin/jest test -c ../../../jest.config.unit.ts --runInBand --forceExit", | ||
"test:integration": "node ../node_modules/.bin/jest test -c ../../../jest.config.integration.ts --runInBand --forceExit" | ||
}, | ||
"author": "dao.xyz", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@types/libsodium-wrappers": "^0.7.10", | ||
"uint8arrays": "^4.0.4" | ||
}, | ||
"dependencies": { | ||
"@dao-xyz/borsh": "^5.2.1", | ||
"@ethersproject/wallet": "^5.7.0", | ||
"@libp2p/crypto": "^3.0.3", | ||
"@libp2p/peer-id": "^4.0.3", | ||
"@peerbit/cache": "^2.0.6", | ||
"@peerbit/uint8arrays": "3.0.1", | ||
"@stablelib/sha256": "^1.0.1", | ||
"libsodium-wrappers": "^0.7.13" | ||
}, | ||
"gitHead": "fcd486b9a5c3b7d94e2ae14f1b06ab2a0f7841ad" | ||
"name": "@peerbit/crypto", | ||
"version": "2.3.0-2b760c3", | ||
"description": "Crypto fn", | ||
"sideEffects": false, | ||
"type": "module", | ||
"types": "./dist/src/index.d.ts", | ||
"typesVersions": { | ||
"*": { | ||
"*": [ | ||
"*", | ||
"dist/*", | ||
"dist/src/*", | ||
"dist/src/*/index" | ||
], | ||
"src/*": [ | ||
"*", | ||
"dist/*", | ||
"dist/src/*", | ||
"dist/src/*/index" | ||
] | ||
} | ||
}, | ||
"files": [ | ||
"src", | ||
"dist", | ||
"!dist/test", | ||
"!**/*.tsbuildinfo" | ||
], | ||
"exports": { | ||
".": { | ||
"types": "./dist/src/index.d.ts", | ||
"import": "./dist/src/index.js" | ||
} | ||
}, | ||
"eslintConfig": { | ||
"extends": "peerbit", | ||
"parserOptions": { | ||
"project": true, | ||
"sourceType": "module" | ||
}, | ||
"ignorePatterns": [ | ||
"!.aegir.js", | ||
"test/ts-use", | ||
"*.d.ts" | ||
] | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"browser": { | ||
"./dist/src/hash.js": "./dist/src/hash.browser.js", | ||
"./dist/src/random.js": "./dist/src/random.browser.js", | ||
"./dist/src/ed25519-sign.js": "./dist/src/ed25519-sign.browser.js" | ||
}, | ||
"scripts": { | ||
"clean": "aegir clean", | ||
"build": "aegir build --no-bundle", | ||
"test": "aegir test", | ||
"lint": "aegir lint" | ||
}, | ||
"author": "dao.xyz", | ||
"license": "MIT", | ||
"devDependencies": { | ||
"@types/libsodium-wrappers": "^0.7.10", | ||
"uint8arrays": "^4.0.4", | ||
"@types/elliptic": "^6.4.18", | ||
"multiformats": "^13.1.0" | ||
}, | ||
"dependencies": { | ||
"@peerbit/cache": "2.1.0-2b760c3", | ||
"@dao-xyz/borsh": "^5.2.3", | ||
"@stablelib/sha256": "^2.0.0", | ||
"@ethersproject/wallet": "^5.7.0", | ||
"@libp2p/crypto": "^4.1.5", | ||
"@libp2p/peer-id": "^4.2.0", | ||
"libsodium-wrappers": "^0.7.13" | ||
} | ||
} |
@@ -5,3 +5,3 @@ /** | ||
*/ | ||
export const coerce = (o) => { | ||
export const coerce = (o: any) => { | ||
if (o instanceof Uint8Array && o.constructor.name === "Uint8Array") return o; | ||
@@ -8,0 +8,0 @@ if (o instanceof ArrayBuffer) return new Uint8Array(o); |
@@ -1,6 +0,6 @@ | ||
import { Ed25519Keypair, Ed25519PublicKey } from "./ed25519.js"; | ||
import crypto from "crypto"; | ||
import sodium from "libsodium-wrappers"; | ||
import crypto from "crypto"; | ||
import { type Ed25519Keypair, type Ed25519PublicKey } from "./ed25519.js"; | ||
import { type PreHash, prehashFn } from "./prehash.js"; | ||
import { SignatureWithKey } from "./signature.js"; | ||
import { PreHash, prehashFn } from "./prehash.js"; | ||
@@ -10,3 +10,3 @@ export const sign = async ( | ||
keypair: Ed25519Keypair, | ||
prehash: PreHash | ||
prehash: PreHash, | ||
) => { | ||
@@ -19,3 +19,3 @@ const hashedData = await prehashFn(data, prehash); | ||
type: "pkcs8", | ||
key: toDER(keypair.privateKeyPublicKey, true) | ||
key: toDER(keypair.privateKeyPublicKey, true), | ||
}); | ||
@@ -26,3 +26,3 @@ } | ||
publicKey: keypair.publicKey, | ||
signature: crypto.sign(null, hashedData, keypair.privateKey.keyObject) | ||
signature: crypto.sign(null, hashedData, keypair.privateKey.keyObject), | ||
}); | ||
@@ -33,3 +33,3 @@ }; | ||
signature: SignatureWithKey, | ||
data: Uint8Array | ||
data: Uint8Array, | ||
) => { | ||
@@ -44,3 +44,3 @@ let res = false; | ||
hashedData, | ||
(signature.publicKey as Ed25519PublicKey).publicKey | ||
(signature.publicKey as Ed25519PublicKey).publicKey, | ||
); | ||
@@ -55,3 +55,3 @@ res = verified; | ||
const DER_PREFIX = Buffer.from([ | ||
48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32 | ||
48, 46, 2, 1, 0, 48, 5, 6, 3, 43, 101, 112, 4, 34, 4, 32, | ||
]); | ||
@@ -78,3 +78,3 @@ const ED25519_OID = Buffer.from([0x06, 0x03, 0x2b, 0x65, 0x70]); | ||
ZERO_BIT_TAG, | ||
key | ||
key, | ||
]); | ||
@@ -86,3 +86,3 @@ | ||
Buffer.from([elements.length]), | ||
elements | ||
elements, | ||
]); | ||
@@ -89,0 +89,0 @@ |
import { field, fixedArray, variant } from "@dao-xyz/borsh"; | ||
import { PrivateSignKey, PublicSignKey, Keypair } from "./key.js"; | ||
import { equals } from "@peerbit/uint8arrays"; | ||
import { Identity, Signer, SignWithKey } from "./signer.js"; | ||
import { SignatureWithKey } from "./signature.js"; | ||
import { toHexString } from "./utils.js"; | ||
import { supportedKeys } from "@libp2p/crypto/keys"; | ||
import type { Ed25519PeerId, PeerId } from "@libp2p/interface"; | ||
import { peerIdFromKeys } from "@libp2p/peer-id"; | ||
import { supportedKeys } from "@libp2p/crypto/keys"; | ||
import sodium from "libsodium-wrappers"; | ||
import { concat, equals } from "uint8arrays"; | ||
import { coerce } from "./bytes.js"; | ||
import sodium from "libsodium-wrappers"; | ||
import type { Ed25519PeerId, PeerId } from "@libp2p/interface"; | ||
import { sign } from "./ed25519-sign.js"; | ||
import { Keypair, PrivateSignKey, PublicSignKey } from "./key.js"; | ||
import { PreHash } from "./prehash.js"; | ||
import { concat } from "uint8arrays"; | ||
import { SignatureWithKey } from "./signature.js"; | ||
import { type Identity, type SignWithKey } from "./signer.js"; | ||
import { toHexString } from "./utils.js"; | ||
@@ -35,2 +34,3 @@ @variant(0) | ||
} | ||
toString(): string { | ||
@@ -42,3 +42,3 @@ return "ed25119p/" + toHexString(this.publicKey); | ||
return peerIdFromKeys( | ||
new supportedKeys["ed25519"].Ed25519PublicKey(this.publicKey).bytes | ||
new supportedKeys["ed25519"].Ed25519PublicKey(this.publicKey).bytes, | ||
); | ||
@@ -65,3 +65,3 @@ } | ||
return new Ed25519PublicKey({ | ||
publicKey: coerce(id.publicKey!.slice(4)) | ||
publicKey: coerce(id.publicKey.slice(4)), | ||
}); | ||
@@ -107,3 +107,3 @@ } | ||
return new Ed25519PrivateKey({ | ||
privateKey: coerce(id.privateKey!.slice(4, 36)) | ||
privateKey: coerce(id.privateKey.slice(4, 36)), | ||
}); | ||
@@ -137,7 +137,7 @@ } | ||
publicKey: new Ed25519PublicKey({ | ||
publicKey: generated.publicKey | ||
publicKey: generated.publicKey, | ||
}), | ||
privateKey: new Ed25519PrivateKey({ | ||
privateKey: generated.privateKey.slice(0, 32) // Only the private key part (?) | ||
}) | ||
privateKey: generated.privateKey.slice(0, 32), // Only the private key part (?) | ||
}), | ||
}); | ||
@@ -150,3 +150,3 @@ | ||
data: Uint8Array, | ||
prehash: PreHash = PreHash.NONE | ||
prehash: PreHash = PreHash.NONE, | ||
): Promise<SignatureWithKey> { | ||
@@ -175,7 +175,7 @@ return sign(data, this, prehash); | ||
privateKey: Ed25519PrivateKey.fromPeerID(peerId), | ||
publicKey: Ed25519PublicKey.fromPeerId(peerId) | ||
publicKey: Ed25519PublicKey.fromPeerId(peerId), | ||
}); | ||
} | ||
_privateKeyPublicKey: Uint8Array; // length 64 | ||
_privateKeyPublicKey!: Uint8Array; // length 64 | ||
get privateKeyPublicKey(): Uint8Array { | ||
@@ -186,3 +186,3 @@ return ( | ||
this.privateKey.privateKey, | ||
this.publicKey.publicKey | ||
this.publicKey.publicKey, | ||
])) | ||
@@ -198,6 +198,6 @@ ); | ||
this.privateKeyPublicKey, | ||
this.publicKey.publicKey | ||
).bytes | ||
this.publicKey.publicKey, | ||
).bytes, | ||
); | ||
} | ||
} |
@@ -1,19 +0,28 @@ | ||
export * from "./errors.js"; | ||
import { | ||
AbstractType, | ||
type AbstractType, | ||
deserialize, | ||
field, | ||
fixedArray, | ||
serialize, | ||
variant, | ||
vec, | ||
fixedArray | ||
} from "@dao-xyz/borsh"; | ||
import { equals } from "@peerbit/uint8arrays"; | ||
import { AccessError } from "./errors.js"; | ||
import sodium from "libsodium-wrappers"; | ||
import { X25519Keypair, X25519PublicKey, X25519SecretKey } from "./x25519.js"; | ||
import { equals as uequals } from "uint8arrays"; | ||
import { Ed25519PublicKey } from "./ed25519.js"; | ||
import { AccessError } from "./errors.js"; | ||
import { sha256 } from "./hash.js"; | ||
import { randomBytes } from "./random.js"; | ||
import { sha256 } from "./hash.js"; | ||
import { X25519Keypair, X25519PublicKey, X25519SecretKey } from "./x25519.js"; | ||
export * from "./errors.js"; | ||
export const equals = (array1?: Uint8Array, array2?: Uint8Array) => { | ||
if (!!array1 !== !!array2) return false; | ||
if (!array1 || !array2) { | ||
return false; | ||
} | ||
return uequals(array1, array2); | ||
}; | ||
export type MaybePromise<T> = Promise<T> | T; | ||
@@ -41,3 +50,3 @@ | ||
T, | ||
Parameters extends KeyExchangeOptions | ||
Parameters extends KeyExchangeOptions, | ||
> = EncryptedThing<T, EnvelopeFromParameter<Parameters>>; | ||
@@ -55,3 +64,3 @@ | ||
function isAsymmetriEncryptionParameters( | ||
parameters: KeyExchangeOptions | ||
parameters: KeyExchangeOptions, | ||
): parameters is PublicKeyEncryptionParameters { | ||
@@ -63,3 +72,3 @@ return ( | ||
function isAsymmetricEncryptionKeys( | ||
parameters: PublicKeyEncryptionKeys | SymmetricKeys | ||
parameters: PublicKeyEncryptionKeys | SymmetricKeys, | ||
): parameters is PublicKeyEncryptionKeys { | ||
@@ -76,3 +85,3 @@ return (parameters as PublicKeyEncryptionKeys) instanceof X25519Keypair; | ||
bytes: Uint8Array, | ||
parameters: Parameters | ||
parameters: Parameters, | ||
) => Promise<CipherWithEnvelope<EnvelopeFromParameter<Parameters>>>; | ||
@@ -88,9 +97,9 @@ | ||
? PublicKeyEncryptionParameters | ||
: SymmetricKeyEncryptionParameters | ||
: SymmetricKeyEncryptionParameters, | ||
>( | ||
keys: K | ||
keys: K, | ||
) => { | ||
return async ( | ||
bytes: Uint8Array, | ||
parameters: Parameters | ||
parameters: Parameters, | ||
): Promise<CipherWithEnvelope<EnvelopeFromParameter<Parameters>>> => { | ||
@@ -111,3 +120,3 @@ const nonce = randomBytes(NONCE_LENGTH); // crypto random is faster than sodim random | ||
return key; | ||
}) | ||
}), | ||
); | ||
@@ -123,7 +132,7 @@ | ||
receiverPublicKey.publicKey, | ||
keys.secretKey.secretKey | ||
keys.secretKey.secretKey, | ||
), | ||
nonce: kNonce | ||
nonce: kNonce, | ||
}), | ||
receiverPublicKey | ||
receiverPublicKey, | ||
}); | ||
@@ -137,4 +146,4 @@ }); | ||
senderPublicKey: keys.publicKey, | ||
ks | ||
}) as EnvelopeFromParameter<Parameters> | ||
ks, | ||
}) as EnvelopeFromParameter<Parameters>, | ||
}; | ||
@@ -150,4 +159,4 @@ } else if ( | ||
envelope: new HashedKeyEnvelope({ | ||
hash: await sha256(keys) | ||
}) as EnvelopeFromParameter<Parameters> | ||
hash: await sha256(keys), | ||
}) as EnvelopeFromParameter<Parameters>, | ||
}; | ||
@@ -163,7 +172,7 @@ } | ||
nonce: Uint8Array, | ||
exchange: Envelope | ||
exchange: Envelope, | ||
) => Promise<Uint8Array>; | ||
type KeyResolver = <T extends X25519PublicKey | Uint8Array>( | ||
key: T | ||
key: T, | ||
) => Promise< | ||
@@ -174,3 +183,3 @@ (T extends X25519PublicKey ? X25519Keypair : Uint8Array) | undefined | ||
export const createDecrypterFromKeyResolver = ( | ||
keyResolver: KeyResolver | ||
keyResolver: KeyResolver, | ||
): DecryptProvider => { | ||
@@ -180,3 +189,3 @@ return async ( | ||
nonce: Uint8Array, | ||
exchange: Envelope | ||
exchange: Envelope, | ||
): Promise<Uint8Array> => { | ||
@@ -194,3 +203,3 @@ // We only need to open with one of the keys | ||
index: i, | ||
keypair: exported | ||
keypair: exported, | ||
}; | ||
@@ -214,3 +223,3 @@ break; | ||
exchange._senderPublicKey.publicKey, | ||
secretKey.secretKey | ||
secretKey.secretKey, | ||
); | ||
@@ -247,3 +256,3 @@ } catch (error) { | ||
decrypt( | ||
keyResolver?: X25519Keypair | KeyProvider | ||
keyResolver?: X25519Keypair | KeyProvider, | ||
): MaybePromise<DecryptedThing<T>>; | ||
@@ -253,6 +262,7 @@ decrypt(provider?: DecryptProvider): MaybePromise<DecryptedThing<T>>; | ||
decrypt( | ||
provider?: X25519Keypair | DecryptProvider | KeyProvider | ||
provider?: X25519Keypair | DecryptProvider | KeyProvider, | ||
): MaybePromise<DecryptedThing<T>> | DecryptedThing<T> { | ||
throw new Error("Not implemented"); | ||
} | ||
equals(other: MaybeEncrypted<T>): boolean { | ||
@@ -298,3 +308,3 @@ throw new Error("Not implemented"); | ||
provider: EncryptProvide<Parameters>, | ||
parameters: Parameters | ||
parameters: Parameters, | ||
): Promise<EncryptReturnValue<T, Parameters>>; | ||
@@ -304,3 +314,3 @@ | ||
x25519Keypair: X25519Keypair, | ||
receiverPublicKeys: (X25519PublicKey | Ed25519PublicKey)[] | ||
receiverPublicKeys: (X25519PublicKey | Ed25519PublicKey)[], | ||
): Promise<EncryptReturnValue<T, PublicKeyEncryptionParameters>>; | ||
@@ -310,3 +320,3 @@ | ||
keypair: EncryptProvide<any> | X25519Keypair, | ||
parameters: KeyExchangeOptions | (X25519PublicKey | Ed25519PublicKey)[] | ||
parameters: KeyExchangeOptions | (X25519PublicKey | Ed25519PublicKey)[], | ||
): Promise<EncryptReturnValue<T, any>> { | ||
@@ -322,3 +332,3 @@ let provider: EncryptProvide<any>; | ||
)[], | ||
type: "publicKey" | ||
type: "publicKey", | ||
}; | ||
@@ -335,3 +345,3 @@ } else { | ||
envelope, | ||
nonce | ||
nonce, | ||
}); | ||
@@ -342,7 +352,7 @@ enc._decrypted = this; | ||
get decrypted(): DecryptedThing<T> { | ||
get decrypted(): this { | ||
return this; | ||
} | ||
decrypt(): DecryptedThing<T> { | ||
decrypt(): this { | ||
return this; | ||
@@ -376,7 +386,5 @@ } | ||
constructor(props?: { nonce: Uint8Array; cipher: Uint8Array }) { | ||
if (props) { | ||
this.nonce = props.nonce; | ||
this.cipher = props.cipher; | ||
} | ||
constructor(props: { nonce: Uint8Array; cipher: Uint8Array }) { | ||
this.nonce = props.nonce; | ||
this.cipher = props.cipher; | ||
} | ||
@@ -403,10 +411,8 @@ | ||
constructor(props?: { | ||
constructor(props: { | ||
encryptedKey: CipherWithNonce; | ||
receiverPublicKey: X25519PublicKey; | ||
}) { | ||
if (props) { | ||
this._encryptedKey = props.encryptedKey; | ||
this._receiverPublicKey = props.receiverPublicKey; | ||
} | ||
this._encryptedKey = props.encryptedKey; | ||
this._receiverPublicKey = props.receiverPublicKey; | ||
} | ||
@@ -438,8 +444,6 @@ | ||
constructor(props?: { senderPublicKey: X25519PublicKey; ks: K[] }) { | ||
constructor(props: { senderPublicKey: X25519PublicKey; ks: K[] }) { | ||
super(); | ||
if (props) { | ||
this._senderPublicKey = props.senderPublicKey; | ||
this._ks = props.ks; | ||
} | ||
this._senderPublicKey = props.senderPublicKey; | ||
this._ks = props.ks; | ||
} | ||
@@ -473,8 +477,7 @@ | ||
hash: Uint8Array; | ||
// TODO: Do we need a salt here? | ||
constructor(props?: { hash: Uint8Array }) { | ||
constructor(props: { hash: Uint8Array }) { | ||
super(); | ||
if (props) { | ||
this.hash = props.hash; | ||
} | ||
this.hash = props.hash; | ||
} | ||
@@ -498,3 +501,3 @@ | ||
T, | ||
E extends Envelope = PublicKeyEnvelope | HashedKeyEnvelope | ||
E extends Envelope = PublicKeyEnvelope | HashedKeyEnvelope, | ||
> extends MaybeEncrypted<T> { | ||
@@ -510,3 +513,3 @@ @field({ type: Uint8Array }) | ||
constructor(props?: { | ||
constructor(props: { | ||
encrypted: Uint8Array; | ||
@@ -517,7 +520,5 @@ nonce: Uint8Array; | ||
super(); | ||
if (props) { | ||
this._encrypted = props.encrypted; | ||
this._nonce = props.nonce; | ||
this._keyexchange = props.envelope; | ||
} | ||
this._encrypted = props.encrypted; | ||
this._nonce = props.nonce; | ||
this._keyexchange = props.envelope; | ||
} | ||
@@ -529,3 +530,3 @@ | ||
throw new Error( | ||
"Entry has not been decrypted, invoke decrypt method before" | ||
"Entry has not been decrypted, invoke decrypt method before", | ||
); | ||
@@ -537,3 +538,3 @@ } | ||
async decrypt( | ||
keyResolver?: X25519Keypair | KeyProvider | ||
keyResolver?: X25519Keypair | KeyProvider, | ||
): Promise<DecryptedThing<T>>; | ||
@@ -543,3 +544,3 @@ async decrypt(provider?: DecryptProvider): Promise<DecryptedThing<T>>; | ||
async decrypt( | ||
providerOrResolver?: X25519Keypair | DecryptProvider | KeyProvider | ||
providerOrResolver?: X25519Keypair | DecryptProvider | KeyProvider, | ||
): Promise<DecryptedThing<T>> { | ||
@@ -579,3 +580,3 @@ let provider: DecryptProvider | undefined; | ||
this._nonce, | ||
this._keyexchange | ||
this._keyexchange, | ||
); | ||
@@ -582,0 +583,0 @@ if (decrypted) { |
@@ -1,5 +0,1 @@ | ||
export class AccessError extends Error { | ||
constructor(message?: string) { | ||
super(message); | ||
} | ||
} | ||
export class AccessError extends Error {} |
@@ -0,1 +1,3 @@ | ||
import libsodium from "libsodium-wrappers"; | ||
export * from "./key.js"; | ||
@@ -13,4 +15,4 @@ export * from "./ed25519.js"; | ||
export * from "./signer.js"; | ||
import libsodium from "libsodium-wrappers"; | ||
const ready = libsodium.ready; | ||
export { ready }; |
@@ -1,5 +0,5 @@ | ||
import { BinaryWriter, field, serialize, serializer } from "@dao-xyz/borsh"; | ||
import { field, serialize } from "@dao-xyz/borsh"; | ||
import { type PeerId } from "@libp2p/interface"; | ||
import { compare } from "uint8arrays"; | ||
import { sha256Base64Sync } from "./hash.js"; | ||
import { PeerId } from "@libp2p/interface"; | ||
import { compare } from "@peerbit/uint8arrays"; | ||
import { toHexString } from "./utils.js"; | ||
@@ -32,5 +32,6 @@ | ||
export abstract class PublicSignKey implements Key { | ||
private _bytes: Uint8Array; | ||
private _bytes!: Uint8Array; | ||
get bytes(): Uint8Array { | ||
return this._bytes || this._bytes || serialize(this); | ||
return this._bytes || (this._bytes = serialize(this)); | ||
} | ||
@@ -37,0 +38,0 @@ |
@@ -1,2 +0,2 @@ | ||
import { PeerId } from "@libp2p/interface"; | ||
import { type PeerId } from "@libp2p/interface"; | ||
import { Ed25519Keypair, Ed25519PublicKey } from "./ed25519.js"; | ||
@@ -6,3 +6,3 @@ import { Secp256k1Keypair, Secp256k1PublicKey } from "./sepc256k1.js"; | ||
export const getKeypairFromPeerId = ( | ||
peerId: PeerId | ||
peerId: PeerId, | ||
): Ed25519Keypair | Secp256k1Keypair => { | ||
@@ -19,3 +19,3 @@ if (peerId.type === "Ed25519") { | ||
export const getPublicKeyFromPeerId = ( | ||
peerId: PeerId | ||
peerId: PeerId, | ||
): Ed25519PublicKey | Secp256k1PublicKey => { | ||
@@ -22,0 +22,0 @@ if (peerId.type === "Ed25519") { |
@@ -1,5 +0,5 @@ | ||
import { sha256 } from "./hash.js"; | ||
import { toUtf8Bytes } from "@ethersproject/strings"; | ||
import sha3 from "js-sha3"; | ||
import { toUtf8Bytes } from "@ethersproject/strings"; | ||
import { concat } from "uint8arrays"; | ||
import { sha256 } from "./hash.js"; | ||
@@ -14,6 +14,6 @@ const messagePrefix = "\x19Ethereum Signed Message:\n"; | ||
toUtf8Bytes(String(message.length)), | ||
message | ||
]) | ||
message, | ||
]), | ||
) | ||
.arrayBuffer() | ||
.arrayBuffer(), | ||
); | ||
@@ -24,4 +24,4 @@ | ||
SHA_256 = 1, | ||
//BLAKE3 = 2, | ||
ETH_KECCAK_256 = 3 | ||
// BLAKE3 = 2, | ||
ETH_KECCAK_256 = 3, | ||
} | ||
@@ -31,3 +31,3 @@ | ||
data: Uint8Array, | ||
prehash: PreHash | ||
prehash: PreHash, | ||
): Promise<Uint8Array> | Uint8Array => { | ||
@@ -34,0 +34,0 @@ if (prehash === PreHash.NONE) { |
import crypto from "crypto"; | ||
export const randomBytes = (len: number) => crypto.randomBytes(len); |
@@ -1,23 +0,25 @@ | ||
import { field, fixedArray, variant, vec } from "@dao-xyz/borsh"; | ||
import { field, fixedArray, variant } from "@dao-xyz/borsh"; | ||
import { | ||
type SignatureLike, | ||
arrayify, | ||
joinSignature, | ||
splitSignature, | ||
} from "@ethersproject/bytes"; | ||
import { Wallet } from "@ethersproject/wallet"; | ||
import { generateKeyPair, supportedKeys } from "@libp2p/crypto/keys"; | ||
import { type PeerId } from "@libp2p/interface"; | ||
import { peerIdFromKeys } from "@libp2p/peer-id"; | ||
import utf8 from "@protobufjs/utf8"; | ||
import _ec from "elliptic"; | ||
import { equals } from "uint8arrays"; | ||
import { coerce } from "./bytes.js"; | ||
import { Keypair, PrivateSignKey, PublicSignKey } from "./key.js"; | ||
import { PreHash, prehashFn } from "./prehash.js"; | ||
import { SignatureWithKey } from "./signature.js"; | ||
import { type Identity } from "./signer.js"; | ||
import { toHexString } from "./utils.js"; | ||
import { Wallet } from "@ethersproject/wallet"; | ||
import { arrayify } from "@ethersproject/bytes"; | ||
import { joinSignature } from "@ethersproject/bytes"; | ||
import { SignatureLike, splitSignature } from "@ethersproject/bytes"; | ||
import _ec from "elliptic"; | ||
import EC = _ec.ec; | ||
let _curve: EC; | ||
import { equals } from "@peerbit/uint8arrays"; | ||
import { toHexString } from "./utils.js"; | ||
import { PeerId } from "@libp2p/interface"; | ||
import { Identity } from "./signer.js"; | ||
import { coerce } from "./bytes.js"; | ||
import { generateKeyPair, supportedKeys } from "@libp2p/crypto/keys"; | ||
import utf8 from "@protobufjs/utf8"; | ||
import { SignatureWithKey } from "./signature.js"; | ||
import { PreHash, prehashFn } from "./prehash.js"; | ||
import { peerIdFromKeys } from "@libp2p/peer-id"; | ||
@variant(1) | ||
@@ -46,3 +48,3 @@ export class Secp256k1PublicKey extends PublicSignKey { | ||
await prehashFn(toSign, PreHash.ETH_KECCAK_256), | ||
signature | ||
signature, | ||
); | ||
@@ -66,3 +68,3 @@ | ||
return peerIdFromKeys( | ||
new supportedKeys["secp256k1"].Secp256k1PublicKey(this.publicKey).bytes | ||
new supportedKeys["secp256k1"].Secp256k1PublicKey(this.publicKey).bytes, | ||
); | ||
@@ -77,3 +79,3 @@ } | ||
return new Secp256k1PublicKey({ | ||
publicKey: id.publicKey.slice(4) // computeAddress(!.slice(4)), | ||
publicKey: id.publicKey.slice(4), // computeAddress(!.slice(4)), | ||
}); | ||
@@ -116,3 +118,3 @@ } | ||
return new Secp256k1PrivateKey({ | ||
privateKey: coerce(id.privateKey!.slice(4)) | ||
privateKey: coerce(id.privateKey.slice(4)), | ||
}); | ||
@@ -132,3 +134,3 @@ } | ||
_wallet: Wallet; | ||
_wallet!: Wallet; | ||
constructor(properties: { | ||
@@ -147,7 +149,7 @@ publicKey: Secp256k1PublicKey; | ||
publicKey: new Secp256k1PublicKey({ | ||
publicKey: generated.public.marshal() | ||
publicKey: generated.public.marshal(), | ||
}), | ||
privateKey: new Secp256k1PrivateKey({ | ||
privateKey: generated.marshal() | ||
}) | ||
privateKey: generated.marshal(), | ||
}), | ||
}); | ||
@@ -160,3 +162,3 @@ | ||
data: Uint8Array, | ||
prehash: PreHash = PreHash.ETH_KECCAK_256 | ||
prehash: PreHash = PreHash.ETH_KECCAK_256, | ||
): Promise<SignatureWithKey> { | ||
@@ -168,3 +170,3 @@ const maybeHashed = await prehashFn(data, prehash); | ||
._signingKey() | ||
.signDigest(maybeHashed) | ||
.signDigest(maybeHashed), | ||
); | ||
@@ -177,3 +179,3 @@ const signatureBytes = new Uint8Array(utf8.length(signature)); // TODO utilize Buffer allocUnsafe | ||
publicKey: this.publicKey, | ||
signature: signatureBytes | ||
signature: signatureBytes, | ||
}); | ||
@@ -195,3 +197,3 @@ } | ||
privateKey: Secp256k1PrivateKey.from(peerId), | ||
publicKey: Secp256k1PublicKey.fromPeerId(peerId) | ||
publicKey: Secp256k1PublicKey.fromPeerId(peerId), | ||
}); | ||
@@ -203,8 +205,8 @@ } | ||
new supportedKeys["secp256k1"].Secp256k1PublicKey( | ||
this.publicKey.publicKey | ||
this.publicKey.publicKey, | ||
).bytes, | ||
new supportedKeys["secp256k1"].Secp256k1PrivateKey( | ||
this.privateKey.privateKey, | ||
this.publicKey.publicKey | ||
).bytes | ||
this.publicKey.publicKey, | ||
).bytes, | ||
); | ||
@@ -225,3 +227,3 @@ } | ||
digest: Uint8Array, | ||
signature: SignatureLike | ||
signature: SignatureLike, | ||
): Uint8Array => { | ||
@@ -233,3 +235,3 @@ const sig = splitSignature(signature); | ||
.recoverPubKey(arrayify(digest), rs, sig.recoveryParam) | ||
.encodeCompressed() | ||
.encodeCompressed(), | ||
); | ||
@@ -240,3 +242,3 @@ }; | ||
signature: SignatureWithKey, | ||
data: Uint8Array | ||
data: Uint8Array, | ||
): Promise<boolean> => { | ||
@@ -246,8 +248,8 @@ const hashedData = await prehashFn(data, signature.prehash); | ||
arrayify(hashedData), | ||
decoder.decode(signature.signature) | ||
decoder.decode(signature.signature), | ||
); | ||
return equals( | ||
signerKey, | ||
(signature.publicKey as Secp256k1PublicKey).publicKey | ||
(signature.publicKey as Secp256k1PublicKey).publicKey, | ||
); | ||
}; |
import { | ||
type AbstractType, | ||
deserialize, | ||
variant, | ||
field, | ||
option, | ||
serialize, | ||
AbstractType | ||
variant, | ||
} from "@dao-xyz/borsh"; | ||
import { equals } from "@peerbit/uint8arrays"; | ||
import { compare, equals } from "uint8arrays"; | ||
import { verifySignatureEd25519 } from "./ed25519-sign.js"; | ||
@@ -15,3 +15,3 @@ import { Ed25519PublicKey } from "./ed25519.js"; | ||
import { Secp256k1PublicKey, verifySignatureSecp256k1 } from "./sepc256k1.js"; | ||
import { SignWithKey } from "./signer.js"; | ||
import { type SignWithKey } from "./signer.js"; | ||
@@ -34,7 +34,5 @@ @variant(0) | ||
}) { | ||
if (props) { | ||
this.signature = props.signature; | ||
this.publicKey = props.publicKey; | ||
this.prehash = props.prehash; | ||
} | ||
this.signature = props.signature; | ||
this.publicKey = props.publicKey; | ||
this.prehash = props.prehash; | ||
} | ||
@@ -47,4 +45,4 @@ | ||
return ( | ||
Buffer.compare(serialize(this.publicKey), serialize(other.publicKey)) === | ||
0 && this.prehash == other.prehash | ||
compare(serialize(this.publicKey), serialize(other.publicKey)) === 0 && | ||
this.prehash === other.prehash | ||
); | ||
@@ -62,3 +60,3 @@ } | ||
constructor(props?: { | ||
constructor(props: { | ||
data: Uint8Array; | ||
@@ -68,7 +66,5 @@ value?: T; | ||
}) { | ||
if (props) { | ||
this.data = props.data; | ||
this.signature = props.signature; | ||
this._value = props.value; | ||
} | ||
this.data = props.data; | ||
this.signature = props.signature; | ||
this._value = props.value; | ||
} | ||
@@ -102,6 +98,5 @@ | ||
/** | ||
* In place | ||
* @param signer | ||
*/ | ||
/* | ||
In place signing | ||
*/ | ||
async sign(signer: SignWithKey): Promise<MaybeSigned<T>> { | ||
@@ -122,170 +117,1 @@ const signatureResult = await signer(this.data); | ||
}; | ||
/* | ||
@variant(0) | ||
export class SignatureWithKey { | ||
@field({ type: Uint8Array }) | ||
signature: Uint8Array; | ||
@field({ type: PublicSignKey }) | ||
publicKey: PublicSignKey; | ||
constructor(props?: { signature: Uint8Array; publicKey: PublicSignKey }) { | ||
if (props) { | ||
this.signature = props.signature; | ||
this.publicKey = props.publicKey; | ||
} | ||
} | ||
equals(other: SignatureWithKey): boolean { | ||
if (!equals(this.signature, other.signature)) { | ||
return false; | ||
} | ||
return ( | ||
Buffer.compare( | ||
serialize(this.publicKey), | ||
serialize(other.publicKey) | ||
) === 0 | ||
); | ||
} | ||
} | ||
@variant(0) | ||
export abstract class MaybeSigned<T> { | ||
@field({ type: Uint8Array }) | ||
data: Uint8Array; | ||
constructor(props: { | ||
data: Uint8Array; | ||
value?: T; | ||
}) { | ||
this.data = props.data; | ||
this._value = props.value; | ||
} | ||
_value?: T; | ||
getValue(constructor: AbstractType<T>): T { | ||
return deserialize(this.data, constructor); | ||
} | ||
abstract verify(): Promise<boolean> | ||
equals(other: MaybeSigned<T>): boolean { | ||
if (!equals(this.data, other.data)) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
@variant(0) | ||
export class Signed<T> extends MaybeSigned<T> { | ||
@field({ type: option(SignatureWithKey) }) | ||
signature: SignatureWithKey; | ||
constructor(props: { | ||
data: Uint8Array; | ||
value?: T; | ||
signature: SignatureWithKey; | ||
}) { | ||
super(props) | ||
this.signature = props.signature; | ||
} | ||
async verify(): Promise<boolean> { | ||
return verify( | ||
this.signature.signature, | ||
this.signature.publicKey, | ||
this.data | ||
); | ||
} | ||
equals(other: MaybeSigned<T>): boolean { | ||
if (other instanceof Signed<T>) { | ||
if (!this.signature !== !other.signature) { | ||
return false; | ||
} | ||
if (this.signature && other.signature) { | ||
return this.signature.equals(other.signature); | ||
} | ||
return super.equals(other) | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
async sign(signer: SignWithKey): Promise<MaybeSigned<T>> { | ||
const signatureResult = await signer(this.data); | ||
this.signature = new SignatureWithKey({ | ||
publicKey: signatureResult.publicKey, | ||
signature: signatureResult.signature, | ||
}); | ||
return this; | ||
} | ||
} | ||
@variant(0) | ||
export class Unsigned<T> extends MaybeSigned<T> { | ||
constructor(props: { | ||
data: Uint8Array; | ||
value?: T; | ||
}) { | ||
super(props) | ||
} | ||
equals(other: MaybeSigned<T>): boolean { | ||
if (other instanceof Unsigned) { | ||
return super.equals(other) | ||
} | ||
else { | ||
return false; | ||
} | ||
} | ||
async sign(signer: SignWithKey): Promise<Signed<T>> { | ||
const signatureResult = await signer(this.data); | ||
return new Signed({ | ||
data: this.data, | ||
signature: new SignatureWithKey({ | ||
publicKey: signatureResult.publicKey, | ||
signature: signatureResult.signature, | ||
}), | ||
value: this._value | ||
}); | ||
} | ||
async verify(): Promise<boolean> { | ||
return true; | ||
} | ||
} | ||
export const verify = ( | ||
signature: Uint8Array, | ||
publicKey: PublicSignKey, | ||
data: Uint8Array | ||
) => { | ||
if (!signature) { | ||
return true; | ||
} | ||
if (publicKey instanceof Ed25519PublicKey) { | ||
return verifySignatureEd25519(signature, publicKey, data); | ||
} else if (publicKey instanceof Secp256k1PublicKey) { | ||
return verifySignatureSecp256k1(signature, publicKey, data); | ||
} | ||
return false; | ||
}; | ||
*/ |
@@ -1,4 +0,4 @@ | ||
import { PublicSignKey } from "./key.js"; | ||
import { PreHash } from "./prehash.js"; | ||
import { SignatureWithKey } from "./signature.js"; | ||
import type { PublicSignKey } from "./key.js"; | ||
import type { PreHash } from "./prehash.js"; | ||
import type { SignatureWithKey } from "./signature.js"; | ||
@@ -8,3 +8,3 @@ export interface Signer { | ||
bytes: Uint8Array, | ||
prehash?: PreHash | ||
prehash?: PreHash, | ||
) => Promise<SignatureWithKey> | SignatureWithKey; | ||
@@ -14,3 +14,3 @@ } | ||
export type SignWithKey = ( | ||
bytes: Uint8Array | ||
bytes: Uint8Array, | ||
) => Promise<SignatureWithKey> | SignatureWithKey; | ||
@@ -17,0 +17,0 @@ |
import sodium from "libsodium-wrappers"; | ||
import { base58btc } from "multiformats/bases/base58"; | ||
export const fromHexString = (hexString: string) => sodium.from_hex(hexString); | ||
@@ -19,1 +21,5 @@ | ||
}; | ||
export const toBase58 = (arr: Uint8Array) => { | ||
return base58btc.baseEncode(arr); | ||
}; |
@@ -1,17 +0,15 @@ | ||
export * from "./errors.js"; | ||
import { field, fixedArray, variant } from "@dao-xyz/borsh"; | ||
import { compare } from "@peerbit/uint8arrays"; | ||
import { type PeerId } from "@libp2p/interface"; | ||
import sodium from "libsodium-wrappers"; | ||
import { compare } from "uint8arrays"; | ||
import { Ed25519Keypair, Ed25519PublicKey } from "./ed25519.js"; | ||
import { | ||
Keypair, | ||
PrivateEncryptionKey, | ||
PublicKeyEncryptionKey | ||
PublicKeyEncryptionKey, | ||
} from "./key.js"; | ||
import { | ||
Ed25519Keypair, | ||
Ed25519PublicKey, | ||
Ed25519PrivateKey | ||
} from "./ed25519.js"; | ||
import { toHexString } from "./utils.js"; | ||
import { PeerId } from "@libp2p/interface"; | ||
export * from "./errors.js"; | ||
@variant(0) | ||
@@ -36,2 +34,3 @@ export class X25519PublicKey extends PublicKeyEncryptionKey { | ||
} | ||
toString(): string { | ||
@@ -42,3 +41,3 @@ return "x25519p/" + toHexString(this.publicKey); | ||
static async from( | ||
ed25119PublicKey: Ed25519PublicKey | ||
ed25119PublicKey: Ed25519PublicKey, | ||
): Promise<X25519PublicKey> { | ||
@@ -48,4 +47,4 @@ await sodium.ready; | ||
publicKey: sodium.crypto_sign_ed25519_pk_to_curve25519( | ||
ed25119PublicKey.publicKey | ||
) | ||
ed25119PublicKey.publicKey, | ||
), | ||
}); | ||
@@ -56,3 +55,3 @@ } | ||
await sodium.ready; | ||
const ed = await Ed25519PublicKey.fromPeerId(peerId); | ||
const ed = Ed25519PublicKey.fromPeerId(peerId); | ||
return X25519PublicKey.from(ed); | ||
@@ -64,3 +63,3 @@ } | ||
return new X25519PublicKey({ | ||
publicKey: sodium.crypto_box_keypair().publicKey | ||
publicKey: sodium.crypto_box_keypair().publicKey, | ||
}); | ||
@@ -85,8 +84,7 @@ } | ||
if (other instanceof X25519SecretKey) { | ||
return ( | ||
compare(this.secretKey, (other as X25519SecretKey).secretKey) === 0 | ||
); | ||
return compare(this.secretKey, other.secretKey) === 0; | ||
} | ||
return false; | ||
} | ||
toString(): string { | ||
@@ -98,5 +96,6 @@ return "x25519s" + toHexString(this.secretKey); | ||
return new X25519PublicKey({ | ||
publicKey: sodium.crypto_scalarmult_base(this.secretKey) | ||
publicKey: sodium.crypto_scalarmult_base(this.secretKey), | ||
}); | ||
} | ||
static async from(ed25119Keypair: Ed25519Keypair): Promise<X25519SecretKey> { | ||
@@ -106,4 +105,4 @@ await sodium.ready; | ||
secretKey: sodium.crypto_sign_ed25519_sk_to_curve25519( | ||
ed25119Keypair.privateKeyPublicKey | ||
) | ||
ed25119Keypair.privateKeyPublicKey, | ||
), | ||
}); | ||
@@ -115,3 +114,3 @@ } | ||
return new X25519SecretKey({ | ||
secretKey: sodium.crypto_box_keypair().privateKey | ||
secretKey: sodium.crypto_box_keypair().privateKey, | ||
}); | ||
@@ -143,7 +142,7 @@ } | ||
publicKey: new X25519PublicKey({ | ||
publicKey: generated.publicKey | ||
publicKey: generated.publicKey, | ||
}), | ||
secretKey: new X25519SecretKey({ | ||
secretKey: generated.privateKey | ||
}) | ||
secretKey: generated.privateKey, | ||
}), | ||
}); | ||
@@ -157,3 +156,3 @@ | ||
publicKey: await X25519PublicKey.from(ed25119Keypair.publicKey), | ||
secretKey: await X25519SecretKey.from(ed25119Keypair) | ||
secretKey: await X25519SecretKey.from(ed25119Keypair), | ||
}); | ||
@@ -167,2 +166,3 @@ return kp; | ||
} | ||
equals(other: Keypair) { | ||
@@ -172,5 +172,3 @@ if (other instanceof X25519Keypair) { | ||
this.publicKey.equals(other.publicKey) && | ||
this.secretKey.equals( | ||
(other as X25519Keypair).secretKey as X25519SecretKey as any | ||
) | ||
this.secretKey.equals(other.secretKey) | ||
); | ||
@@ -177,0 +175,0 @@ } |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
179476
7
109
0
4
3426
2
1
+ Added@libp2p/crypto@4.1.9(transitive)
+ Added@peerbit/cache@2.1.0-2b760c3(transitive)
+ Added@stablelib/binary@2.0.1(transitive)
+ Added@stablelib/hash@2.0.0(transitive)
+ Added@stablelib/int@2.0.1(transitive)
+ Added@stablelib/sha256@2.0.1(transitive)
+ Added@stablelib/wipe@2.0.1(transitive)
+ Addedasn1js@3.0.5(transitive)
+ Addedpvtsutils@1.3.6(transitive)
+ Addedpvutils@1.1.3(transitive)
+ Addedtslib@2.8.1(transitive)
- Removed@peerbit/uint8arrays@3.0.1
- Removed@libp2p/crypto@3.0.4(transitive)
- Removed@peerbit/cache@2.1.2(transitive)
- Removed@peerbit/uint8arrays@3.0.1(transitive)
- Removed@stablelib/binary@1.0.1(transitive)
- Removed@stablelib/hash@1.0.1(transitive)
- Removed@stablelib/int@1.0.1(transitive)
- Removed@stablelib/sha256@1.0.1(transitive)
- Removed@stablelib/wipe@1.0.1(transitive)
- Removedmultiformats@12.1.3(transitive)
- Removednode-forge@1.3.1(transitive)
- Removeduint8arrays@4.0.10(transitive)
Updated@dao-xyz/borsh@^5.2.3
Updated@libp2p/crypto@^4.1.5
Updated@libp2p/peer-id@^4.2.0
Updated@peerbit/cache@2.1.0-2b760c3
Updated@stablelib/sha256@^2.0.0