Skip to content

Commit

Permalink
$mol_crypto: total restack
Browse files Browse the repository at this point in the history
  • Loading branch information
jin committed Feb 16, 2025
1 parent 0dacf0d commit e473d77
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
16 changes: 8 additions & 8 deletions crypto/key/key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ namespace $ {
ecdsa,
Boolean( 'extractable' ),
[ 'verify' ],
)
).catch( $mol_crypto_restack )
}

@ $mol_memo.method
Expand All @@ -77,7 +77,7 @@ namespace $ {
ecdh,
true,
[],
)
).catch( $mol_crypto_restack )

}

Expand All @@ -87,7 +87,7 @@ namespace $ {
await this.native(),
sign,
data,
)
).catch( $mol_crypto_restack )
}

}
Expand All @@ -104,9 +104,9 @@ namespace $ {
ecdsa,
Boolean( 'extractable' ),
[ 'sign', 'verify' ]
)
).catch( $mol_crypto_restack )

const { x, y, d } = await $mol_crypto_native.subtle.exportKey( 'jwk', pair.privateKey )
const { x, y, d } = await $mol_crypto_native.subtle.exportKey( 'jwk', pair.privateKey ).catch( $mol_crypto_restack )
return this.from( x + y! + d! )

}
Expand All @@ -128,7 +128,7 @@ namespace $ {
ecdsa,
Boolean( 'extractable' ),
[ 'sign' ],
)
).catch( $mol_crypto_restack )
}

@ $mol_memo.method
Expand All @@ -148,7 +148,7 @@ namespace $ {
ecdh,
Boolean( 'extractable' ),
[ 'deriveKey', 'deriveBits' ],
)
).catch( $mol_crypto_restack )

}

Expand All @@ -162,7 +162,7 @@ namespace $ {
ecdsa,
await this.native(),
data
) )
).catch( $mol_crypto_restack ) )
}

}
Expand Down
9 changes: 9 additions & 0 deletions crypto/restack/restack.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace $ {

/** Derived debuggable error with stack */
export function $mol_crypto_restack( error: any ): never {
error = new Error( error instanceof Error ? error.message : String( error ), { cause: error } )
$mol_fail_hidden( error )
}

}
4 changes: 2 additions & 2 deletions crypto/sacred/pass/pass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace $ {
"PBKDF2",
false,
[ "deriveKey" ],
)
).catch( $mol_crypto_restack )

const hard = await $mol_crypto_native.subtle.deriveKey(
{
Expand All @@ -25,7 +25,7 @@ namespace $ {
},
Boolean( 'extractable' ),
[ 'encrypt', 'decrypt' ],
)
).catch( $mol_crypto_restack )

return $mol_crypto_sacred.from_native( hard )

Expand Down
14 changes: 4 additions & 10 deletions crypto/sacred/sacred.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
namespace $ {

/** Derived debuggable error with stack */
function restack( error: any ): never {
error = new Error( error instanceof Error ? error.message : String( error ), { cause: error } )
$mol_fail_hidden( error )
}

/** Symmetric cipher with shortest payload. */
export class $mol_crypto_sacred extends $mol_buffer {

Expand Down Expand Up @@ -42,7 +36,7 @@ namespace $ {

static async from_native( native: CryptoKey ) {

const buf = await $mol_crypto_native.subtle.exportKey( 'raw', native ).catch( restack )
const buf = await $mol_crypto_native.subtle.exportKey( 'raw', native ).catch( $mol_crypto_restack )

const sacred = this.from( new Uint8Array( buf ) )
sacred._native = native as CryptoKey & { type: 'secret' }
Expand Down Expand Up @@ -72,7 +66,7 @@ namespace $ {
},
true,
[ 'encrypt', 'decrypt' ],
).catch( restack ) as CryptoKey & { type: 'secret' } )
).catch( $mol_crypto_restack ) as CryptoKey & { type: 'secret' } )
}

/** Encrypt any binary message. 16n bytes */
Expand All @@ -86,7 +80,7 @@ namespace $ {
},
await this.native(),
open
).catch( restack ) )
).catch( $mol_crypto_restack ) )
}

/** Decrypt any binary message. */
Expand All @@ -100,7 +94,7 @@ namespace $ {
},
await this.native(),
closed
).catch( restack ) )
).catch( $mol_crypto_restack ) )
}

/** Encrypts this Sacred by another. 16 bytes */
Expand Down
4 changes: 2 additions & 2 deletions crypto/sacred/shared/shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace $ {
},
await priv.native_derive(),
$mol_crypto_sacred.size * 8,
) )
)
).catch( $mol_crypto_restack )
) )

}

Expand Down

0 comments on commit e473d77

Please sign in to comment.