@onekeyfe/blockchain-libs
Version:
OneKey Blockchain Libs
149 lines • 5.26 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.makeTxRawBytes = exports.makeMsgExecuteContract = exports.makeMsgSend = exports.fastMakeSignDoc = exports.makeSignBytes = void 0;
const tx_1 = require("cosmjs-types/cosmos/bank/v1beta1/tx");
const coin_1 = require("cosmjs-types/cosmos/base/v1beta1/coin");
const keys_1 = require("cosmjs-types/cosmos/crypto/ed25519/keys");
const signing_1 = require("cosmjs-types/cosmos/tx/signing/v1beta1/signing");
const tx_2 = require("cosmjs-types/cosmos/tx/v1beta1/tx");
const tx_3 = require("cosmjs-types/cosmwasm/wasm/v1beta1/tx");
const any_1 = require("cosmjs-types/google/protobuf/any");
const long_1 = __importDefault(require("long"));
const MsgProtoRegistry = {
'/cosmos.bank.v1beta1.MsgSend': tx_1.MsgSend,
'/terra.wasm.v1beta1.MsgExecuteContract': tx_3.MsgExecuteContract,
};
function makeTxBodyBytes(body) {
return tx_2.TxBody.encode(tx_2.TxBody.fromPartial({
...body,
messages: body.messages.map((msg) => {
const proto = MsgProtoRegistry[msg.typeUrl];
return {
typeUrl: msg.typeUrl,
value: proto.encode(proto.fromPartial(msg.value)).finish(),
};
}),
})).finish();
}
/**
* Create signer infos from the provided signers.
*
* This implementation does not support different signing modes for the different signers.
*/
function makeSignerInfos(signers, signMode) {
return signers.map(({ pubkey, sequence }) => ({
publicKey: pubkey,
modeInfo: {
single: { mode: signMode },
},
sequence: long_1.default.fromNumber(sequence),
}));
}
/**
* Creates and serializes an AuthInfo document.
*
* This implementation does not support different signing modes for the different signers.
*/
function makeAuthInfoBytes(signers, feeAmount, gasLimit, signMode = signing_1.SignMode.SIGN_MODE_DIRECT) {
const authInfo = {
signerInfos: makeSignerInfos(signers, signMode),
fee: {
amount: [...feeAmount],
gasLimit: long_1.default.fromNumber(gasLimit),
},
};
return tx_2.AuthInfo.encode(tx_2.AuthInfo.fromPartial(authInfo)).finish();
}
function encodePubkey(pubkey) {
const pubkeyProto = keys_1.PubKey.fromPartial({
key: pubkey,
});
return any_1.Any.fromPartial({
typeUrl: '/cosmos.crypto.secp256k1.PubKey',
value: Uint8Array.from(keys_1.PubKey.encode(pubkeyProto).finish()),
});
}
function makeSignDoc(bodyBytes, authInfoBytes, chainId, accountNumber) {
return {
bodyBytes: bodyBytes,
authInfoBytes: authInfoBytes,
chainId: chainId,
accountNumber: long_1.default.fromNumber(accountNumber),
};
}
function makeSignBytes({ accountNumber, authInfoBytes, bodyBytes, chainId, }) {
const signDoc = tx_2.SignDoc.fromPartial({
authInfoBytes: authInfoBytes,
bodyBytes: bodyBytes,
chainId: chainId,
accountNumber: accountNumber,
});
return tx_2.SignDoc.encode(signDoc).finish();
}
exports.makeSignBytes = makeSignBytes;
function fastMakeSignDoc(messages, memo, gasLimit, feeAmount, pubkey, mainCoinDenom, chainId, accountNumber, nonce) {
const bodyBytes = makeTxBodyBytes({
messages,
memo,
});
const encodePub = encodePubkey(pubkey);
const authBytes = makeAuthInfoBytes([{ pubkey: encodePub, sequence: nonce }], [
{
amount: feeAmount,
denom: mainCoinDenom,
},
], Number(gasLimit));
return makeSignDoc(bodyBytes, authBytes, chainId, accountNumber);
}
exports.fastMakeSignDoc = fastMakeSignDoc;
function makeMsgSend(fromAddress, toAddress, value, denom) {
return {
typeUrl: '/cosmos.bank.v1beta1.MsgSend',
value: {
fromAddress,
toAddress,
amount: [
{
amount: value,
denom: denom,
},
],
},
};
}
exports.makeMsgSend = makeMsgSend;
function removeNull(obj) {
if (obj !== null && typeof obj === 'object') {
return Object.entries(obj)
.filter(([, v]) => v != null)
.reduce((acc, [k, v]) => ({
...acc,
[k]: v === Object(v) && !Array.isArray(v) ? removeNull(v) : v,
}), {});
}
return obj;
}
function makeMsgExecuteContract(sender, contract, msg, funds) {
return {
typeUrl: '/terra.wasm.v1beta1.MsgExecuteContract',
value: {
sender,
contract,
msg: Buffer.from(JSON.stringify(removeNull(msg)), 'utf-8'),
funds: (funds !== null && funds !== void 0 ? funds : []).map((i) => coin_1.Coin.encode(i).finish()),
},
};
}
exports.makeMsgExecuteContract = makeMsgExecuteContract;
function makeTxRawBytes(bodyBytes, authInfoBytes, signatures) {
return tx_2.TxRaw.encode(tx_2.TxRaw.fromPartial({
bodyBytes,
authInfoBytes,
signatures,
})).finish();
}
exports.makeTxRawBytes = makeTxRawBytes;
//# sourceMappingURL=signing.js.map