@onekeyfe/blockchain-libs
Version:
OneKey Blockchain Libs
157 lines • 6.3 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Provider = void 0;
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const borsh_1 = require("borsh");
const abc_1 = require("../../abc");
const nearcli_1 = require("./nearcli");
const key_pair_1 = require("./sdk/key_pair");
const nearTx = __importStar(require("./sdk/transaction"));
const FT_TRANSFER_GAS = '30000000000000';
const FT_TRANSFER_DEPOSIT = '1';
const IMPLICIT_ACCOUNT_PATTERN = /^[a-z\d]{64}$/;
const REGISTER_ACCOUNT_PATTERN = /^(([a-z\d]+[-_])*[a-z\d]+\.)*([a-z\d]+[-_])*[a-z\d]+$/;
const packActions = (unsignedTx) => {
const { outputs } = unsignedTx;
const [output] = outputs;
const actions = [];
if (!output.tokenAddress) {
actions.push(nearTx.transfer(output.value.integerValue().toFixed()));
}
else {
actions.push(nearTx.functionCall('ft_transfer', {
amount: output.value.integerValue().toFixed(),
receiver_id: output.address,
}, FT_TRANSFER_GAS, FT_TRANSFER_DEPOSIT));
}
return actions;
};
class Provider extends abc_1.BaseProvider {
get nearCli() {
return this.clientSelector((i) => i instanceof nearcli_1.NearCli);
}
async getTxCostConfig() {
if (!this._txCostConfig) {
this._txCostConfig = await this.nearCli.then((i) => i.getTxCostConfig());
}
return this._txCostConfig;
}
async pubkeyToAddress(verifier, encoding) {
const pubkey = await verifier.getPubkey(true);
if (encoding === 'ENCODED_PUBKEY') {
return 'ed25519:' + (0, borsh_1.baseEncode)(pubkey);
}
else {
return pubkey.toString('hex');
}
}
async verifyAddress(address) {
let encoding = undefined;
if (IMPLICIT_ACCOUNT_PATTERN.test(address)) {
encoding = 'IMPLICIT_ACCOUNT';
}
else if (REGISTER_ACCOUNT_PATTERN.test(address)) {
return {
isValid: true,
normalizedAddress: address,
displayAddress: address,
encoding: 'REGISTER_ACCOUNT',
};
}
else if (address.includes(':')) {
const [prefix, encoded] = address.split(':');
try {
if (prefix === 'ed25519' &&
Buffer.from((0, borsh_1.baseDecode)(encoded)).length === 32) {
encoding = 'ENCODED_PUBKEY';
}
}
catch (e) {
// ignored
}
}
if (encoding) {
return {
isValid: true,
normalizedAddress: address,
displayAddress: address,
encoding,
};
}
else {
return {
isValid: false,
};
}
}
async buildUnsignedTx(unsignedTx) {
const cli = await this.nearCli;
const { inputs: [input], payload = {}, } = unsignedTx;
let { nonce, feeLimit } = unsignedTx;
const feePricePerUnit = unsignedTx.feePricePerUnit ||
(await cli.getFeePricePerUnit().then((i) => i.normal.price));
if (input) {
nonce =
nonce !== null && nonce !== void 0 ? nonce : (await cli.getAddress(input.address).then((i) => i.nonce));
const { blockHash } = await cli.getBestBlock();
Object.assign(payload, { blockHash });
if (!feeLimit) {
const txCostConfig = await this.getTxCostConfig();
const { transfer_cost, action_receipt_creation_config } = txCostConfig;
if (!input.tokenAddress) {
feeLimit = new bignumber_js_1.default(transfer_cost.execution)
.plus(action_receipt_creation_config.execution)
.multipliedBy(2);
}
else {
feeLimit = new bignumber_js_1.default(FT_TRANSFER_GAS); // hard to estimate gas of function call
}
}
}
feeLimit = feeLimit || new bignumber_js_1.default('100000000000');
return Object.assign({}, unsignedTx, {
feePricePerUnit,
feeLimit,
nonce,
payload,
});
}
async signTransaction(unsignedTx, signers) {
const { inputs: [input], outputs: [output], nonce, payload: { blockHash }, } = unsignedTx;
const signer = signers[input.address];
const pubkey = await signer.getPubkey(true);
const actions = packActions(unsignedTx);
const tx = nearTx.createTransaction(input.address, key_pair_1.PublicKey.from(new Uint8Array(pubkey)), output.tokenAddress || output.address,
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
nonce, actions, (0, borsh_1.baseDecode)(blockHash));
const [hash, signedTx] = await nearTx.signTransactionObject(tx, (digest) => signer.sign(Buffer.from(digest)).then((res) => new Uint8Array(res[0])));
return {
txid: (0, borsh_1.baseEncode)(hash),
rawTx: Buffer.from(signedTx.encode()).toString('base64'),
};
}
}
exports.Provider = Provider;
//# sourceMappingURL=provider.js.map