Skip to content

Commit 8762526

Browse files
authored
Merge pull request #263 from expand-network/feature_ENC-2674_CosmosSigner
Feature enc 2674 cosmos signer
2 parents 1dc2417 + e0ebf28 commit 8762526

File tree

9 files changed

+161
-5
lines changed

9 files changed

+161
-5
lines changed

configuration/config.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,22 @@
272272
"networkPassphrase": "Test SDF Network ; September 2015",
273273
"rpc": "https://horizon-testnet.stellar.org"
274274
},
275+
"cosmoshub-4": {
276+
"localName": "cosmoshub_mainnet",
277+
"chainName": "Cosmos",
278+
"chainSymbol": "cosmos",
279+
"rpc": "https://cosmos-rpc.publicnode.com:443",
280+
"publicRpc": "https://cosmos-rpc.publicnode.com:443",
281+
"gasPrice": "3000"
282+
},
283+
"theta-testnet-001": {
284+
"localName": "cosmoshub_testnet",
285+
"chainName": "Cosmos",
286+
"chainSymbol": "cosmos",
287+
"rpc": "https://rpc.sentry-01.theta-testnet.polypore.xyz",
288+
"publicRpc": "https://rpc.sentry-01.theta-testnet.polypore.xyz",
289+
"gasPrice": "3000"
290+
},
275291
"1600": {
276292
"localName": "XRPLedger",
277293
"chainName": "XRPL",

configuration/intialiseWeb3.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ const {
1010
} = require("@mysten/sui.js");
1111
const aptos = require('aptos');
1212
const { TonClient, WalletContractV4, internal } = require("@ton/ton");
13+
const { StargateClient } = require("@cosmjs/stargate");
14+
1315
const common = require('./common');
1416
const config = require('./config.json');
1517
const errorMessage = require('./errorMessage.json');
@@ -97,9 +99,10 @@ exports.initialiseWeb3 = async (data) => {
9799
});
98100
} else if (chainName === 'Stellar') {
99101
web3 = new Horizon.Server(rpc);
102+
} else if (chainName === "Cosmos") {
103+
web3 = await StargateClient.connect(rpc);
100104
}
101105

102-
103106
return (web3);
104107

105108
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"tronweb": "5.3.1",
6262
"tweetnacl": "1.0.3",
6363
"web3": "1.7.5",
64+
"@cosmjs/stargate": "0.32.4",
6465
"xrpl": "^3.0.0"
6566
},
6667
"devDependencies": {

src/adapters/Wallet/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ class Wallet {
5656
signVersionedTransaction = async (transactionObject) => {
5757

5858
const configuration = { "params": {} };
59-
transactionObject.function = "txObjectSol()";
59+
transactionObject.function = "txObjSol()";
6060
const validObject = await schemaValidator.validateInput(transactionObject);
6161

6262
if (!validObject.valid) {

src/adapters/WalletCosmos/index.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
const { DirectSecp256k1HdWallet } = require("@cosmjs/proto-signing");
2+
const { SigningStargateClient } = require("@cosmjs/stargate");
3+
const { TxRaw } = require("cosmjs-types/cosmos/tx/v1beta1/tx");
4+
const axios = require('axios').default;
5+
const schemaValidator = require('../../../configuration/schemaValidator');
6+
const common = require('../../../configuration/common');
7+
const config = require('../../../configuration/config.json');
8+
9+
10+
class WalletCosmos {
11+
12+
constructor(options) {
13+
this.wallet = options.privateKey;
14+
}
15+
16+
signTransaction = async (transactionObject) => {
17+
18+
const configuration = { "params": {} };
19+
transactionObject.function = "txObjSol()";
20+
const validJson = await schemaValidator.validateInput(transactionObject);
21+
22+
if (!validJson.valid) {
23+
return validJson;
24+
}
25+
26+
const chainId = await common.getChainId({ chainId: transactionObject.chainId, chainSymbol: transactionObject.chainSymbol });
27+
let chainName = config.chains[chainId].chainName;
28+
29+
axios.defaults.headers['X-API-KEY'] = this.xApiKey;
30+
const apiURL = `${config.url.apiurl}/chain/getpublicrpc/`;
31+
32+
configuration.params = {
33+
chainId
34+
};
35+
36+
let rpc = await axios.get(apiURL, configuration);
37+
rpc = rpc.data.data.rpc;
38+
39+
if (chainName !== "Cosmos") {
40+
return {
41+
"msg": "Cosmos wallet can be used only with Cosmos chains"
42+
}
43+
};
44+
45+
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(this.wallet, {
46+
prefix: "cosmos",
47+
});
48+
49+
const Account = (await wallet.getAccounts())[0].address;
50+
const signingClient = await SigningStargateClient.connectWithSigner(rpc, wallet);
51+
52+
const tx = await signingClient.sign(Account, [
53+
{
54+
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
55+
value: {
56+
fromAddress: Account,
57+
toAddress: transactionObject.to,
58+
amount: [{ denom: "uatom", amount: transactionObject.value }],
59+
},
60+
}
61+
], {
62+
amount: [{ denom: "uatom", amount: "1000" }],
63+
gas: "200000",
64+
},
65+
"expand"
66+
);
67+
68+
const encodedTx = TxRaw.encode(tx).finish();
69+
const rawString = Buffer.from(encodedTx).toString("base64");
70+
return { chainId: chainId, rawTransaction: rawString };
71+
72+
}
73+
74+
sendTransaction = async (options) => {
75+
76+
const filterOptions = options;
77+
filterOptions.function = "sendTransaction()";
78+
const validJson = await schemaValidator.validateInput(options);
79+
if (!validJson.valid) {
80+
return (validJson);
81+
}
82+
83+
try {
84+
85+
const apiURL = `${config.url.apiurl}/chain/sendtransaction/`;
86+
87+
const params = {
88+
method: "post",
89+
url: apiURL,
90+
data: options,
91+
headers: {
92+
"x-api-key": this.xApiKey
93+
}
94+
};
95+
96+
const transactionHash = await axios(params);
97+
return transactionHash.data;
98+
}
99+
100+
catch (error) {
101+
return error;
102+
}
103+
104+
};
105+
106+
107+
}
108+
109+
module.exports = { WalletCosmos };

src/adapters/WalletPhantom/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class WalletPhantom {
1616
signTransaction = async (transactionObject) => {
1717

1818
const configuration = { "params": {} };
19-
transactionObject.function = "txObjectSol()";
19+
transactionObject.function = "txObjSol()";
2020
const validObject = await schemaValidator.validateInput(transactionObject);
2121

2222
if (!validObject.valid) {

src/index.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
const axios = require('axios').default;
22
const config = require('../configuration/config.json');
33
const schemaValidator = require('../configuration/schemaValidator');
4-
const { Wallet, WalletFordefi, WalletDFNS, WalletTON, WalletFireblocks, WalletPhantom, WalletCoinbase, WalletCircle, WalletStellar, WalletXRPL } = require('./interfaces/index');
4+
const { Wallet, WalletFordefi, WalletDFNS, WalletTON,
5+
WalletFireblocks, WalletPhantom, WalletCoinbase,
6+
WalletCircle, WalletCosmos, WalletStellar, WalletXRPL } = require('./interfaces/index');
57

68
exports.prepareTransaction = async (apiURL, options) => {
79

@@ -88,6 +90,9 @@ exports.WalletStellar = WalletStellar;
8890

8991
exports.WalletXRPL = WalletXRPL;
9092

93+
exports.WalletCosmos = WalletCosmos;
94+
95+
9196

9297

9398

src/interfaces/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const { WalletCoinbase } = require('../adapters/WalletCoinbase');
66
const { WalletTON } = require('../adapters/WalletTON');
77
const { WalletFireblocks } = require('../adapters/WalletFireblocks');
88
const { WalletCircle } = require('../adapters/WalletCircle');
9+
const { WalletCosmos } = require("../adapters/WalletCosmos");
910
const { WalletStellar } = require('../adapters/WalletStellar');
1011
const { WalletXRPL } = require('../adapters/WalletXRPL');
1112

@@ -19,5 +20,6 @@ module.exports = {
1920
WalletFireblocks,
2021
WalletCircle,
2122
WalletStellar,
22-
WalletXRPL
23+
WalletXRPL,
24+
WalletCosmos
2325
};

test/cosmosTest.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const { WalletCosmos } = require('../src');
2+
3+
const xApiKey = 'X_API_KEY';
4+
5+
async function main() {
6+
const preparedTx = {
7+
"chainId": "theta-testnet-001",
8+
"from": "cosmos1h7sp085zemehp5gunplymxhflrp8ls4qm3mxrq",
9+
"to": "cosmos1h6r7sgwxfxps4payfyc8rl56svzmx6t5kpumg3",
10+
"value": "1000",
11+
}
12+
13+
const wallet = new WalletCosmos({ privateKey:'your Mnemonic in English', xApiKey:xApiKey});
14+
const signedTx = await wallet.signTransaction(preparedTx);
15+
console.log(signedTx);
16+
// const TxHash = await wallet.sendTransaction(signedTx);
17+
// console.log(TxHash);
18+
}
19+
20+
main();

0 commit comments

Comments
 (0)