Skip to content

Commit

Permalink
isDigest Removal
Browse files Browse the repository at this point in the history
  • Loading branch information
Akamig committed Oct 30, 2023
1 parent 83612b1 commit a3e4579
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 28 deletions.
16 changes: 13 additions & 3 deletions @planetarium/account-aws-kms/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@
"description": "Libplanet account implementation using AWS KMS",
"type": "module",
"main": "./dist/index.js",
"imports": {
"#crypto": {
"node": "./src/crypto/node.ts",
"default": "./src/crypto/browser.ts"
}
},
"exports": {
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
"node": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"browser": "./dist/index.browser.mjs",
"default": "./dist/index.js"
}
},
"files": [
Expand Down
14 changes: 13 additions & 1 deletion @planetarium/account-aws-kms/src/AwsKmsAccount.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AwsKmsKeyId } from "./AwsKmsKeyId.js";
import { KMSClient, SignCommand } from "@aws-sdk/client-kms";
import { Signature as NobleSignature } from "@noble/secp256k1";
import { crypto } from "#crypto";
import {
Address,
type Account,
Expand Down Expand Up @@ -36,9 +37,20 @@ export class AwsKmsAccount implements Account {
}

async sign(message: Message, isDigest: boolean = false): Promise<Signature> {
let finalMessage = message;

if (isDigest) {
const digest = await crypto.subtle.digest(
"SHA-256",
crypto.encode(message)
);
const digestArray = new Uint8Array(digest);
finalMessage = digestArray;
}

const cmd = new SignCommand({
KeyId: this.keyId,
Message: message,
Message: finalMessage,
MessageType: isDigest ? "DIGEST" : "RAW",
SigningAlgorithm: "ECDSA_SHA_256",
});
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion @planetarium/account/src/Account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import Signature from "./Signature.js";
export interface Account {
getAddress(): Promise<Address>;
getPublicKey(): Promise<PublicKey>;
sign(message: Message, isDigest?: boolean): Promise<Signature>;
sign(message: Message): Promise<Signature>;
}

export function isAccount(account: unknown): account is Account {
Expand Down
16 changes: 3 additions & 13 deletions @planetarium/tx/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,11 @@
"description": "Creating Libplanet transactions from JavaScript/TypeScript",
"type": "module",
"main": "./dist/index.js",
"imports": {
"#crypto": {
"node": "./src/crypto/node.ts",
"default": "./src/crypto/browser.ts"
}
},
"exports": {
".": {
"node": {
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"browser": "./dist/index.browser.mjs",
"default": "./dist/index.js"
"types": "./dist/index.d.ts",
"import": "./dist/index.js",
"require": "./dist/index.cjs"
}
},
"types": "./dist/index.d.ts",
Expand Down
1 change: 0 additions & 1 deletion @planetarium/tx/src/assets.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { encode, RecordValue, RecordView, Value } from "@planetarium/bencodex";
import { Address, encodeAddressSet } from "./address.js";
import { crypto } from "#crypto";

export interface Currency {
ticker: string;
Expand Down
10 changes: 1 addition & 9 deletions @planetarium/tx/src/tx/signed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { BencodexDictionary, Dictionary, encode } from "@planetarium/bencodex";
import { Account, Address, Signature } from "@planetarium/account";
import { crypto } from "#crypto";
import { type UnsignedTx, encodeUnsignedTx } from "./unsigned.js";
import { bytesEqual } from "../bytes.js";

Expand All @@ -10,8 +9,7 @@ export type SignedTx<T extends UnsignedTx> = T & { signature: Signature };

export async function signTx(
tx: UnsignedTx,
signAccount: Account,
isDigest: boolean = false
signAccount: Account
): Promise<SignedTx<typeof tx>> {
if (
!bytesEqual(
Expand All @@ -29,12 +27,6 @@ export async function signTx(
throw new Error("The transaction signer does not match to the signAccount");
}
const payload = encodeUnsignedTx(tx);
if (isDigest) {
const digest = await crypto.subtle.digest("SHA-256", encode(payload));
const array = new Uint8Array(digest);
const signature = await signAccount.sign(array, true);
return { ...tx, signature };
}
const signature = await signAccount.sign(encode(payload));
return {
...tx,
Expand Down

1 comment on commit a3e4579

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Benchmark.Net Benchmark'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: a3e4579 Previous: 0e4d8e5 Ratio
Libplanet.Benchmarks.Store.GetRecentTxOutOfManyTxs 8447.145161290322 ns (± 1675.8303694195079) 3653.2634408602153 ns (± 846.9821541912596) 2.31

This comment was automatically generated by workflow using github-action-benchmark.

CC: @libplanet

Please sign in to comment.