From a3e457900d929f8414148ac8a388d6e996a98864 Mon Sep 17 00:00:00 2001 From: Akamig Date: Mon, 30 Oct 2023 16:13:26 +0900 Subject: [PATCH] isDigest Removal --- @planetarium/account-aws-kms/package.json | 16 +++++++++++++--- .../account-aws-kms/src/AwsKmsAccount.ts | 14 +++++++++++++- .../src/crypto/browser.ts | 0 .../{tx => account-aws-kms}/src/crypto/node.ts | 0 @planetarium/account/src/Account.ts | 2 +- @planetarium/tx/package.json | 16 +++------------- @planetarium/tx/src/assets.ts | 1 - @planetarium/tx/src/tx/signed.ts | 10 +--------- 8 files changed, 31 insertions(+), 28 deletions(-) rename @planetarium/{tx => account-aws-kms}/src/crypto/browser.ts (100%) rename @planetarium/{tx => account-aws-kms}/src/crypto/node.ts (100%) diff --git a/@planetarium/account-aws-kms/package.json b/@planetarium/account-aws-kms/package.json index 0210e9a7ed3..c1cbab984b4 100644 --- a/@planetarium/account-aws-kms/package.json +++ b/@planetarium/account-aws-kms/package.json @@ -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": [ diff --git a/@planetarium/account-aws-kms/src/AwsKmsAccount.ts b/@planetarium/account-aws-kms/src/AwsKmsAccount.ts index 5b775cc5044..6a47eaff15c 100644 --- a/@planetarium/account-aws-kms/src/AwsKmsAccount.ts +++ b/@planetarium/account-aws-kms/src/AwsKmsAccount.ts @@ -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, @@ -36,9 +37,20 @@ export class AwsKmsAccount implements Account { } async sign(message: Message, isDigest: boolean = false): Promise { + 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", }); diff --git a/@planetarium/tx/src/crypto/browser.ts b/@planetarium/account-aws-kms/src/crypto/browser.ts similarity index 100% rename from @planetarium/tx/src/crypto/browser.ts rename to @planetarium/account-aws-kms/src/crypto/browser.ts diff --git a/@planetarium/tx/src/crypto/node.ts b/@planetarium/account-aws-kms/src/crypto/node.ts similarity index 100% rename from @planetarium/tx/src/crypto/node.ts rename to @planetarium/account-aws-kms/src/crypto/node.ts diff --git a/@planetarium/account/src/Account.ts b/@planetarium/account/src/Account.ts index f56e1ec29e7..087a2352320 100644 --- a/@planetarium/account/src/Account.ts +++ b/@planetarium/account/src/Account.ts @@ -7,7 +7,7 @@ import Signature from "./Signature.js"; export interface Account { getAddress(): Promise
; getPublicKey(): Promise; - sign(message: Message, isDigest?: boolean): Promise; + sign(message: Message): Promise; } export function isAccount(account: unknown): account is Account { diff --git a/@planetarium/tx/package.json b/@planetarium/tx/package.json index 033e150568e..1a5d44b5bbd 100644 --- a/@planetarium/tx/package.json +++ b/@planetarium/tx/package.json @@ -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", diff --git a/@planetarium/tx/src/assets.ts b/@planetarium/tx/src/assets.ts index a222949d028..4727283cc6a 100644 --- a/@planetarium/tx/src/assets.ts +++ b/@planetarium/tx/src/assets.ts @@ -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; diff --git a/@planetarium/tx/src/tx/signed.ts b/@planetarium/tx/src/tx/signed.ts index 320c7dc64e4..ddb1cea512b 100644 --- a/@planetarium/tx/src/tx/signed.ts +++ b/@planetarium/tx/src/tx/signed.ts @@ -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"; @@ -10,8 +9,7 @@ export type SignedTx = T & { signature: Signature }; export async function signTx( tx: UnsignedTx, - signAccount: Account, - isDigest: boolean = false + signAccount: Account ): Promise> { if ( !bytesEqual( @@ -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,