-
Notifications
You must be signed in to change notification settings - Fork 14
/
cosmos.js
94 lines (82 loc) · 2.57 KB
/
cosmos.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
import * as bip39 from 'bip39'
import cosmosjs from "@cosmostation/cosmosjs"
const secp256k1 = require('secp256k1')
const CHAIN_ID = "signed-message-v1"
function get_verification_inner_string(message) {
// Returns a serialized string to verify the message integrity
return `${message.chain}\n${message.sender}\n${message.type}\n${message.item_hash}`
}
export function get_signable_message(message) {
let signable = get_verification_inner_string(message)
let content_message = {
"type": "signutil/MsgSignText",
"value": {
"message": signable,
"signer": message['sender'],
}
}
return {
"chain_id": CHAIN_ID,
"account_number": "0",
"fee": {
"amount": [],
"gas": "0",
},
"memo": "",
"sequence": "0",
"msgs": [content_message]
}
}
export async function sign(account, message) {
let signable = get_signable_message(message)
if (account.source == 'integrated') {
const cosmos = cosmosjs.network("...", CHAIN_ID)
let signed = cosmos.sign(cosmos.newStdMsg(signable), Buffer.from(account.private_key, 'hex'))
message['signature'] = JSON.stringify(signed['tx']['signatures'][0])
} else if (account.source == "function") {
message['signature'] = await account.signer(account, message, signable)
}
return message
}
export async function new_account({path = "m/44'/118'/0'/0/0", prefix = "cosmos"} = {}) {
let mnemonics = bip39.generateMnemonic()
return import_account({
'mnemonics': mnemonics,
'path': path,
'prefix': prefix
})
}
export function private_key_to_public_key (prv) {
return secp256k1.publicKeyCreate(prv)
}
export async function import_account({
mnemonics = null, path = "m/44'/118'/0'/0/0", prefix = "cosmos",
name = null} = {}){
const cosmos = cosmosjs.network("...", CHAIN_ID)
cosmos.setBech32MainPrefix(prefix)
cosmos.setPath(path)
let private_key = cosmos.getECPairPriv(mnemonics)
let account = {
'private_key': private_key.toString('hex'),
'public_key': private_key_to_public_key(private_key).toString('hex'),
'mnemonics': mnemonics,
'address': cosmos.getAddress(mnemonics),
'prefix': prefix,
'path': path,
'type': 'CSDK',
'source': 'integrated'
}
return account
}
export async function from_external_signer({
address = null, name = null, signer = null, public_key = null} = {}) {
let account = {
'public_key': public_key,
'address': address,
'type': 'CSDK',
'source': 'function',
'signer': signer,
'name': name
}
return account
}