@celo/wallet-base
Version:
Wallet base implementation
165 lines • 7.11 kB
JavaScript
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.WalletBase = void 0;
const address_1 = require("@celo/base/lib/address");
const ethUtil = __importStar(require("@ethereumjs/util"));
const signing_utils_1 = require("./signing-utils");
class WalletBase {
constructor() {
// By creating the Signers in advance we can have a common pattern across wallets
// Each implementation is responsible for populating this map through addSigner
this.accountSigners = new Map();
}
/**
* Gets a list of accounts that have been registered
*/
getAccounts() {
return Array.from(this.accountSigners.keys());
}
/**
* Removes the account with the given address. Needs to be implemented by subclass, otherwise throws error
* @param _address The address of the account to be removed
*/
removeAccount(_address) {
throw new Error('removeAccount is not supported for this wallet');
}
/**
* Returns true if account has been registered
* @param address Account to check
*/
hasAccount(address) {
if (address) {
const normalizedAddress = (0, address_1.normalizeAddressWith0x)(address);
return this.accountSigners.has(normalizedAddress);
}
else {
return false;
}
}
/**
* Adds the account-signer set to the internal map
* @param address Account address
* @param signer Account signer
*/
addSigner(address, signer) {
const normalizedAddress = (0, address_1.normalizeAddressWith0x)(address);
this.accountSigners.set(normalizedAddress, signer);
}
/**
* Removes the account-signer
* @param address Account address
*/
removeSigner(address) {
const normalizedAddress = (0, address_1.normalizeAddressWith0x)(address);
this.accountSigners.delete(normalizedAddress);
}
/**
* Gets the signer based on the 'from' field in the tx body
* @param txParams Transaction to sign
*/
signTransaction(txParams) {
return __awaiter(this, void 0, void 0, function* () {
if (!txParams) {
throw new Error('No transaction object given!');
}
if (txParams.gasPrice && txParams.feeCurrency && txParams.feeCurrency !== '0x') {
throw new Error('Cannot serialize both "gasPrice" and "feeCurrency" together. To keep "feeCurrency", replace "gasPrice" with "maxFeePerGas". To keep "gasPrice" and send a type 0 transaction remove "feeCurrency"');
}
const rlpEncoded = (0, signing_utils_1.rlpEncodedTx)(txParams);
const addToV = rlpEncoded.type === 'ethereum-legacy'
? (0, signing_utils_1.chainIdTransformationForSigning)(txParams.chainId)
: 27;
// Get the signer from the 'from' field
const fromAddress = txParams.from.toString();
const signer = this.getSigner(fromAddress);
const signature = yield signer.signTransaction(addToV, rlpEncoded);
return (0, signing_utils_1.encodeTransaction)(rlpEncoded, signature);
});
}
/**
* Sign a personal Ethereum signed message.
* @param address Address of the account to sign with
* @param data Hex string message to sign
* @return Signature hex string (order: rsv)
*/
signPersonalMessage(address, data) {
return __awaiter(this, void 0, void 0, function* () {
if (!(0, address_1.isHexString)(data)) {
throw new Error('wallet@signPersonalMessage: Expected data has to be a hex string ');
}
const signer = this.getSigner(address);
const sig = yield signer.signPersonalMessage(data);
return ethUtil.toRpcSig(BigInt(sig.v), sig.r, sig.s);
});
}
/**
* Sign an EIP712 Typed Data message.
* @param address Address of the account to sign with
* @param typedData the typed data object
* @return Signature hex string (order: rsv)
*/
signTypedData(address, typedData) {
return __awaiter(this, void 0, void 0, function* () {
if (typedData === undefined) {
throw new Error('wallet@signTypedData: TypedData Missing');
}
const signer = this.getSigner(address);
const sig = yield signer.signTypedData(typedData);
return ethUtil.toRpcSig(BigInt(sig.v), sig.r, sig.s);
});
}
getSigner(address) {
const normalizedAddress = (0, address_1.normalizeAddressWith0x)(address);
if (!this.accountSigners.has(normalizedAddress)) {
throw new Error(`Could not find address ${normalizedAddress}`);
}
return this.accountSigners.get(normalizedAddress);
}
decrypt(address, ciphertext) {
return __awaiter(this, void 0, void 0, function* () {
const signer = this.getSigner(address);
return signer.decrypt(ciphertext);
});
}
/**
* Computes the shared secret (an ECDH key exchange object) between two accounts
*/
computeSharedSecret(address, publicKey) {
const signer = this.getSigner(address);
return signer.computeSharedSecret(publicKey);
}
}
exports.WalletBase = WalletBase;
//# sourceMappingURL=wallet-base.js.map
;