This repository has been archived by the owner on Apr 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 78
/
index.js
159 lines (142 loc) · 4.87 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import * as bech32 from "bech32";
import * as bip32 from "bip32";
import * as bip39 from "bip39";
import bitcoinjs from "bitcoinjs-lib";
import crypto from "crypto";
/*
Developed / Developing by Cosmostation
[WARNING] CosmosJS is under ACTIVE DEVELOPMENT and should be treated as alpha version. We will remove this warning when we have a release that is stable, secure, and propoerly tested.
*/
import fetch from 'node-fetch';
import message from "./messages/proto";
import request from "request";
import secp256k1 from "secp256k1";
export class Cosmos {
constructor(url, chainId) {
this.url = url;
this.chainId = chainId;
this.path = "m/44'/118'/0'/0/0";
this.bech32MainPrefix = "cosmos";
}
// strength(128): 12 words, strength(256): 24 words
getRandomMnemonic(strength = 256) {
return bip39.generateMnemonic(strength);
}
setBech32MainPrefix(prefix) {
this.bech32MainPrefix = prefix;
if (!this.bech32MainPrefix) throw new Error("bech32MainPrefix object was not set or invalid");
}
setPath(value) {
this.path = value;
if (!this.path) throw new Error("path object was not set or invalid");
}
getAddress(mnemonic, checkSum = true) {
if (typeof mnemonic !== "string") {
throw new Error("mnemonic expects a string")
}
if (checkSum) {
if (!bip39.validateMnemonic(mnemonic)) throw new Error("mnemonic phrases have invalid checksums");
}
const seed = bip39.mnemonicToSeed(mnemonic);
const node = bip32.fromSeed(seed)
const child = node.derivePath(this.path)
const words = bech32.toWords(child.identifier);
return bech32.encode(this.bech32MainPrefix, words);
}
changeAddress(prefix, address) {
try {
const decode = bech32.decode(address);
return bech32.encode(prefix, decode.words);
} catch (e) {
throw new Error("cannot change address")
return "";
}
}
getECPairPriv(mnemonic) {
if (typeof mnemonic !== "string") {
throw new Error("mnemonic expects a string")
}
const seed = bip39.mnemonicToSeed(mnemonic);
const node = bip32.fromSeed(seed);
const child = node.derivePath(this.path);
return child.privateKey;
}
getPubKey(privKey) {
const pubKeyByte = secp256k1.publicKeyCreate(privKey);
return pubKeyByte;
}
getPubKeyAny(privKey) {
const pubKeyByte = secp256k1.publicKeyCreate(privKey);
var buf1 = new Buffer.from([10]);
var buf2 = new Buffer.from([pubKeyByte.length]);
var buf3 = new Buffer.from(pubKeyByte);
const pubKey = Buffer.concat([buf1, buf2, buf3]);
const pubKeyAny = new message.google.protobuf.Any({
type_url: "/cosmos.crypto.secp256k1.PubKey",
value: pubKey
});
return pubKeyAny;
}
wasmQuery(contractAddress, query) {
let smartQueryApi = "/wasm/contract/" + contractAddress + "/smart/" + toHex(query) + "?encoding=UTF-8";
return fetch(this.url + smartQueryApi).then(response => response.json())
}
getAccounts(address) {
let accountsApi = "/cosmos/auth/v1beta1/accounts/";
return fetch(this.url + accountsApi + address).then(response => response.json())
}
sign(txBody, authInfo, accountNumber, privKey) {
const bodyBytes = message.cosmos.tx.v1beta1.TxBody.encode(txBody).finish();
const authInfoBytes = message.cosmos.tx.v1beta1.AuthInfo.encode(authInfo).finish();
const signDoc = new message.cosmos.tx.v1beta1.SignDoc({
body_bytes: bodyBytes,
auth_info_bytes: authInfoBytes,
chain_id: this.chainId,
account_number: Number(accountNumber)
});
let signMessage = message.cosmos.tx.v1beta1.SignDoc.encode(signDoc).finish();
const hash = crypto.createHash("sha256").update(signMessage).digest();
const sig = secp256k1.sign(hash, Buffer.from(privKey));
const txRaw = new message.cosmos.tx.v1beta1.TxRaw({
body_bytes: bodyBytes,
auth_info_bytes: authInfoBytes,
signatures: [sig.signature],
});
const txBytes = message.cosmos.tx.v1beta1.TxRaw.encode(txRaw).finish();
const txBytesBase64 = Buffer.from(txBytes, 'binary').toString('base64');
return txBytes;
}
// "BROADCAST_MODE_UNSPECIFIED", "BROADCAST_MODE_BLOCK", "BROADCAST_MODE_SYNC", "BROADCAST_MODE_ASYNC"
broadcast(signedTxBytes, broadCastMode = "BROADCAST_MODE_SYNC") {
const txBytesBase64 = Buffer.from(signedTxBytes, 'binary').toString('base64');
var options = {
method: 'POST',
url: this.url + '/cosmos/tx/v1beta1/txs',
headers:
{ 'Content-Type': 'application/json' },
body: { tx_bytes: txBytesBase64, mode: broadCastMode },
json: true
};
return new Promise(function(resolve, reject){
request(options, function (error, response, body) {
if (error) return reject(error);
try {
resolve(body);
} catch(e) {
reject(e);
}
});
});
}
}
function toHex(str,hex){
try {
hex = unescape(encodeURIComponent(str)).split('').map(function(v) {
return v.charCodeAt(0).toString(16)
}).join('')
} catch(e) {
hex = str
console.log('invalid text input: ' + str)
}
return hex
}