Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace bech32 and old chacha with noble and scure packages #294

Merged
merged 6 commits into from
Sep 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@babel/preset-typescript": "7.22.15",
"@ethersproject/address": "5.7.0",
"@greymass/eosio": "^0.7.0",
"@jest/globals": "^29.7.0",
"@semantic-release/changelog": "6.0.3",
"@semantic-release/git": "10.0.1",
"@tonomy/antelope-did": "^0.1.5",
Expand All @@ -69,6 +70,7 @@
"eslint-plugin-jest": "27.2.3",
"eslint-plugin-prettier": "5.0.0",
"jest": "29.6.4",
"jest-config": "^29.7.0",
"jsontokens": "4.0.1",
"jsonwebtoken": "9.0.2",
"jwk-to-pem": "2.0.5",
Expand All @@ -86,10 +88,10 @@
"webpack-cli": "5.1.4"
},
"dependencies": {
"@noble/ciphers": "^0.3.0",
"@noble/curves": "^1.0.0",
"@noble/hashes": "^1.3.0",
"@stablelib/xchacha20poly1305": "^1.0.1",
"bech32": "^2.0.0",
"@scure/base": "^1.1.3",
"canonicalize": "^2.0.0",
"did-resolver": "^4.1.0",
"multiformats": "^12.0.0",
Expand Down
2 changes: 1 addition & 1 deletion src/blockchains/cosmos.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { secp256k1 } from '@noble/curves/secp256k1'
import { bech32 } from 'bech32'
import { bech32 } from '@scure/base'
import { sha256, ripemd160 } from '../Digest.js'

export function publicKeyToAddress(publicKey: string, prefix: string): string {
Expand Down
20 changes: 11 additions & 9 deletions src/encryption/xc20pDir.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import type { Decrypter, Encrypter, EncryptionResult, ProtectedHeader } from './types.js'
import { bytesToBase64url, encodeBase64url } from '../util.js'
import { fromString } from 'uint8arrays/from-string'
import { XChaCha20Poly1305 } from '@stablelib/xchacha20poly1305'
import { xchacha20poly1305 } from '@noble/ciphers/chacha'
import { randomBytes } from '@noble/hashes/utils'

export function xc20pEncrypter(key: Uint8Array): (cleartext: Uint8Array, aad?: Uint8Array) => EncryptionResult {
const cipher = new XChaCha20Poly1305(key)
return (cleartext: Uint8Array, aad?: Uint8Array) => {
const iv = randomBytes(cipher.nonceLength)
const sealed = cipher.seal(iv, cleartext, aad)
const iv = randomBytes(24)
const cipher = xchacha20poly1305(key, iv, aad)
const sealed = cipher.encrypt(cleartext)
return {
ciphertext: sealed.subarray(0, sealed.length - cipher.tagLength),
tag: sealed.subarray(sealed.length - cipher.tagLength),
ciphertext: sealed.subarray(0, sealed.length - 16),
tag: sealed.subarray(sealed.length - 16),
iv,
}
}
Expand Down Expand Up @@ -39,10 +39,12 @@ export function xc20pDirEncrypter(key: Uint8Array): Encrypter {
}

export function xc20pDirDecrypter(key: Uint8Array): Decrypter {
const cipher = new XChaCha20Poly1305(key)

async function decrypt(sealed: Uint8Array, iv: Uint8Array, aad?: Uint8Array): Promise<Uint8Array | null> {
return cipher.open(iv, sealed, aad)
try {
return xchacha20poly1305(key, iv, aad).decrypt(sealed)
} catch (error) {
return null
}
}

return { alg: 'dir', enc: 'XC20P', decrypt }
Expand Down
Loading