@celo/wallet-hsm-azure 5.1.1 → 5.1.2-beta.0

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/.eslintrc.js ADDED
@@ -0,0 +1,3 @@
1
+ module.exports = {
2
+ extends: '../../../../.eslintrc.js',
3
+ }
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @celo/wallet-hsm-azure
2
2
 
3
+ ## 5.1.2-beta.0
4
+
5
+ ### Patch Changes
6
+
7
+ - [#94](https://github.com/celo-org/developer-tooling/pull/94) [`45d156d`](https://github.com/celo-org/developer-tooling/commit/45d156d03c03399bef51a00fe2b5cfb5e5669642) Thanks [@aaronmgdr](https://github.com/aaronmgdr)! - Bump all @azure packages to latest versions. These includes several major bumps which should not affect use in this library. For More details
8
+ - https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/CHANGELOG.md
9
+ - https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/BREAKING_CHANGES.md
10
+ - https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/keyvault/keyvault-secrets/CHANGELOG.md
11
+ - https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/keyvault/keyvault-keys/CHANGELOG.md
12
+ - Updated dependencies [[`6b2e34c`](https://github.com/celo-org/developer-tooling/commit/6b2e34c973290da221aaabdc2bf4c6654ef9f99c)]:
13
+ - @celo/utils@6.0.0-beta.0
14
+ - @celo/connect@5.1.2-beta.0
15
+ - @celo/wallet-base@5.1.2-beta.0
16
+ - @celo/wallet-remote@5.1.2-beta.0
17
+ - @celo/wallet-hsm@5.1.2-beta.0
18
+
3
19
  ## 5.1.1
4
20
 
5
21
  ### Patch Changes
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "exclude": []
4
+ }
@@ -31,130 +31,76 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
31
31
  step((generator = generator.apply(thisArg, _arguments || [])).next());
32
32
  });
33
33
  };
34
- var __generator = (this && this.__generator) || function (thisArg, body) {
35
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
36
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
37
- function verb(n) { return function (v) { return step([n, v]); }; }
38
- function step(op) {
39
- if (f) throw new TypeError("Generator is already executing.");
40
- while (_) try {
41
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
42
- if (y = 0, t) op = [op[0] & 2, t.value];
43
- switch (op[0]) {
44
- case 0: case 1: t = op; break;
45
- case 4: _.label++; return { value: op[1], done: false };
46
- case 5: _.label++; y = op[1]; op = [0]; continue;
47
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
48
- default:
49
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
50
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
51
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
52
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
53
- if (t[2]) _.ops.pop();
54
- _.trys.pop(); continue;
55
- }
56
- op = body.call(thisArg, _);
57
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
58
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
59
- }
60
- };
61
34
  Object.defineProperty(exports, "__esModule", { value: true });
62
35
  exports.AzureHSMSigner = void 0;
63
- var address_1 = require("@celo/base/lib/address");
64
- var sign_typed_data_utils_1 = require("@celo/utils/lib/sign-typed-data-utils");
65
- var wallet_base_1 = require("@celo/wallet-base");
66
- var ethUtil = __importStar(require("@ethereumjs/util"));
36
+ const address_1 = require("@celo/base/lib/address");
37
+ const sign_typed_data_utils_1 = require("@celo/utils/lib/sign-typed-data-utils");
38
+ const wallet_base_1 = require("@celo/wallet-base");
39
+ const ethUtil = __importStar(require("@ethereumjs/util"));
67
40
  /**
68
41
  * Signs the EVM transaction using an HSM key in Azure Key Vault
69
42
  */
70
- var AzureHSMSigner = /** @class */ (function () {
71
- function AzureHSMSigner(keyVaultClient, keyName) {
43
+ class AzureHSMSigner {
44
+ constructor(keyVaultClient, keyName) {
72
45
  if (!AzureHSMSigner.keyVaultClient) {
73
46
  AzureHSMSigner.keyVaultClient = keyVaultClient;
74
47
  }
75
48
  this.keyName = keyName;
76
49
  }
77
- AzureHSMSigner.prototype.signTransaction = function (addToV, encodedTx) {
78
- return __awaiter(this, void 0, void 0, function () {
79
- var hash, bufferedMessage, signature, sigV;
80
- return __generator(this, function (_a) {
81
- switch (_a.label) {
82
- case 0:
83
- hash = (0, wallet_base_1.getHashFromEncoded)(encodedTx.rlpEncode);
84
- bufferedMessage = Buffer.from((0, address_1.trimLeading0x)(hash), 'hex');
85
- return [4 /*yield*/, AzureHSMSigner.keyVaultClient.signMessage(bufferedMessage, this.keyName)];
86
- case 1:
87
- signature = _a.sent();
88
- sigV = addToV + signature.v;
89
- return [2 /*return*/, {
90
- v: sigV,
91
- r: signature.r,
92
- s: signature.s,
93
- }];
94
- }
95
- });
50
+ signTransaction(addToV, encodedTx) {
51
+ return __awaiter(this, void 0, void 0, function* () {
52
+ const hash = (0, wallet_base_1.getHashFromEncoded)(encodedTx.rlpEncode);
53
+ const bufferedMessage = Buffer.from((0, address_1.trimLeading0x)(hash), 'hex');
54
+ const signature = yield AzureHSMSigner.keyVaultClient.signMessage(bufferedMessage, this.keyName);
55
+ const sigV = addToV + signature.v;
56
+ return {
57
+ v: sigV,
58
+ r: signature.r,
59
+ s: signature.s,
60
+ };
96
61
  });
97
- };
98
- AzureHSMSigner.prototype.signPersonalMessage = function (data) {
99
- return __awaiter(this, void 0, void 0, function () {
100
- var dataBuff, msgHashBuff, signature, sigV;
101
- return __generator(this, function (_a) {
102
- switch (_a.label) {
103
- case 0:
104
- dataBuff = ethUtil.toBuffer((0, address_1.ensureLeading0x)(data));
105
- msgHashBuff = ethUtil.hashPersonalMessage(dataBuff);
106
- return [4 /*yield*/, AzureHSMSigner.keyVaultClient.signMessage(Buffer.from(msgHashBuff), this.keyName)
107
- // Recovery ID should be a byte prefix
108
- // https://bitcoin.stackexchange.com/questions/38351/ecdsa-v-r-s-what-is-v
109
- ];
110
- case 1:
111
- signature = _a.sent();
112
- sigV = signature.v + 27;
113
- return [2 /*return*/, {
114
- v: sigV,
115
- r: signature.r,
116
- s: signature.s,
117
- }];
118
- }
119
- });
62
+ }
63
+ signPersonalMessage(data) {
64
+ return __awaiter(this, void 0, void 0, function* () {
65
+ const dataBuff = ethUtil.toBuffer((0, address_1.ensureLeading0x)(data));
66
+ const msgHashBuff = ethUtil.hashPersonalMessage(dataBuff);
67
+ const signature = yield AzureHSMSigner.keyVaultClient.signMessage(Buffer.from(msgHashBuff), this.keyName);
68
+ // Recovery ID should be a byte prefix
69
+ // https://bitcoin.stackexchange.com/questions/38351/ecdsa-v-r-s-what-is-v
70
+ const sigV = signature.v + 27;
71
+ return {
72
+ v: sigV,
73
+ r: signature.r,
74
+ s: signature.s,
75
+ };
120
76
  });
121
- };
122
- AzureHSMSigner.prototype.signTypedData = function (typedData) {
123
- return __awaiter(this, void 0, void 0, function () {
124
- var dataBuff, signature, sigV;
125
- return __generator(this, function (_a) {
126
- switch (_a.label) {
127
- case 0:
128
- dataBuff = (0, sign_typed_data_utils_1.generateTypedDataHash)(typedData);
129
- return [4 /*yield*/, AzureHSMSigner.keyVaultClient.signMessage(dataBuff, this.keyName)
130
- // Recovery ID should be a byte prefix
131
- // https://bitcoin.stackexchange.com/questions/38351/ecdsa-v-r-s-what-is-v
132
- ];
133
- case 1:
134
- signature = _a.sent();
135
- sigV = signature.v + 27;
136
- return [2 /*return*/, {
137
- v: sigV,
138
- r: signature.r,
139
- s: signature.s,
140
- }];
141
- }
142
- });
77
+ }
78
+ signTypedData(typedData) {
79
+ return __awaiter(this, void 0, void 0, function* () {
80
+ const dataBuff = (0, sign_typed_data_utils_1.generateTypedDataHash)(typedData);
81
+ const signature = yield AzureHSMSigner.keyVaultClient.signMessage(dataBuff, this.keyName);
82
+ // Recovery ID should be a byte prefix
83
+ // https://bitcoin.stackexchange.com/questions/38351/ecdsa-v-r-s-what-is-v
84
+ const sigV = signature.v + 27;
85
+ return {
86
+ v: sigV,
87
+ r: signature.r,
88
+ s: signature.s,
89
+ };
143
90
  });
144
- };
145
- AzureHSMSigner.prototype.getNativeKey = function () {
91
+ }
92
+ getNativeKey() {
146
93
  return this.keyName;
147
- };
148
- AzureHSMSigner.prototype.decrypt = function (_ciphertext) {
94
+ }
95
+ decrypt(_ciphertext) {
149
96
  throw new Error('Decryption operation is not supported on this signer');
150
97
  // To make the compiler happy
151
98
  return Promise.resolve(_ciphertext);
152
- };
153
- AzureHSMSigner.prototype.computeSharedSecret = function (_publicKey) {
99
+ }
100
+ computeSharedSecret(_publicKey) {
154
101
  throw new Error('Not implemented');
155
102
  return Promise.resolve(Buffer.from([]));
156
- };
157
- return AzureHSMSigner;
158
- }());
103
+ }
104
+ }
159
105
  exports.AzureHSMSigner = AzureHSMSigner;
160
106
  //# sourceMappingURL=azure-hsm-signer.js.map
@@ -1,19 +1,4 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- if (typeof b !== "function" && b !== null)
11
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
12
- extendStatics(d, b);
13
- function __() { this.constructor = d; }
14
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
15
- };
16
- })();
17
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
18
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
19
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -23,123 +8,66 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
23
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
24
9
  });
25
10
  };
26
- var __generator = (this && this.__generator) || function (thisArg, body) {
27
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
28
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
29
- function verb(n) { return function (v) { return step([n, v]); }; }
30
- function step(op) {
31
- if (f) throw new TypeError("Generator is already executing.");
32
- while (_) try {
33
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
34
- if (y = 0, t) op = [op[0] & 2, t.value];
35
- switch (op[0]) {
36
- case 0: case 1: t = op; break;
37
- case 4: _.label++; return { value: op[1], done: false };
38
- case 5: _.label++; y = op[1]; op = [0]; continue;
39
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
40
- default:
41
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
42
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
43
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
44
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
45
- if (t[2]) _.ops.pop();
46
- _.trys.pop(); continue;
47
- }
48
- op = body.call(thisArg, _);
49
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
50
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
51
- }
52
- };
53
11
  var __importDefault = (this && this.__importDefault) || function (mod) {
54
12
  return (mod && mod.__esModule) ? mod : { "default": mod };
55
13
  };
56
14
  Object.defineProperty(exports, "__esModule", { value: true });
57
15
  exports.AzureHSMWallet = void 0;
58
- var address_1 = require("@celo/utils/lib/address");
59
- var wallet_remote_1 = require("@celo/wallet-remote");
60
- var debug_1 = __importDefault(require("debug"));
61
- var azure_hsm_signer_1 = require("./azure-hsm-signer");
62
- var azure_key_vault_client_1 = require("./azure-key-vault-client");
63
- var debug = (0, debug_1.default)('kit:wallet:aws-hsm-wallet');
16
+ const address_1 = require("@celo/utils/lib/address");
17
+ const wallet_remote_1 = require("@celo/wallet-remote");
18
+ const debug_1 = __importDefault(require("debug"));
19
+ const azure_hsm_signer_1 = require("./azure-hsm-signer");
20
+ const azure_key_vault_client_1 = require("./azure-key-vault-client");
21
+ const debug = (0, debug_1.default)('kit:wallet:aws-hsm-wallet');
64
22
  // Azure Key Vault implementation of a RemoteWallet
65
- var AzureHSMWallet = /** @class */ (function (_super) {
66
- __extends(AzureHSMWallet, _super);
67
- function AzureHSMWallet(vaultName) {
68
- var _this = _super.call(this) || this;
69
- _this.vaultName = vaultName;
70
- return _this;
23
+ class AzureHSMWallet extends wallet_remote_1.RemoteWallet {
24
+ constructor(vaultName) {
25
+ super();
26
+ this.vaultName = vaultName;
71
27
  }
72
- AzureHSMWallet.prototype.loadAccountSigners = function () {
73
- return __awaiter(this, void 0, void 0, function () {
74
- var keys, addressToSigner, _i, keys_1, key, address, e_1;
75
- return __generator(this, function (_a) {
76
- switch (_a.label) {
77
- case 0:
78
- if (!this.keyVaultClient) {
79
- this.keyVaultClient = this.generateNewKeyVaultClient(this.vaultName);
80
- }
81
- return [4 /*yield*/, this.keyVaultClient.getKeys()];
82
- case 1:
83
- keys = _a.sent();
84
- addressToSigner = new Map();
85
- _i = 0, keys_1 = keys;
86
- _a.label = 2;
87
- case 2:
88
- if (!(_i < keys_1.length)) return [3 /*break*/, 7];
89
- key = keys_1[_i];
90
- _a.label = 3;
91
- case 3:
92
- _a.trys.push([3, 5, , 6]);
93
- return [4 /*yield*/, this.getAddressFromKeyName(key)];
94
- case 4:
95
- address = _a.sent();
96
- addressToSigner.set(address, new azure_hsm_signer_1.AzureHSMSigner(this.keyVaultClient, key));
97
- return [3 /*break*/, 6];
98
- case 5:
99
- e_1 = _a.sent();
100
- // Safely ignore non-secp256k1 keys
101
- if (!e_1.message.includes('Invalid secp256k1')) {
102
- throw e_1;
103
- }
104
- else {
105
- debug("Ignoring non-secp256k1 key ".concat(key));
106
- }
107
- return [3 /*break*/, 6];
108
- case 6:
109
- _i++;
110
- return [3 /*break*/, 2];
111
- case 7: return [2 /*return*/, addressToSigner];
28
+ loadAccountSigners() {
29
+ return __awaiter(this, void 0, void 0, function* () {
30
+ if (!this.keyVaultClient) {
31
+ this.keyVaultClient = this.generateNewKeyVaultClient(this.vaultName);
32
+ }
33
+ const keys = yield this.keyVaultClient.getKeys();
34
+ const addressToSigner = new Map();
35
+ for (const key of keys) {
36
+ try {
37
+ const address = yield this.getAddressFromKeyName(key);
38
+ addressToSigner.set(address, new azure_hsm_signer_1.AzureHSMSigner(this.keyVaultClient, key));
39
+ }
40
+ catch (e) {
41
+ // Safely ignore non-secp256k1 keys
42
+ if (!e.message.includes('Invalid secp256k1')) {
43
+ throw e;
44
+ }
45
+ else {
46
+ debug(`Ignoring non-secp256k1 key ${key}`);
47
+ }
112
48
  }
113
- });
49
+ }
50
+ return addressToSigner;
114
51
  });
115
- };
52
+ }
116
53
  // Extracted for testing purpose
117
- AzureHSMWallet.prototype.generateNewKeyVaultClient = function (vaultName) {
54
+ generateNewKeyVaultClient(vaultName) {
118
55
  return new azure_key_vault_client_1.AzureKeyVaultClient(vaultName);
119
- };
56
+ }
120
57
  /**
121
58
  * Returns the EVM address for the given key
122
59
  * Useful for initially getting the 'from' field given a keyName
123
60
  * @param keyName Azure KeyVault key name
124
61
  */
125
- AzureHSMWallet.prototype.getAddressFromKeyName = function (keyName) {
126
- return __awaiter(this, void 0, void 0, function () {
127
- var publicKey;
128
- return __generator(this, function (_a) {
129
- switch (_a.label) {
130
- case 0:
131
- if (!this.keyVaultClient) {
132
- throw new Error('AzureHSMWallet needs to be initialized first');
133
- }
134
- return [4 /*yield*/, this.keyVaultClient.getPublicKey(keyName)];
135
- case 1:
136
- publicKey = _a.sent();
137
- return [2 /*return*/, (0, address_1.publicKeyToAddress)(publicKey.toString(16))];
138
- }
139
- });
62
+ getAddressFromKeyName(keyName) {
63
+ return __awaiter(this, void 0, void 0, function* () {
64
+ if (!this.keyVaultClient) {
65
+ throw new Error('AzureHSMWallet needs to be initialized first');
66
+ }
67
+ const publicKey = yield this.keyVaultClient.getPublicKey(keyName);
68
+ return (0, address_1.publicKeyToAddress)(publicKey.toString(16));
140
69
  });
141
- };
142
- return AzureHSMWallet;
143
- }(wallet_remote_1.RemoteWallet));
70
+ }
71
+ }
144
72
  exports.AzureHSMWallet = AzureHSMWallet;
145
73
  //# sourceMappingURL=azure-hsm-wallet.js.map
@@ -8,33 +8,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
9
  });
10
10
  };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
11
  var __asyncValues = (this && this.__asyncValues) || function (o) {
39
12
  if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
40
13
  var m = o[Symbol.asyncIterator], i;
@@ -44,266 +17,173 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
44
17
  };
45
18
  Object.defineProperty(exports, "__esModule", { value: true });
46
19
  exports.AzureKeyVaultClient = void 0;
47
- var identity_1 = require("@azure/identity");
48
- var keyvault_keys_1 = require("@azure/keyvault-keys");
49
- var keyvault_secrets_1 = require("@azure/keyvault-secrets");
50
- var wallet_hsm_1 = require("@celo/wallet-hsm");
51
- var SUPPORTED_CURVES = ['P-256K', 'SECP256K1'];
52
- var SIGNING_ALGORITHM_FOR = {
20
+ const identity_1 = require("@azure/identity");
21
+ const keyvault_keys_1 = require("@azure/keyvault-keys");
22
+ const keyvault_secrets_1 = require("@azure/keyvault-secrets");
23
+ const wallet_hsm_1 = require("@celo/wallet-hsm");
24
+ const SUPPORTED_CURVES = ['P-256K', 'SECP256K1'];
25
+ const SIGNING_ALGORITHM_FOR = {
53
26
  'P-256K': 'ES256K',
54
27
  SECP256K1: 'ECDSA256',
55
28
  };
56
- var isSupportedCurve = function (curveName) {
29
+ const isSupportedCurve = (curveName) => {
57
30
  return SUPPORTED_CURVES.indexOf(curveName) > -1;
58
31
  };
59
32
  /**
60
33
  * Provides an abstraction on Azure Key Vault for performing signing operations
61
34
  */
62
- var AzureKeyVaultClient = /** @class */ (function () {
63
- function AzureKeyVaultClient(vaultName, credential) {
35
+ class AzureKeyVaultClient {
36
+ constructor(vaultName, credential) {
64
37
  this.cryptographyClientSet = new Map();
65
38
  this.vaultName = vaultName;
66
- this.vaultUri = "https://".concat(this.vaultName, ".vault.azure.net");
39
+ this.vaultUri = `https://${this.vaultName}.vault.azure.net`;
67
40
  // DefaultAzureCredential supports service principal or managed identity
68
41
  // If using a service principal, you must set the appropriate environment vars
69
42
  this.credential = credential || new identity_1.DefaultAzureCredential();
70
43
  this.keyClient = new keyvault_keys_1.KeyClient(this.vaultUri, this.credential);
71
44
  this.secretClient = new keyvault_secrets_1.SecretClient(this.vaultUri, this.credential);
72
45
  }
73
- AzureKeyVaultClient.prototype.getKeys = function () {
74
- var e_1, _a;
75
- return __awaiter(this, void 0, void 0, function () {
76
- var keyNames, _b, _c, keyProperties, e_1_1;
77
- return __generator(this, function (_d) {
78
- switch (_d.label) {
79
- case 0:
80
- keyNames = new Array();
81
- _d.label = 1;
82
- case 1:
83
- _d.trys.push([1, 6, 7, 12]);
84
- _b = __asyncValues(this.keyClient.listPropertiesOfKeys());
85
- _d.label = 2;
86
- case 2: return [4 /*yield*/, _b.next()];
87
- case 3:
88
- if (!(_c = _d.sent(), !_c.done)) return [3 /*break*/, 5];
89
- keyProperties = _c.value;
90
- keyNames.push(keyProperties.name);
91
- _d.label = 4;
92
- case 4: return [3 /*break*/, 2];
93
- case 5: return [3 /*break*/, 12];
94
- case 6:
95
- e_1_1 = _d.sent();
96
- e_1 = { error: e_1_1 };
97
- return [3 /*break*/, 12];
98
- case 7:
99
- _d.trys.push([7, , 10, 11]);
100
- if (!(_c && !_c.done && (_a = _b.return))) return [3 /*break*/, 9];
101
- return [4 /*yield*/, _a.call(_b)];
102
- case 8:
103
- _d.sent();
104
- _d.label = 9;
105
- case 9: return [3 /*break*/, 11];
106
- case 10:
107
- if (e_1) throw e_1.error;
108
- return [7 /*endfinally*/];
109
- case 11: return [7 /*endfinally*/];
110
- case 12: return [2 /*return*/, keyNames];
46
+ getKeys() {
47
+ var _a, e_1, _b, _c;
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ const keyNames = new Array();
50
+ try {
51
+ for (var _d = true, _e = __asyncValues(this.keyClient.listPropertiesOfKeys()), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
52
+ _c = _f.value;
53
+ _d = false;
54
+ const keyProperties = _c;
55
+ keyNames.push(keyProperties.name);
111
56
  }
112
- });
113
- });
114
- };
115
- AzureKeyVaultClient.prototype.getPublicKey = function (keyName) {
116
- return __awaiter(this, void 0, void 0, function () {
117
- var signingKey, pubKeyPrefix, rawPublicKey, publicKey;
118
- return __generator(this, function (_a) {
119
- switch (_a.label) {
120
- case 0: return [4 /*yield*/, this.getKey(keyName)];
121
- case 1:
122
- signingKey = _a.sent();
123
- pubKeyPrefix = Buffer.from(new Uint8Array([wallet_hsm_1.publicKeyPrefix]));
124
- rawPublicKey = Buffer.concat([
125
- pubKeyPrefix,
126
- Buffer.from(signingKey.key.x),
127
- Buffer.from(signingKey.key.y),
128
- ]);
129
- publicKey = (0, wallet_hsm_1.bufferToBigNumber)(rawPublicKey);
130
- return [2 /*return*/, publicKey];
57
+ }
58
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
59
+ finally {
60
+ try {
61
+ if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
131
62
  }
132
- });
63
+ finally { if (e_1) throw e_1.error; }
64
+ }
65
+ return keyNames;
133
66
  });
134
- };
135
- AzureKeyVaultClient.prototype.getKeyId = function (keyName) {
136
- return __awaiter(this, void 0, void 0, function () {
137
- return __generator(this, function (_a) {
138
- switch (_a.label) {
139
- case 0: return [4 /*yield*/, this.hasKey(keyName)];
140
- case 1:
141
- if (!(_a.sent())) {
142
- throw new Error("Unable to locate key: ".concat(keyName));
143
- }
144
- return [4 /*yield*/, this.getKey(keyName)];
145
- case 2: return [2 /*return*/, (_a.sent()).id];
146
- }
147
- });
67
+ }
68
+ getPublicKey(keyName) {
69
+ return __awaiter(this, void 0, void 0, function* () {
70
+ const signingKey = yield this.getKey(keyName);
71
+ const pubKeyPrefix = Buffer.from(new Uint8Array([wallet_hsm_1.publicKeyPrefix]));
72
+ const rawPublicKey = Buffer.concat([
73
+ pubKeyPrefix,
74
+ Buffer.from(signingKey.key.x),
75
+ Buffer.from(signingKey.key.y),
76
+ ]);
77
+ const publicKey = (0, wallet_hsm_1.bufferToBigNumber)(rawPublicKey);
78
+ return publicKey;
148
79
  });
149
- };
150
- AzureKeyVaultClient.prototype.signMessage = function (message, keyName) {
151
- return __awaiter(this, void 0, void 0, function () {
152
- var curve, signingAlgorithm, cryptographyClient, signResult, rawSignature, R, S, rBuff, sBuff, canonicalizedSignature, publicKey, recoveryParam;
153
- return __generator(this, function (_a) {
154
- switch (_a.label) {
155
- case 0: return [4 /*yield*/, this.hasKey(keyName)];
156
- case 1:
157
- if (!(_a.sent())) {
158
- throw new Error("Unable to locate key: ".concat(keyName));
159
- }
160
- return [4 /*yield*/, this.getKeyCurve(keyName)];
161
- case 2:
162
- curve = _a.sent();
163
- if (!isSupportedCurve(curve)) {
164
- throw new Error("Key curve ".concat(curve, " is not supported, must be one of: ").concat(SUPPORTED_CURVES));
165
- }
166
- signingAlgorithm = SIGNING_ALGORITHM_FOR[curve];
167
- return [4 /*yield*/, this.getCryptographyClient(keyName)];
168
- case 3:
169
- cryptographyClient = _a.sent();
170
- return [4 /*yield*/, cryptographyClient.sign(
171
- // @ts-ignore-next-line (ECDSA256 is not included in the client enum but is valid)
172
- signingAlgorithm, new Uint8Array(message))
173
- // The output of this will be a 64 byte array.
174
- // The first 32 are the value for R and the rest is S
175
- ];
176
- case 4:
177
- signResult = _a.sent();
178
- // The output of this will be a 64 byte array.
179
- // The first 32 are the value for R and the rest is S
180
- if (typeof signResult === 'undefined' ||
181
- typeof signResult.result === 'undefined' ||
182
- signResult.result.length !== wallet_hsm_1.sixtyFour) {
183
- throw new Error("Invalid signature returned from Azure: ".concat(signResult));
184
- }
185
- rawSignature = signResult.result;
186
- R = (0, wallet_hsm_1.bufferToBigNumber)(Buffer.from(rawSignature.slice(0, wallet_hsm_1.thirtyTwo)));
187
- S = (0, wallet_hsm_1.bufferToBigNumber)(Buffer.from(rawSignature.slice(wallet_hsm_1.thirtyTwo, wallet_hsm_1.sixtyFour)));
188
- S = (0, wallet_hsm_1.makeCanonical)(S);
189
- rBuff = (0, wallet_hsm_1.bigNumberToBuffer)(R, wallet_hsm_1.thirtyTwo);
190
- sBuff = (0, wallet_hsm_1.bigNumberToBuffer)(S, wallet_hsm_1.thirtyTwo);
191
- canonicalizedSignature = Buffer.concat([rBuff, sBuff]);
192
- return [4 /*yield*/, this.getPublicKey(keyName)
193
- // Azure doesn't provide the recovery key in the signature
194
- ];
195
- case 5:
196
- publicKey = _a.sent();
197
- recoveryParam = (0, wallet_hsm_1.recoverKeyIndex)(canonicalizedSignature, publicKey, message);
198
- return [2 /*return*/, new wallet_hsm_1.Signature(recoveryParam, rBuff, sBuff)];
199
- }
200
- });
80
+ }
81
+ getKeyId(keyName) {
82
+ return __awaiter(this, void 0, void 0, function* () {
83
+ if (!(yield this.hasKey(keyName))) {
84
+ throw new Error(`Unable to locate key: ${keyName}`);
85
+ }
86
+ return (yield this.getKey(keyName)).id;
201
87
  });
202
- };
203
- AzureKeyVaultClient.prototype.hasKey = function (keyName) {
204
- return __awaiter(this, void 0, void 0, function () {
205
- var e_2;
206
- return __generator(this, function (_a) {
207
- switch (_a.label) {
208
- case 0:
209
- _a.trys.push([0, 2, , 3]);
210
- return [4 /*yield*/, this.keyClient.getKey(keyName)];
211
- case 1:
212
- _a.sent();
213
- return [3 /*break*/, 3];
214
- case 2:
215
- e_2 = _a.sent();
216
- if (e_2.message.includes('this is not a valid private key')) {
217
- return [2 /*return*/, false];
218
- }
219
- throw e_2;
220
- case 3: return [2 /*return*/, true];
221
- }
222
- });
88
+ }
89
+ signMessage(message, keyName) {
90
+ return __awaiter(this, void 0, void 0, function* () {
91
+ if (!(yield this.hasKey(keyName))) {
92
+ throw new Error(`Unable to locate key: ${keyName}`);
93
+ }
94
+ const curve = yield this.getKeyCurve(keyName);
95
+ if (!isSupportedCurve(curve)) {
96
+ throw new Error(`Key curve ${curve} is not supported, must be one of: ${SUPPORTED_CURVES}`);
97
+ }
98
+ const signingAlgorithm = SIGNING_ALGORITHM_FOR[curve];
99
+ const cryptographyClient = yield this.getCryptographyClient(keyName);
100
+ const signResult = yield cryptographyClient.sign(signingAlgorithm, new Uint8Array(message));
101
+ // The output of this will be a 64 byte array.
102
+ // The first 32 are the value for R and the rest is S
103
+ if (typeof signResult === 'undefined' ||
104
+ typeof signResult.result === 'undefined' ||
105
+ signResult.result.length !== wallet_hsm_1.sixtyFour) {
106
+ throw new Error(`Invalid signature returned from Azure: ${signResult}`);
107
+ }
108
+ const rawSignature = signResult.result;
109
+ // Canonicalize signature
110
+ const R = (0, wallet_hsm_1.bufferToBigNumber)(Buffer.from(rawSignature.slice(0, wallet_hsm_1.thirtyTwo)));
111
+ let S = (0, wallet_hsm_1.bufferToBigNumber)(Buffer.from(rawSignature.slice(wallet_hsm_1.thirtyTwo, wallet_hsm_1.sixtyFour)));
112
+ S = (0, wallet_hsm_1.makeCanonical)(S);
113
+ const rBuff = (0, wallet_hsm_1.bigNumberToBuffer)(R, wallet_hsm_1.thirtyTwo);
114
+ const sBuff = (0, wallet_hsm_1.bigNumberToBuffer)(S, wallet_hsm_1.thirtyTwo);
115
+ const canonicalizedSignature = Buffer.concat([rBuff, sBuff]);
116
+ const publicKey = yield this.getPublicKey(keyName);
117
+ // Azure doesn't provide the recovery key in the signature
118
+ const recoveryParam = (0, wallet_hsm_1.recoverKeyIndex)(canonicalizedSignature, publicKey, message);
119
+ return new wallet_hsm_1.Signature(recoveryParam, rBuff, sBuff);
223
120
  });
224
- };
225
- AzureKeyVaultClient.prototype.getSecret = function (secretName) {
226
- return __awaiter(this, void 0, void 0, function () {
227
- var secret;
228
- return __generator(this, function (_a) {
229
- switch (_a.label) {
230
- case 0: return [4 /*yield*/, this.secretClient.getSecret(secretName)];
231
- case 1:
232
- secret = _a.sent();
233
- if (!secret.value) {
234
- throw new Error("Could not locate secret ".concat(secretName, " in vault ").concat(this.vaultName));
235
- }
236
- return [2 /*return*/, secret.value];
121
+ }
122
+ hasKey(keyName) {
123
+ return __awaiter(this, void 0, void 0, function* () {
124
+ try {
125
+ yield this.keyClient.getKey(keyName);
126
+ }
127
+ catch (e) {
128
+ if (e.message.includes('this is not a valid private key')) {
129
+ return false;
237
130
  }
238
- });
131
+ throw e;
132
+ }
133
+ return true;
134
+ });
135
+ }
136
+ getSecret(secretName) {
137
+ return __awaiter(this, void 0, void 0, function* () {
138
+ const secret = yield this.secretClient.getSecret(secretName);
139
+ if (!secret.value) {
140
+ throw new Error(`Could not locate secret ${secretName} in vault ${this.vaultName}`);
141
+ }
142
+ return secret.value;
239
143
  });
240
- };
241
- AzureKeyVaultClient.prototype.getKey = function (keyName) {
144
+ }
145
+ getKey(keyName) {
242
146
  var _a, _b;
243
- return __awaiter(this, void 0, void 0, function () {
244
- var signingKey, e_3;
245
- return __generator(this, function (_c) {
246
- switch (_c.label) {
247
- case 0:
248
- _c.trys.push([0, 2, , 3]);
249
- return [4 /*yield*/, this.keyClient.getKey(keyName)];
250
- case 1:
251
- signingKey = _c.sent();
252
- if (typeof (signingKey === null || signingKey === void 0 ? void 0 : signingKey.id) === 'undefined' ||
253
- typeof ((_a = signingKey === null || signingKey === void 0 ? void 0 : signingKey.key) === null || _a === void 0 ? void 0 : _a.x) === 'undefined' ||
254
- typeof ((_b = signingKey === null || signingKey === void 0 ? void 0 : signingKey.key) === null || _b === void 0 ? void 0 : _b.y) === 'undefined') {
255
- throw new Error("Invalid key data returned from Azure: ".concat(signingKey));
256
- }
257
- return [2 /*return*/, signingKey];
258
- case 2:
259
- e_3 = _c.sent();
260
- if (e_3.message.includes('Key not found')) {
261
- throw new Error("Key ".concat(keyName, " not found in KeyVault ").concat(this.vaultName));
262
- }
263
- throw new Error("Unexpected KeyVault error ".concat(e_3.message));
264
- case 3: return [2 /*return*/];
147
+ return __awaiter(this, void 0, void 0, function* () {
148
+ try {
149
+ const signingKey = yield this.keyClient.getKey(keyName);
150
+ if (typeof (signingKey === null || signingKey === void 0 ? void 0 : signingKey.id) === 'undefined' ||
151
+ typeof ((_a = signingKey === null || signingKey === void 0 ? void 0 : signingKey.key) === null || _a === void 0 ? void 0 : _a.x) === 'undefined' ||
152
+ typeof ((_b = signingKey === null || signingKey === void 0 ? void 0 : signingKey.key) === null || _b === void 0 ? void 0 : _b.y) === 'undefined') {
153
+ throw new Error(`Invalid key data returned from Azure: ${signingKey}`);
265
154
  }
266
- });
267
- });
268
- };
269
- AzureKeyVaultClient.prototype.getKeyCurve = function (keyName) {
270
- return __awaiter(this, void 0, void 0, function () {
271
- var key;
272
- return __generator(this, function (_a) {
273
- switch (_a.label) {
274
- case 0: return [4 /*yield*/, this.getKey(keyName)];
275
- case 1:
276
- key = _a.sent();
277
- if (key.key === undefined || key.key.crv === undefined) {
278
- throw new Error("Key or curve is undefined");
279
- }
280
- return [2 /*return*/, key.key.crv];
155
+ return signingKey;
156
+ }
157
+ catch (e) {
158
+ if (e.message.includes('Key not found')) {
159
+ throw new Error(`Key ${keyName} not found in KeyVault ${this.vaultName}`);
281
160
  }
282
- });
161
+ throw new Error(`Unexpected KeyVault error ${e.message}`);
162
+ }
163
+ });
164
+ }
165
+ getKeyCurve(keyName) {
166
+ return __awaiter(this, void 0, void 0, function* () {
167
+ const key = yield this.getKey(keyName);
168
+ if (key.key === undefined || key.key.crv === undefined) {
169
+ throw new Error(`Key or curve is undefined`);
170
+ }
171
+ return key.key.crv;
283
172
  });
284
- };
173
+ }
285
174
  /**
286
175
  * Provides the CryptographyClient for the requested key
287
176
  * Creates a new client if it doesn't already exist
288
177
  */
289
- AzureKeyVaultClient.prototype.getCryptographyClient = function (keyName) {
290
- return __awaiter(this, void 0, void 0, function () {
291
- var keyId;
292
- return __generator(this, function (_a) {
293
- switch (_a.label) {
294
- case 0:
295
- if (!!this.cryptographyClientSet.has(keyName)) return [3 /*break*/, 2];
296
- return [4 /*yield*/, this.getKeyId(keyName)];
297
- case 1:
298
- keyId = _a.sent();
299
- this.cryptographyClientSet.set(keyName, new keyvault_keys_1.CryptographyClient(keyId, this.credential));
300
- _a.label = 2;
301
- case 2: return [2 /*return*/, this.cryptographyClientSet.get(keyName)];
302
- }
303
- });
178
+ getCryptographyClient(keyName) {
179
+ return __awaiter(this, void 0, void 0, function* () {
180
+ if (!this.cryptographyClientSet.has(keyName)) {
181
+ const keyId = yield this.getKeyId(keyName);
182
+ this.cryptographyClientSet.set(keyName, new keyvault_keys_1.CryptographyClient(keyId, this.credential));
183
+ }
184
+ return this.cryptographyClientSet.get(keyName);
304
185
  });
305
- };
306
- return AzureKeyVaultClient;
307
- }());
186
+ }
187
+ }
308
188
  exports.AzureKeyVaultClient = AzureKeyVaultClient;
309
189
  //# sourceMappingURL=azure-key-vault-client.js.map
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@celo/wallet-hsm-azure",
3
- "version": "5.1.1",
3
+ "version": "5.1.2-beta.0",
4
4
  "description": "Azure HSM wallet implementation",
5
5
  "author": "Celo",
6
6
  "license": "Apache-2.0",
7
- "repository": "https://github.com/celo-org/celo-monorepo/tree/master/packages/sdk/wallets/",
7
+ "repository": "https://github.com/celo-org/developer-tooling/tree/master/packages/sdk/wallets/",
8
8
  "homepage": "https://docs.celo.org",
9
9
  "main": "./lib/index.js",
10
10
  "types": "./lib/index.d.ts",
@@ -14,28 +14,28 @@
14
14
  "sdk"
15
15
  ],
16
16
  "scripts": {
17
- "build": "tsc -b .",
18
- "clean": "tsc -b . --clean",
19
- "docs": "typedoc",
20
- "test": "jest --runInBand",
21
- "lint": "tslint -c tslint.json --project .",
17
+ "build": "yarn run --top-level tsc -b .",
18
+ "clean": "yarn run --top-level tsc -b . --clean",
19
+ "docs": "yarn run --top-level typedoc",
20
+ "test": "yarn run --top-level jest --runInBand",
21
+ "lint": "yarn run --top-level eslint -c .eslintrc.js ",
22
22
  "prepublishOnly": "yarn build"
23
23
  },
24
24
  "dependencies": {
25
- "@azure/identity": "^1.1.0",
26
- "@azure/keyvault-keys": "^4.1.0",
27
- "@azure/keyvault-secrets": "^4.1.0",
25
+ "@azure/identity": "^4.0.1",
26
+ "@azure/keyvault-keys": "^4.7.2",
27
+ "@azure/keyvault-secrets": "^4.7.0",
28
28
  "@celo/base": "^6.0.0",
29
- "@celo/utils": "^5.0.6",
30
- "@celo/wallet-base": "^5.1.1",
31
- "@celo/wallet-remote": "^5.1.1",
32
- "@celo/wallet-hsm": "^5.1.1",
33
- "@celo/connect": "^5.1.1",
34
- "@types/secp256k1": "^4.0.0",
35
- "eth-lib": "^0.2.8",
29
+ "@celo/connect": "^5.1.2-beta.0",
30
+ "@celo/utils": "^6.0.0-beta.0",
31
+ "@celo/wallet-base": "^5.1.2-beta.0",
32
+ "@celo/wallet-hsm": "^5.1.2-beta.0",
33
+ "@celo/wallet-remote": "^5.1.2-beta.0",
36
34
  "@ethereumjs/util": "8.0.5",
35
+ "@types/secp256k1": "^4.0.0",
37
36
  "bignumber.js": "^9.0.0",
38
37
  "debug": "^4.1.1",
38
+ "eth-lib": "^0.2.8",
39
39
  "secp256k1": "^4.0.0"
40
40
  },
41
41
  "devDependencies": {
@@ -46,4 +46,4 @@
46
46
  "engines": {
47
47
  "node": ">=8.14.2"
48
48
  }
49
- }
49
+ }
package/typedoc.json CHANGED
@@ -1,13 +1,19 @@
1
1
  {
2
- "mode": "modules",
3
- "exclude": ["**/*+(index|.test).ts"],
4
- "excludeNotExported": true,
5
- "excludePrivate": true,
6
- "excludeProtected": true,
7
- "includeDeclarations": false,
8
- "ignoreCompilerErrors": true,
9
- "hideGenerator": "true",
10
- "out": "../../../docs/sdk/docs/wallet-hsm-azure",
11
- "gitRevision": "master",
12
- "readme": "none"
13
- }
2
+ "exclude": [
3
+ "**/*+(index|.test).ts"
4
+ ],
5
+ "excludePrivate": true,
6
+ "excludeProtected": true,
7
+ "hideGenerator": true,
8
+ "out": "../../../docs/sdk/docs/wallet-hsm-azure",
9
+ "gitRevision": "master",
10
+ "readme": "none",
11
+ "entryPoints": [
12
+ "./src"
13
+ ],
14
+ "githubPages": false,
15
+ "plugin": [
16
+ "typedoc-plugin-markdown"
17
+ ],
18
+ "entryPointStrategy": "expand"
19
+ }