laconic-sdk/src/index.ts

120 lines
3.3 KiB
TypeScript
Raw Normal View History

import axios from "axios";
import { MessageTypes, signTypedData, SignTypedDataVersion } from "@metamask/eth-sig-util";
2022-03-30 05:21:03 +00:00
import { generateEndpointAccount, generateEndpointBroadcast, generatePostBodyBroadcast } from '@tharsis/provider';
import {
createMessageSend,
createTxRawEIP712,
signatureToWeb3Extension,
createTxMsgVote,
Chain,
Sender
} from '@tharsis/transactions'
const ETHERMINT_REST_ENDPOINT = 'http://127.0.0.1:1317'
interface TypedMessageDomain {
name?: string;
version?: string;
chainId?: number;
verifyingContract?: string;
salt?: ArrayBuffer;
}
2022-03-30 05:21:03 +00:00
interface VoteParams {
proposalId: number;
option: number;
}
export const sendTokens = async (senderPrivateKey: string, senderAddress: string, destinationAddress: string) => {
let { data: addrData} = await axios.get(`${ETHERMINT_REST_ENDPOINT}${generateEndpointAccount(senderAddress)}`)
const chain = {
chainId: 9000,
cosmosChainId: 'ethermint_9000-1',
}
const sender = {
accountAddress: addrData.account.base_account.address,
sequence: addrData.account.base_account.sequence,
accountNumber: addrData.account.base_account.account_number,
pubkey: addrData.account.base_account.pub_key.key,
}
const fee = {
amount: '20',
denom: 'aphoton',
gas: '200000',
}
const memo = ''
const params = {
destinationAddress: destinationAddress,
amount: '10',
denom: 'aphoton',
}
// Create a MsgSend transaction.
const msg = createMessageSend(chain, sender, fee, memo, params)
2022-03-30 05:21:03 +00:00
await signAndSendMessage(senderPrivateKey, chain, sender, msg)
}
export const sendVote = async (senderPrivateKey: string, senderAddress: string, params: VoteParams) => {
let { data: addrData} = await axios.get(`${ETHERMINT_REST_ENDPOINT}${generateEndpointAccount(senderAddress)}`)
const chain = {
chainId: 9000,
cosmosChainId: 'ethermint_9000-1',
}
const sender = {
accountAddress: addrData.account.base_account.address,
sequence: addrData.account.base_account.sequence,
accountNumber: addrData.account.base_account.account_number,
pubkey: addrData.account.base_account.pub_key.key,
}
const fee = {
amount: '20',
denom: 'aphoton',
gas: '200000',
}
const memo = ''
const msg = createTxMsgVote(chain, sender, fee, memo, params)
await signAndSendMessage(senderPrivateKey, chain, sender, msg)
}
const signAndSendMessage = async (senderPrivateKey: string, chain: Chain, sender: Sender, msg: any) => {
const eipMessageDomain: any = msg.eipToSign.domain;
// Sign transaction.
const signature = signTypedData({
data: {
types: msg.eipToSign.types as MessageTypes,
primaryType: msg.eipToSign.primaryType,
domain: eipMessageDomain as TypedMessageDomain,
message: msg.eipToSign.message as Record<string, unknown>
},
privateKey: Buffer.from(senderPrivateKey, 'hex'),
version: SignTypedDataVersion.V4
})
let extension = signatureToWeb3Extension(chain, sender, signature)
2022-03-30 05:21:03 +00:00
// Create the txRaw.
let rawTx = createTxRawEIP712(msg.legacyAmino.body, msg.legacyAmino.authInfo, extension)
const body = generatePostBodyBroadcast(rawTx)
2022-03-30 05:21:03 +00:00
// Broadcast transaction.
return axios.post(
`${ETHERMINT_REST_ENDPOINT}${generateEndpointBroadcast()}`,
JSON.parse(body)
)
2022-03-30 05:21:03 +00:00
// TODO: Check for successful broadcast.
}