@socket.tech/dl-common 1.0.3-test.17 → 1.0.3-test.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/utils/ethersAwsKmsSigner.d.ts +2 -2
- package/dist/utils/ethersAwsKmsSigner.js +2 -2
- package/dist/utils/signer/adapter.d.ts +18 -0
- package/dist/utils/signer/adapter.js +71 -0
- package/dist/utils/signer/address.d.ts +10 -0
- package/dist/utils/signer/address.js +42 -0
- package/dist/utils/signer/asn1-parser.d.ts +11 -0
- package/dist/utils/signer/asn1-parser.js +80 -0
- package/dist/utils/signer/crypto.d.ts +6 -0
- package/dist/utils/signer/crypto.js +34 -0
- package/dist/utils/signer/index.d.ts +4 -0
- package/dist/utils/signer/index.js +20 -0
- package/dist/utils/signer/kms-ethers-signer.d.ts +19 -0
- package/dist/utils/signer/kms-ethers-signer.js +32 -0
- package/dist/utils/signer/kms-signer.d.ts +17 -0
- package/dist/utils/signer/kms-signer.js +46 -0
- package/dist/utils/signer/signature.d.ts +17 -0
- package/dist/utils/signer/signature.js +65 -0
- package/package.json +15 -4
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export declare const getAwsKmsSigner: (keyId: string, region?: string) => Promise<
|
|
1
|
+
import { KmsEthersSigner } from "./signer";
|
|
2
|
+
export declare const getAwsKmsSigner: (keyId: string, region?: string) => Promise<KmsEthersSigner>;
|
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getAwsKmsSigner = void 0;
|
|
4
|
-
const
|
|
4
|
+
const signer_1 = require("./signer");
|
|
5
5
|
const getAwsKmsSigner = async (keyId, region = "us-east-1") => {
|
|
6
6
|
try {
|
|
7
7
|
const kmsCredentials = {
|
|
8
8
|
region,
|
|
9
9
|
keyId,
|
|
10
10
|
};
|
|
11
|
-
let signer = new
|
|
11
|
+
let signer = new signer_1.KmsEthersSigner(kmsCredentials);
|
|
12
12
|
let signerAddress = await signer.getAddress();
|
|
13
13
|
// logger.info(` keyid ${keyId} ${signerAddress}`);
|
|
14
14
|
if (!signerAddress) {
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { Signer as ISigner } from "aws-kms-signer";
|
|
2
|
+
import { Signer } from "@ethersproject/abstract-signer";
|
|
3
|
+
import { Deferrable } from "@ethersproject/properties";
|
|
4
|
+
import { Bytes } from "@ethersproject/bytes";
|
|
5
|
+
import type { Provider, TransactionRequest } from "@ethersproject/abstract-provider";
|
|
6
|
+
export type AdapterConfig = {
|
|
7
|
+
signer: ISigner;
|
|
8
|
+
version: string;
|
|
9
|
+
};
|
|
10
|
+
export declare class Adapter extends Signer {
|
|
11
|
+
private readonly signer;
|
|
12
|
+
private readonly logger;
|
|
13
|
+
constructor(config: AdapterConfig, provider?: Provider);
|
|
14
|
+
getAddress(): Promise<string>;
|
|
15
|
+
signMessage(message: Bytes | string): Promise<string>;
|
|
16
|
+
signTransaction(deferrableTransaction: Deferrable<TransactionRequest>): Promise<string>;
|
|
17
|
+
connect(provider: Provider): Adapter;
|
|
18
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Adapter = void 0;
|
|
4
|
+
const abstract_signer_1 = require("@ethersproject/abstract-signer");
|
|
5
|
+
const properties_1 = require("@ethersproject/properties");
|
|
6
|
+
const bytes_1 = require("@ethersproject/bytes");
|
|
7
|
+
const hash_1 = require("@ethersproject/hash");
|
|
8
|
+
const logger_1 = require("@ethersproject/logger");
|
|
9
|
+
const address_1 = require("@ethersproject/address");
|
|
10
|
+
const keccak256_1 = require("@ethersproject/keccak256");
|
|
11
|
+
const bignumber_1 = require("@ethersproject/bignumber");
|
|
12
|
+
const transactions_1 = require("@ethersproject/transactions");
|
|
13
|
+
class Adapter extends abstract_signer_1.Signer {
|
|
14
|
+
constructor(config, provider) {
|
|
15
|
+
super();
|
|
16
|
+
this.signer = config.signer;
|
|
17
|
+
(0, properties_1.defineReadOnly)(this, "provider", provider);
|
|
18
|
+
this.logger = new logger_1.Logger(config.version);
|
|
19
|
+
}
|
|
20
|
+
async getAddress() {
|
|
21
|
+
const address = await this.signer.getAddress();
|
|
22
|
+
return (0, address_1.getAddress)(address.toString());
|
|
23
|
+
}
|
|
24
|
+
async signMessage(message) {
|
|
25
|
+
const digest = (0, hash_1.hashMessage)(message);
|
|
26
|
+
const signature = await this.signer.sign(Buffer.from(digest.slice(2), "hex"));
|
|
27
|
+
return signature.toString();
|
|
28
|
+
}
|
|
29
|
+
async signTransaction(deferrableTransaction) {
|
|
30
|
+
const transaction = await (0, properties_1.resolveProperties)(deferrableTransaction);
|
|
31
|
+
const address = await this.getAddress();
|
|
32
|
+
if (transaction.from != null) {
|
|
33
|
+
if ((0, address_1.getAddress)(transaction.from) !== address) {
|
|
34
|
+
this.logger.throwArgumentError("transaction from address mismatch", "transaction.from", transaction.from);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
const nonce = transaction.nonce
|
|
38
|
+
? bignumber_1.BigNumber.from(transaction.nonce).toNumber()
|
|
39
|
+
: undefined;
|
|
40
|
+
const unsignedTransaction = {
|
|
41
|
+
to: transaction.to,
|
|
42
|
+
nonce,
|
|
43
|
+
gasLimit: transaction.gasLimit,
|
|
44
|
+
gasPrice: transaction.gasPrice,
|
|
45
|
+
data: transaction.data,
|
|
46
|
+
value: transaction.value,
|
|
47
|
+
chainId: transaction.chainId,
|
|
48
|
+
type: transaction.type,
|
|
49
|
+
accessList: transaction.accessList,
|
|
50
|
+
maxPriorityFeePerGas: transaction.maxPriorityFeePerGas,
|
|
51
|
+
maxFeePerGas: transaction.maxFeePerGas,
|
|
52
|
+
};
|
|
53
|
+
Object.keys(unsignedTransaction).forEach((key) => {
|
|
54
|
+
if (key in unsignedTransaction && unsignedTransaction[key] == undefined) {
|
|
55
|
+
delete unsignedTransaction[key];
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
const digest = (0, keccak256_1.keccak256)((0, transactions_1.serialize)(unsignedTransaction));
|
|
59
|
+
const signature = await this.signer.sign(Buffer.from(digest.slice(2), "hex"));
|
|
60
|
+
const ethersSignature = (0, bytes_1.splitSignature)({
|
|
61
|
+
v: signature.v,
|
|
62
|
+
r: `0x${signature.r.toString("hex")}`,
|
|
63
|
+
s: `0x${signature.s.toString("hex")}`,
|
|
64
|
+
});
|
|
65
|
+
return (0, transactions_1.serialize)(unsignedTransaction, ethersSignature);
|
|
66
|
+
}
|
|
67
|
+
connect(provider) {
|
|
68
|
+
return new Adapter({ signer: this.signer, version: this.logger.version }, provider);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.Adapter = Adapter;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
export declare class Address {
|
|
3
|
+
private readonly buffer;
|
|
4
|
+
private constructor();
|
|
5
|
+
private static toAddress;
|
|
6
|
+
static fromPublicKey(publicKey: Buffer): Address;
|
|
7
|
+
private toChecksumAddress;
|
|
8
|
+
toString(): string;
|
|
9
|
+
equals(other: Address): boolean;
|
|
10
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Address = void 0;
|
|
7
|
+
const keccak_1 = __importDefault(require("keccak"));
|
|
8
|
+
class Address {
|
|
9
|
+
constructor(buffer) {
|
|
10
|
+
this.buffer = buffer;
|
|
11
|
+
}
|
|
12
|
+
static toAddress(publicKey) {
|
|
13
|
+
const address = (0, keccak_1.default)("keccak256")
|
|
14
|
+
.update(publicKey)
|
|
15
|
+
.digest()
|
|
16
|
+
.slice(12, 32);
|
|
17
|
+
return address;
|
|
18
|
+
}
|
|
19
|
+
static fromPublicKey(publicKey) {
|
|
20
|
+
if (publicKey.length !== 64) {
|
|
21
|
+
throw TypeError(`Address: invalid public key. buffer length must be 64. actual: ${publicKey.length}`);
|
|
22
|
+
}
|
|
23
|
+
return new Address(this.toAddress(publicKey));
|
|
24
|
+
}
|
|
25
|
+
toChecksumAddress(address) {
|
|
26
|
+
// EIP-55 https://github.com/ethereum/EIPs/blob/master/EIPS/eip-55.md#implementation
|
|
27
|
+
const hash = (0, keccak_1.default)("keccak256").update(address).digest("hex");
|
|
28
|
+
return address
|
|
29
|
+
.split("")
|
|
30
|
+
.map((c, i) => {
|
|
31
|
+
return Number.parseInt(hash[i], 16) > 7 ? c.toUpperCase() : c;
|
|
32
|
+
})
|
|
33
|
+
.join("");
|
|
34
|
+
}
|
|
35
|
+
toString() {
|
|
36
|
+
return this.toChecksumAddress(this.buffer.toString("hex"));
|
|
37
|
+
}
|
|
38
|
+
equals(other) {
|
|
39
|
+
return this.buffer.equals(other.buffer);
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
exports.Address = Address;
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
export declare function parsePublicKey(buf: Buffer): Buffer;
|
|
3
|
+
/**
|
|
4
|
+
* Parse signature from the given signature.
|
|
5
|
+
* @param buf The buffer of the signature which following ASN.1 format.
|
|
6
|
+
* @returns The set consists of, `r` and `s`. The `r` and `s` are parsed from the given signature and converted to 32 statically sized format from ASN.1 format. For example, if there is 33-bytes unsigned integer formatted by ASN.1, it will be converted into 32-length bytes which dropped the first byte. (e.g. `00e26f7c547cf497959af070ec7c43ebdbb3e4341395912d6ccc950b43e886781b` → `e26f7c547cf497959af070ec7c43ebdbb3e4341395912d6ccc950b43e886781b`). And, if there is 31-bytes integer formatted by ASN.1, it will be padded into 32-length bytes with `0` byte. (e.g. `74e3e4d71e7385ae71042b0f99f7fbbf66e7760dd513ed2fcea754e2a9131c` → `0074e3e4d71e7385ae71042b0f99f7fbbf66e7760dd513ed2fcea754e2a9131c`).
|
|
7
|
+
*/
|
|
8
|
+
export declare function parseSignature(buf: Buffer): {
|
|
9
|
+
r: Buffer;
|
|
10
|
+
s: Buffer;
|
|
11
|
+
};
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
26
|
+
exports.parseSignature = exports.parsePublicKey = void 0;
|
|
27
|
+
const asn1js = __importStar(require("asn1js"));
|
|
28
|
+
function toArrayBuffer(buffer) {
|
|
29
|
+
const ab = new ArrayBuffer(buffer.length);
|
|
30
|
+
const view = new Uint8Array(ab);
|
|
31
|
+
for (let i = 0; i < buffer.length; ++i) {
|
|
32
|
+
view[i] = buffer[i];
|
|
33
|
+
}
|
|
34
|
+
return ab;
|
|
35
|
+
}
|
|
36
|
+
function isUnsignedInteger(buffer) {
|
|
37
|
+
return buffer[0] === 0;
|
|
38
|
+
}
|
|
39
|
+
function pad(params) {
|
|
40
|
+
const { buffer, length, element } = params;
|
|
41
|
+
const padding = Buffer.alloc(length - buffer.length, element);
|
|
42
|
+
return Buffer.concat([padding, buffer]);
|
|
43
|
+
}
|
|
44
|
+
function parsePublicKey(buf) {
|
|
45
|
+
const { result } = asn1js.fromBER(toArrayBuffer(buf));
|
|
46
|
+
const values = result.valueBlock.value;
|
|
47
|
+
const value = values[1];
|
|
48
|
+
return Buffer.from(value.valueBlock.valueHex.slice(1));
|
|
49
|
+
}
|
|
50
|
+
exports.parsePublicKey = parsePublicKey;
|
|
51
|
+
/**
|
|
52
|
+
* Parse signature from the given signature.
|
|
53
|
+
* @param buf The buffer of the signature which following ASN.1 format.
|
|
54
|
+
* @returns The set consists of, `r` and `s`. The `r` and `s` are parsed from the given signature and converted to 32 statically sized format from ASN.1 format. For example, if there is 33-bytes unsigned integer formatted by ASN.1, it will be converted into 32-length bytes which dropped the first byte. (e.g. `00e26f7c547cf497959af070ec7c43ebdbb3e4341395912d6ccc950b43e886781b` → `e26f7c547cf497959af070ec7c43ebdbb3e4341395912d6ccc950b43e886781b`). And, if there is 31-bytes integer formatted by ASN.1, it will be padded into 32-length bytes with `0` byte. (e.g. `74e3e4d71e7385ae71042b0f99f7fbbf66e7760dd513ed2fcea754e2a9131c` → `0074e3e4d71e7385ae71042b0f99f7fbbf66e7760dd513ed2fcea754e2a9131c`).
|
|
55
|
+
*/
|
|
56
|
+
function parseSignature(buf) {
|
|
57
|
+
const { result } = asn1js.fromBER(toArrayBuffer(buf));
|
|
58
|
+
const values = result.valueBlock.value;
|
|
59
|
+
const getHex = (value) => Buffer.from(value.valueBlock.valueHex);
|
|
60
|
+
let r = getHex(values[0]);
|
|
61
|
+
let s = getHex(values[1]);
|
|
62
|
+
if (isUnsignedInteger(r)) {
|
|
63
|
+
r = r.slice(1);
|
|
64
|
+
}
|
|
65
|
+
if (isUnsignedInteger(s)) {
|
|
66
|
+
s = s.slice(1);
|
|
67
|
+
}
|
|
68
|
+
r = pad({
|
|
69
|
+
buffer: r,
|
|
70
|
+
length: 32,
|
|
71
|
+
element: 0,
|
|
72
|
+
});
|
|
73
|
+
s = pad({
|
|
74
|
+
buffer: s,
|
|
75
|
+
length: 32,
|
|
76
|
+
element: 0,
|
|
77
|
+
});
|
|
78
|
+
return { r, s };
|
|
79
|
+
}
|
|
80
|
+
exports.parseSignature = parseSignature;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
export type Hash = string;
|
|
3
|
+
export type Address = string;
|
|
4
|
+
export declare function removeHexadecimalPrefix(x: string): string;
|
|
5
|
+
export declare function addHexadecimalPrefix(x: string): string;
|
|
6
|
+
export declare function recover(digest: Buffer, r: Buffer, s: Buffer, v: number): Buffer;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.recover = exports.addHexadecimalPrefix = exports.removeHexadecimalPrefix = void 0;
|
|
7
|
+
const secp256k1_1 = __importDefault(require("secp256k1"));
|
|
8
|
+
function removeHexadecimalPrefix(x) {
|
|
9
|
+
if (x.startsWith("0x")) {
|
|
10
|
+
return x.slice(2);
|
|
11
|
+
}
|
|
12
|
+
return x;
|
|
13
|
+
}
|
|
14
|
+
exports.removeHexadecimalPrefix = removeHexadecimalPrefix;
|
|
15
|
+
function addHexadecimalPrefix(x) {
|
|
16
|
+
if (x.startsWith("0x")) {
|
|
17
|
+
return x;
|
|
18
|
+
}
|
|
19
|
+
return `0x${x}`;
|
|
20
|
+
}
|
|
21
|
+
exports.addHexadecimalPrefix = addHexadecimalPrefix;
|
|
22
|
+
function recover(digest, r, s, v) {
|
|
23
|
+
if (r.length !== 32) {
|
|
24
|
+
throw new Error("invalid signature length");
|
|
25
|
+
}
|
|
26
|
+
if (s.length !== 32) {
|
|
27
|
+
throw new Error("invalid signature length");
|
|
28
|
+
}
|
|
29
|
+
const publicKey = secp256k1_1.default
|
|
30
|
+
.ecdsaRecover(Uint8Array.from(Buffer.concat([r, s])), v, Uint8Array.from(digest), false)
|
|
31
|
+
.slice(1);
|
|
32
|
+
return Buffer.from(publicKey);
|
|
33
|
+
}
|
|
34
|
+
exports.recover = recover;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./address"), exports);
|
|
18
|
+
__exportStar(require("./signature"), exports);
|
|
19
|
+
__exportStar(require("./kms-signer"), exports);
|
|
20
|
+
__exportStar(require("./kms-ethers-signer"), exports);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Signer } from "@ethersproject/abstract-signer";
|
|
2
|
+
import type { Deferrable } from "@ethersproject/properties";
|
|
3
|
+
import type { Bytes } from "@ethersproject/bytes";
|
|
4
|
+
import type { Provider, TransactionRequest } from "@ethersproject/abstract-provider";
|
|
5
|
+
import type { KMSClientConfig } from "@aws-sdk/client-kms";
|
|
6
|
+
export type KmsEthersSignerConfig = {
|
|
7
|
+
keyId: string;
|
|
8
|
+
kmsClientConfig?: KMSClientConfig;
|
|
9
|
+
};
|
|
10
|
+
export declare class KmsEthersSigner extends Signer {
|
|
11
|
+
private readonly config;
|
|
12
|
+
private readonly kmsSigner;
|
|
13
|
+
private readonly adapter;
|
|
14
|
+
constructor(config: KmsEthersSignerConfig, provider?: Provider);
|
|
15
|
+
getAddress(): Promise<string>;
|
|
16
|
+
signMessage(message: Bytes | string): Promise<string>;
|
|
17
|
+
signTransaction(deferrableTransaction: Deferrable<TransactionRequest>): Promise<string>;
|
|
18
|
+
connect(provider: Provider): KmsEthersSigner;
|
|
19
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KmsEthersSigner = void 0;
|
|
4
|
+
const abstract_signer_1 = require("@ethersproject/abstract-signer");
|
|
5
|
+
const properties_1 = require("@ethersproject/properties");
|
|
6
|
+
const aws_kms_signer_1 = require("aws-kms-signer");
|
|
7
|
+
const adapter_1 = require("./adapter");
|
|
8
|
+
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
|
9
|
+
const version = require("../package.json").version;
|
|
10
|
+
class KmsEthersSigner extends abstract_signer_1.Signer {
|
|
11
|
+
constructor(config, provider) {
|
|
12
|
+
var _a;
|
|
13
|
+
super();
|
|
14
|
+
this.config = config;
|
|
15
|
+
(0, properties_1.defineReadOnly)(this, "provider", provider);
|
|
16
|
+
this.kmsSigner = new aws_kms_signer_1.KmsSigner(config.keyId, (_a = config.kmsClientConfig) !== null && _a !== void 0 ? _a : {});
|
|
17
|
+
this.adapter = new adapter_1.Adapter({ signer: this.kmsSigner, version }, provider);
|
|
18
|
+
}
|
|
19
|
+
async getAddress() {
|
|
20
|
+
return this.adapter.getAddress();
|
|
21
|
+
}
|
|
22
|
+
async signMessage(message) {
|
|
23
|
+
return this.adapter.signMessage(message);
|
|
24
|
+
}
|
|
25
|
+
async signTransaction(deferrableTransaction) {
|
|
26
|
+
return this.adapter.signTransaction(deferrableTransaction);
|
|
27
|
+
}
|
|
28
|
+
connect(provider) {
|
|
29
|
+
return new KmsEthersSigner(this.config, provider);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.KmsEthersSigner = KmsEthersSigner;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import { KMSClientConfig } from "@aws-sdk/client-kms";
|
|
3
|
+
import { Signature } from "./signature";
|
|
4
|
+
import { Address } from "./address";
|
|
5
|
+
export interface Signer {
|
|
6
|
+
sign(digest: Buffer): Promise<Signature>;
|
|
7
|
+
getAddress(): Promise<Address>;
|
|
8
|
+
}
|
|
9
|
+
export declare class KmsSigner implements Signer {
|
|
10
|
+
private readonly keyId;
|
|
11
|
+
private readonly client;
|
|
12
|
+
constructor(keyId: string, config?: KMSClientConfig);
|
|
13
|
+
sign(digest: Buffer): Promise<Signature>;
|
|
14
|
+
getAddress(): Promise<Address>;
|
|
15
|
+
getPublicKey(): Promise<Buffer>;
|
|
16
|
+
private _sign;
|
|
17
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.KmsSigner = void 0;
|
|
4
|
+
const client_kms_1 = require("@aws-sdk/client-kms");
|
|
5
|
+
const signature_1 = require("./signature");
|
|
6
|
+
const asn1_parser_1 = require("./asn1-parser");
|
|
7
|
+
const address_1 = require("./address");
|
|
8
|
+
class KmsSigner {
|
|
9
|
+
constructor(keyId, config) {
|
|
10
|
+
this.keyId = keyId;
|
|
11
|
+
this.client = new client_kms_1.KMSClient(config !== null && config !== void 0 ? config : {});
|
|
12
|
+
}
|
|
13
|
+
async sign(digest) {
|
|
14
|
+
const asn1Signature = await this._sign(digest);
|
|
15
|
+
const address = await this.getAddress();
|
|
16
|
+
const { r, s } = (0, asn1_parser_1.parseSignature)(asn1Signature);
|
|
17
|
+
return signature_1.Signature.fromDigest(digest, address, r, s);
|
|
18
|
+
}
|
|
19
|
+
async getAddress() {
|
|
20
|
+
const asn1PublicKey = await this.getPublicKey();
|
|
21
|
+
const publicKey = (0, asn1_parser_1.parsePublicKey)(asn1PublicKey);
|
|
22
|
+
return address_1.Address.fromPublicKey(publicKey);
|
|
23
|
+
}
|
|
24
|
+
async getPublicKey() {
|
|
25
|
+
const command = new client_kms_1.GetPublicKeyCommand({ KeyId: this.keyId });
|
|
26
|
+
const response = await this.client.send(command);
|
|
27
|
+
if (!response.PublicKey) {
|
|
28
|
+
throw new TypeError("PublicKey is undefined");
|
|
29
|
+
}
|
|
30
|
+
return Buffer.from(response.PublicKey);
|
|
31
|
+
}
|
|
32
|
+
async _sign(digest) {
|
|
33
|
+
const command = new client_kms_1.SignCommand({
|
|
34
|
+
KeyId: this.keyId,
|
|
35
|
+
Message: digest,
|
|
36
|
+
MessageType: "DIGEST",
|
|
37
|
+
SigningAlgorithm: "ECDSA_SHA_256",
|
|
38
|
+
});
|
|
39
|
+
const response = await this.client.send(command);
|
|
40
|
+
if (!response.Signature) {
|
|
41
|
+
throw new TypeError("Signature is undefined");
|
|
42
|
+
}
|
|
43
|
+
return Buffer.from(response.Signature);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
exports.KmsSigner = KmsSigner;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/// <reference types="node" />
|
|
2
|
+
import BN from "bn.js";
|
|
3
|
+
import { Address } from "./address";
|
|
4
|
+
export declare const secp256k1N: BN;
|
|
5
|
+
export declare const secp256k1halfN: BN;
|
|
6
|
+
export declare class Signature {
|
|
7
|
+
private readonly buffer;
|
|
8
|
+
constructor(buffer: Buffer);
|
|
9
|
+
static fromRSV(r: Buffer, s: Buffer, v: number): Signature;
|
|
10
|
+
static fromDigest(digest: Buffer, address: Address, r: Buffer, s: Buffer): Signature;
|
|
11
|
+
get r(): Buffer;
|
|
12
|
+
get s(): Buffer;
|
|
13
|
+
get v(): number;
|
|
14
|
+
get recovery(): number;
|
|
15
|
+
isCompatibleEip2(): boolean;
|
|
16
|
+
toString(): string;
|
|
17
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.Signature = exports.secp256k1halfN = exports.secp256k1N = void 0;
|
|
7
|
+
const bn_js_1 = __importDefault(require("bn.js"));
|
|
8
|
+
const crypto_1 = require("./crypto");
|
|
9
|
+
const address_1 = require("./address");
|
|
10
|
+
exports.secp256k1N = new bn_js_1.default("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", 16);
|
|
11
|
+
exports.secp256k1halfN = exports.secp256k1N.div(new bn_js_1.default(2));
|
|
12
|
+
class Signature {
|
|
13
|
+
constructor(buffer) {
|
|
14
|
+
if (buffer.length !== 65) {
|
|
15
|
+
throw TypeError(`Signature: invalid signature. buffer length must be 65. actual: ${buffer.length}`);
|
|
16
|
+
}
|
|
17
|
+
this.buffer = buffer;
|
|
18
|
+
if (![27, 28].includes(this.recovery)) {
|
|
19
|
+
throw Error(`Signature: invalid signature. V must be 27 or 28. actual: ${this.recovery}`);
|
|
20
|
+
}
|
|
21
|
+
if (!this.isCompatibleEip2()) {
|
|
22
|
+
const s = new bn_js_1.default(this.s);
|
|
23
|
+
const recovery = Buffer.alloc(1);
|
|
24
|
+
recovery.writeUInt8((this.v % 2) + 27, 0);
|
|
25
|
+
const reverseS = exports.secp256k1N.sub(s).toArrayLike(Buffer, "be", 32);
|
|
26
|
+
this.buffer = Buffer.concat([this.r, reverseS, recovery]);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
static fromRSV(r, s, v) {
|
|
30
|
+
const recovery = Buffer.alloc(1);
|
|
31
|
+
recovery.writeUInt8(v, 0);
|
|
32
|
+
return new Signature(Buffer.concat([r, s, recovery]));
|
|
33
|
+
}
|
|
34
|
+
static fromDigest(digest, address, r, s) {
|
|
35
|
+
const candidate = [...new Array(2).keys()].filter((v) => {
|
|
36
|
+
const publicKey = (0, crypto_1.recover)(digest, r, s, v);
|
|
37
|
+
return address.equals(address_1.Address.fromPublicKey(publicKey));
|
|
38
|
+
});
|
|
39
|
+
if (candidate.length === 1) {
|
|
40
|
+
const v = candidate[0] + 27;
|
|
41
|
+
return Signature.fromRSV(r, s, v);
|
|
42
|
+
}
|
|
43
|
+
throw new Error(`Signature: failed to solve V.`);
|
|
44
|
+
}
|
|
45
|
+
get r() {
|
|
46
|
+
return this.buffer.slice(0, 32);
|
|
47
|
+
}
|
|
48
|
+
get s() {
|
|
49
|
+
return this.buffer.slice(32, 64);
|
|
50
|
+
}
|
|
51
|
+
get v() {
|
|
52
|
+
return this.buffer.readUInt8(64);
|
|
53
|
+
}
|
|
54
|
+
get recovery() {
|
|
55
|
+
return this.v;
|
|
56
|
+
}
|
|
57
|
+
isCompatibleEip2() {
|
|
58
|
+
const s = new bn_js_1.default(this.s);
|
|
59
|
+
return exports.secp256k1halfN.cmp(s) > 0;
|
|
60
|
+
}
|
|
61
|
+
toString() {
|
|
62
|
+
return this.buffer.toString("hex");
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.Signature = Signature;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@socket.tech/dl-common",
|
|
3
|
-
"version": "1.0.3-test.
|
|
3
|
+
"version": "1.0.3-test.19",
|
|
4
4
|
"main": "./dist/index.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"description": "common utilities for socket data layer.",
|
|
@@ -32,11 +32,14 @@
|
|
|
32
32
|
"format:check": "prettier \"./**\" --ignore-unknown --check",
|
|
33
33
|
"prepare": "husky install",
|
|
34
34
|
"publishp": "yarn lint && yarn build",
|
|
35
|
-
"testc": "jest --coverage",
|
|
36
|
-
"
|
|
35
|
+
"testc": "jest --testPathIgnorePatterns '.db.test.ts$' --coverage",
|
|
36
|
+
"testdb": "jest --testMatch **/*.db.test.ts --runInBand",
|
|
37
|
+
"testdbc": "jest --testMatch **/*.db.test.ts --runInBand --coverage",
|
|
38
|
+
"test": "jest --testPathIgnorePatterns '.db.test.ts$'"
|
|
37
39
|
},
|
|
38
40
|
"dependencies": {
|
|
39
41
|
"@aws-sdk/client-eventbridge": "^3.449.0",
|
|
42
|
+
"@aws-sdk/client-kms": "^3.454.0",
|
|
40
43
|
"@aws-sdk/client-secrets-manager": "^3.433.0",
|
|
41
44
|
"@aws-sdk/client-sqs": "^3.421.0",
|
|
42
45
|
"@aws-sdk/smithy-client": "^3.329.0",
|
|
@@ -44,11 +47,15 @@
|
|
|
44
47
|
"@socket.tech/dl-core": "2.4.13",
|
|
45
48
|
"@socket.tech/ll-common": "^0.0.19",
|
|
46
49
|
"async-redis": "^2.0.0",
|
|
50
|
+
"aws-kms-ethers-signer": "^0.1.3",
|
|
51
|
+
"bn.js": "^5.2.1",
|
|
47
52
|
"ethers": "^5.7.1",
|
|
48
|
-
"
|
|
53
|
+
"keccak": "^3.0.4",
|
|
54
|
+
"lodash": "^4.17.21",
|
|
49
55
|
"pg": "^8.8.0",
|
|
50
56
|
"pg-hstore": "^2.3.4",
|
|
51
57
|
"prom-client": "^14.2.0",
|
|
58
|
+
"secp256k1": "^5.0.0",
|
|
52
59
|
"sequelize": "^6.21.6"
|
|
53
60
|
},
|
|
54
61
|
"devDependencies": {
|
|
@@ -59,13 +66,17 @@
|
|
|
59
66
|
"@commitlint/config-conventional": "^16.2.1",
|
|
60
67
|
"@jest/globals": "^29.7.0",
|
|
61
68
|
"@types/aws-lambda": "^8.10.108",
|
|
69
|
+
"@types/bn.js": "^5.1.5",
|
|
62
70
|
"@types/express": "^4.17.14",
|
|
63
71
|
"@types/jest": "^29.5.8",
|
|
72
|
+
"@types/keccak": "^3.0.4",
|
|
64
73
|
"@types/node": "^17.0.23",
|
|
74
|
+
"@types/secp256k1": "^4.0.6",
|
|
65
75
|
"@typescript-eslint/eslint-plugin": "^6.4.0",
|
|
66
76
|
"@typescript-eslint/parser": "^5.17.0",
|
|
67
77
|
"aws-lambda": "^1.0.7",
|
|
68
78
|
"babel-jest": "^29.7.0",
|
|
79
|
+
"csv-parse": "^5.5.2",
|
|
69
80
|
"esbuild": "^0.19.3",
|
|
70
81
|
"eslint": "^8.0.1",
|
|
71
82
|
"eslint-config-prettier": "8.3.0",
|