Skip to content
This repository was archived by the owner on Apr 15, 2022. It is now read-only.

Commit 547e895

Browse files
author
Booyoun-Kim
committed
support irishub-1
1 parent 6b53456 commit 547e895

File tree

5 files changed

+38
-381
lines changed

5 files changed

+38
-381
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const ecpairPriv = cosmos.getECPairPriv(mnemonic);
7979
```js
8080
const cosmosjs = require("@cosmostation/cosmosjs");
8181

82-
const chainId = "irishub";
82+
const chainId = "irishub-1";
8383
const iris = cosmosjs.network(lcdUrl, chainId);
8484
iris.setBech32MainPrefix("iaa");
8585
```

example/irishub.js

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,51 +2,37 @@ const cosmosjs = require("../src");
22

33
// [WARNING] This mnemonic is just for the demo purpose. DO NOT USE THIS MNEMONIC for your own wallet.
44
const mnemonic = "swear buyer security impulse public stereo peasant correct cross tornado bid discover anchor float venture deal patch property cool wreck eight dwarf december surface";
5-
const chainId = "irishub";
5+
const chainId = "irishub-1";
66
// Please install and use rest server separately. (https://hub.cosmos.network/master/resources/service-providers.html#setting-up-the-rest-server)
7-
const iris = cosmosjs.network("YOUR REST SERVER URL", chainId);
7+
const iris = cosmosjs.network("https://api.irisnet.org", chainId);
88
iris.setBech32MainPrefix("iaa");
99
iris.setPath("m/44'/118'/0'/0/0");
1010
const address = iris.getAddress(mnemonic);
1111
const ecpairPriv = iris.getECPairPriv(mnemonic);
1212

13-
// Generate irishub/bank/Send transaction and broadcast
13+
// Generate MsgSend transaction and broadcast
1414
iris.getAccounts(address).then(data => {
1515
let stdSignMsg = iris.newStdMsg({
1616
msgs: [
1717
{
18-
type: "irishub/bank/Send",
18+
type: "cosmos-sdk/MsgSend",
1919
value: {
20-
inputs: [
20+
amount: [
2121
{
22-
address: address,
23-
coins: [
24-
{
25-
denom: "iris-atto",
26-
amount: String(1000000000000000000) // 18 decimal places
27-
}
28-
]
22+
amount: String(100000), // 6 decimal places (1000000 uiris = 1 IRIS)
23+
denom: "uiris"
2924
}
3025
],
31-
outputs: [
32-
{
33-
address: "iaa12g4vfyq65yf5cds4v5pr3jmdd4v6s40fkaaxtf",
34-
coins: [
35-
{
36-
denom: "iris-atto",
37-
amount: String(1000000000000000000)
38-
}
39-
]
40-
}
41-
]
26+
from_address: address,
27+
to_address: "iaa12g4vfyq65yf5cds4v5pr3jmdd4v6s40fkaaxtf"
4228
}
4329
}
4430
],
4531
chain_id: chainId,
46-
fee: { amount: [ { amount: String(400000000000000000), denom: "iris-atto" } ], gas: String(50000) },
32+
fee: { amount: [ { amount: String(20000), denom: "uiris" } ], gas: String(200000) },
4733
memo: "",
48-
account_number: String(data.value.account_number),
49-
sequence: String(data.value.sequence)
34+
account_number: String(data.account.account_number),
35+
sequence: String(data.account.sequence)
5036
});
5137

5238
const signedTx = iris.sign(stdSignMsg, ecpairPriv);

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@cosmostation/cosmosjs",
3-
"version": "0.9.2",
3+
"version": "0.9.3",
44
"description": "A JavasSript Open Source Library for Cosmos Network, IRISnet, Kava, Band Protocol, Starname, Secret Network, Akash, and Certik. (HTML developers can use /dist/cosmos.js)",
55
"main": "./src/index.js",
66
"repository": {

src/index.js

Lines changed: 24 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ let Cosmos = function(url, chainId) {
2626
throw new Error("chainId object was not set or invalid")
2727
}
2828

29-
console.log("WARN deprecated @cosmostation/cosmosjs@0.8.2: You needs to upgrade to @cosmostation/cosmosjs above 0.9.0+ : 1) Proper nodejs v14+ support 2) 0.9.0+ supports protobuf signing for cosmos-sdk 0.40.0+");
29+
console.log("WARN deprecated @cosmostation/cosmosjs@0.9.x: You needs to upgrade to @cosmostation/cosmosjs above 0.10.0+ : 1) Proper nodejs v14+ support 2) 0.10.0+ supports protobuf signing for cosmos-sdk 0.40.0+");
3030
}
3131

3232
function network(url, chainId) {
@@ -84,8 +84,10 @@ Cosmos.prototype.getAccounts = function(address) {
8484
accountsApi = "/cosmos/auth/v1beta1/accounts/";
8585
} else if (this.chainId.indexOf("stargate-final") != -1) {
8686
accountsApi = "/cosmos/auth/v1beta1/accounts/";
87+
} else if (this.chainId.indexOf("bifrost") != -1) {
88+
accountsApi = "/cosmos/auth/v1beta1/accounts/";
8789
} else if (this.chainId.indexOf("irishub") != -1) {
88-
accountsApi = "/bank/accounts/";
90+
accountsApi = "/cosmos/auth/v1beta1/accounts/";
8991
} else {
9092
accountsApi = "/auth/accounts/";
9193
}
@@ -121,150 +123,44 @@ Cosmos.prototype.getECPairPriv = function(mnemonic) {
121123
Cosmos.prototype.newStdMsg = function(input) {
122124
const stdSignMsg = new Object;
123125
stdSignMsg.json = input;
124-
125-
// Exception
126-
if (input.msgs[0].type == "irishub/bank/Send") {
127-
stdSignMsg.jsonForSigningIrisTx =
128-
{
129-
msgs: [
130-
{
131-
inputs: [
132-
{
133-
address: input.msgs[0].value.inputs[0].address,
134-
coins: [
135-
{
136-
denom: input.msgs[0].value.inputs[0].coins[0].denom,
137-
amount: input.msgs[0].value.inputs[0].coins[0].amount
138-
}
139-
]
140-
}
141-
],
142-
outputs: [
143-
...input.msgs[0].value.outputs
144-
]
145-
}
146-
],
147-
chain_id: input.chain_id,
148-
fee: { amount: [ { amount: input.fee.amount[0].amount, denom: input.fee.amount[0].denom } ], gas: input.fee.gas },
149-
memo: input.memo,
150-
account_number: input.account_number,
151-
sequence: input.sequence
152-
}
153-
} else if (input.msgs[0].type == "irishub/stake/BeginUnbonding") {
154-
stdSignMsg.jsonForSigningIrisTx =
155-
{
156-
msgs: [
157-
{
158-
shares_amount: String(input.msgs[0].value.shares_amount),
159-
delegator_addr: input.msgs[0].value.delegator_addr,
160-
validator_addr: input.msgs[0].value.validator_addr
161-
}
162-
],
163-
chain_id: input.chain_id,
164-
fee: { amount: [ { amount: input.fee.amount[0].amount, denom: input.fee.amount[0].denom } ], gas: input.fee.gas },
165-
memo: input.memo,
166-
account_number: input.account_number,
167-
sequence: input.sequence
168-
}
169-
} else if (input.msgs[0].type == "irishub/stake/BeginRedelegate") {
170-
stdSignMsg.jsonForSigningIrisTx =
171-
{
172-
msgs: [
173-
{
174-
delegator_addr: input.msgs[0].value.delegator_addr,
175-
validator_src_addr: input.msgs[0].value.validator_src_addr,
176-
validator_dst_addr: input.msgs[0].value.validator_dst_addr,
177-
shares: String(input.msgs[0].value.shares_amount) + ".0000000000" // IRIS Exception) For signing, shares is correct.
178-
}
179-
],
180-
chain_id: input.chain_id,
181-
fee: { amount: [ { amount: input.fee.amount[0].amount, denom: input.fee.amount[0].denom } ], gas: input.fee.gas },
182-
memo: input.memo,
183-
account_number: input.account_number,
184-
sequence: input.sequence
185-
}
186-
}
187-
188126
stdSignMsg.bytes = convertStringToBytes(JSON.stringify(sortObject(stdSignMsg.json)));
189127
return stdSignMsg;
190128
}
191129

192130
Cosmos.prototype.sign = function(stdSignMsg, ecpairPriv, modeType = "sync") {
193131
// The supported return types includes "block"(return after tx commit), "sync"(return after CheckTx) and "async"(return right away).
194132
let signMessage = new Object;
195-
if (stdSignMsg.json.msgs[0].type == "irishub/bank/Send" ||
196-
stdSignMsg.json.msgs[0].type == "irishub/stake/BeginUnbonding" ||
197-
stdSignMsg.json.msgs[0].type == "irishub/stake/BeginRedelegate") {
198-
signMessage = stdSignMsg.jsonForSigningIrisTx;
199-
} else {
200-
signMessage = stdSignMsg.json;
201-
}
133+
signMessage = stdSignMsg.json;
202134
const hash = crypto.createHash('sha256').update(JSON.stringify(sortObject(signMessage))).digest('hex');
203135
const buf = Buffer.from(hash, 'hex');
204136
let signObj = secp256k1.sign(buf, ecpairPriv);
205137
var signatureBase64 = Buffer.from(signObj.signature, 'binary').toString('base64');
206138
let signedTx = new Object;
207-
if (this.chainId.indexOf("irishub") != -1) {
208-
signedTx = {
209-
"tx": {
210-
"msg": stdSignMsg.json.msgs,
211-
"fee": stdSignMsg.json.fee,
212-
"signatures": [
213-
{
214-
"signature": signatureBase64,
215-
"account_number": stdSignMsg.json.account_number,
216-
"sequence": stdSignMsg.json.sequence,
217-
"pub_key": {
218-
"type": "tendermint/PubKeySecp256k1",
219-
"value": getPubKeyBase64(ecpairPriv)
220-
}
221-
}
222-
],
223-
"memo": stdSignMsg.json.memo
224-
},
225-
"mode": modeType
226-
}
227-
228-
// The key of "shares" is using to sign for IRIS Redelegate.
229-
// After signing, you have to replace the "shares" key name to "shares_amount".
230-
// It is an exception to "irishub/stake/BeginRedelegate".
231-
if (stdSignMsg.json.msgs[0].type == "irishub/stake/BeginRedelegate") {
232-
var txBodyStr = JSON.stringify(signedTx);
233-
txBodyStr = txBodyStr.replace("\"shares", "\"shares_amount");
234-
signedTx = JSON.parse(txBodyStr);
235-
}
236-
} else {
237-
signedTx = {
238-
"tx": {
239-
"msg": stdSignMsg.json.msgs,
240-
"fee": stdSignMsg.json.fee,
241-
"signatures": [
242-
{
243-
"account_number": stdSignMsg.json.account_number,
244-
"sequence": stdSignMsg.json.sequence,
245-
"signature": signatureBase64,
246-
"pub_key": {
247-
"type": "tendermint/PubKeySecp256k1",
248-
"value": getPubKeyBase64(ecpairPriv)
249-
}
250-
}
251-
],
252-
"memo": stdSignMsg.json.memo
253-
},
254-
"mode": modeType
255-
}
139+
signedTx = {
140+
"tx": {
141+
"msg": stdSignMsg.json.msgs,
142+
"fee": stdSignMsg.json.fee,
143+
"signatures": [
144+
{
145+
"account_number": stdSignMsg.json.account_number,
146+
"sequence": stdSignMsg.json.sequence,
147+
"signature": signatureBase64,
148+
"pub_key": {
149+
"type": "tendermint/PubKeySecp256k1",
150+
"value": getPubKeyBase64(ecpairPriv)
151+
}
152+
}
153+
],
154+
"memo": stdSignMsg.json.memo
155+
},
156+
"mode": modeType
256157
}
257158

258159
return signedTx;
259160
}
260161

261162
Cosmos.prototype.broadcast = function(signedTx) {
262-
let broadcastApi = "";
263-
if (this.chainId.indexOf("irishub") != -1) {
264-
broadcastApi = "/tx/broadcast";
265-
} else {
266-
broadcastApi = "/txs";
267-
}
163+
let broadcastApi = "/txs";
268164

269165
return fetch(this.url + broadcastApi, {
270166
method: 'POST',

0 commit comments

Comments
 (0)