hive-keychain-commons 1.4.1 → 1.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/config/index.d.ts +6 -0
- package/lib/config/index.js +5 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/interfaces/keychain.d.ts +2 -2
- package/lib/logger/formats.js +24 -1
- package/lib/logger/logger.js +24 -1
- package/lib/logger/transports.js +24 -1
- package/lib/utils/vsc.utils.d.ts +7 -0
- package/lib/utils/vsc.utils.js +141 -0
- package/package.json +2 -2
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -24,3 +24,4 @@ __exportStar(require("./swaps/swap-api.interface"), exports);
|
|
|
24
24
|
__exportStar(require("./swaps/swap.interface"), exports);
|
|
25
25
|
__exportStar(require("./utils/keys.utils"), exports);
|
|
26
26
|
__exportStar(require("./utils/transfer.utils"), exports);
|
|
27
|
+
__exportStar(require("./utils/vsc.utils"), exports);
|
|
@@ -271,14 +271,14 @@ export type RequestVscCallContract = CommonRequestParams & {
|
|
|
271
271
|
payload: object;
|
|
272
272
|
method: KeychainKeyTypes.posting | KeychainKeyTypes.active;
|
|
273
273
|
};
|
|
274
|
-
export type
|
|
274
|
+
export type RequestDeposit = CommonRequestParams & {
|
|
275
275
|
type: KeychainRequestTypes.vscDeposit;
|
|
276
276
|
username?: string;
|
|
277
277
|
address: string;
|
|
278
278
|
amount: string;
|
|
279
279
|
currency: string;
|
|
280
280
|
};
|
|
281
|
-
export type KeychainRequestData = (RequestDecode | RequestEncodeWithKeys | RequestEncode | RequestSignBuffer | RequestBroadcast | RequestAddAccountAuthority | RequestRemoveAccountAuthority | RequestAddKeyAuthority | RequestRemoveKeyAuthority | RequestSignTx | RequestPost | RequestVote | RequestCustomJSON | RequestSignedCall | RequestTransfer | RequestSendToken | RequestDelegation | RequestWitnessVote | RequestProxy | RequestPowerUp | RequestPowerDown | RequestCreateClaimedAccount | RequestUpdateProposalVote | RequestCreateProposal | RequestRemoveProposal | RequestAddAccount | RequestConvert | RequestRecurrentTransfer | RequestSwap | RequestVscCallContract |
|
|
281
|
+
export type KeychainRequestData = (RequestDecode | RequestEncodeWithKeys | RequestEncode | RequestSignBuffer | RequestBroadcast | RequestAddAccountAuthority | RequestRemoveAccountAuthority | RequestAddKeyAuthority | RequestRemoveKeyAuthority | RequestSignTx | RequestPost | RequestVote | RequestCustomJSON | RequestSignedCall | RequestTransfer | RequestSendToken | RequestDelegation | RequestWitnessVote | RequestProxy | RequestPowerUp | RequestPowerDown | RequestCreateClaimedAccount | RequestUpdateProposalVote | RequestCreateProposal | RequestRemoveProposal | RequestAddAccount | RequestConvert | RequestRecurrentTransfer | RequestSwap | RequestVscCallContract | RequestDeposit) & {
|
|
282
282
|
redirect_uri?: string;
|
|
283
283
|
};
|
|
284
284
|
export type RequestId = {
|
package/lib/logger/formats.js
CHANGED
|
@@ -1,9 +1,32 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/* istanbul ignore file */
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
+
}) : function(o, v) {
|
|
17
|
+
o["default"] = v;
|
|
18
|
+
});
|
|
19
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
20
|
+
if (mod && mod.__esModule) return mod;
|
|
21
|
+
var result = {};
|
|
22
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
23
|
+
__setModuleDefault(result, mod);
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
3
26
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
27
|
exports.LoggerFormats = void 0;
|
|
5
28
|
const moment = require("moment");
|
|
6
|
-
const winston = require("winston");
|
|
29
|
+
const winston = __importStar(require("winston"));
|
|
7
30
|
const logFormat = winston.format.printf((info) => {
|
|
8
31
|
const timestamp = moment(info.timestamp);
|
|
9
32
|
return `[${timestamp.format('L') + ' ' + timestamp.format('HH:mm:ss')}][${info.level}] ${info.message}`;
|
package/lib/logger/logger.js
CHANGED
|
@@ -1,8 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/* eslint-disable */
|
|
3
3
|
/* istanbul ignore file */
|
|
4
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
5
|
+
if (k2 === undefined) k2 = k;
|
|
6
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
7
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
8
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
9
|
+
}
|
|
10
|
+
Object.defineProperty(o, k2, desc);
|
|
11
|
+
}) : (function(o, m, k, k2) {
|
|
12
|
+
if (k2 === undefined) k2 = k;
|
|
13
|
+
o[k2] = m[k];
|
|
14
|
+
}));
|
|
15
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
16
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
17
|
+
}) : function(o, v) {
|
|
18
|
+
o["default"] = v;
|
|
19
|
+
});
|
|
20
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
21
|
+
if (mod && mod.__esModule) return mod;
|
|
22
|
+
var result = {};
|
|
23
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
24
|
+
__setModuleDefault(result, mod);
|
|
25
|
+
return result;
|
|
26
|
+
};
|
|
4
27
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
5
|
-
const winston = require("winston");
|
|
28
|
+
const winston = __importStar(require("winston"));
|
|
6
29
|
const transports_1 = require("./transports");
|
|
7
30
|
let winstonLogger;
|
|
8
31
|
let environment;
|
package/lib/logger/transports.js
CHANGED
|
@@ -1,8 +1,31 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
/* istanbul ignore file */
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
+
}) : function(o, v) {
|
|
17
|
+
o["default"] = v;
|
|
18
|
+
});
|
|
19
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
20
|
+
if (mod && mod.__esModule) return mod;
|
|
21
|
+
var result = {};
|
|
22
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
23
|
+
__setModuleDefault(result, mod);
|
|
24
|
+
return result;
|
|
25
|
+
};
|
|
3
26
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
27
|
exports.LoggerTransports = void 0;
|
|
5
|
-
const winston = require("winston");
|
|
28
|
+
const winston = __importStar(require("winston"));
|
|
6
29
|
const formats_1 = require("./formats");
|
|
7
30
|
const DailyRotateFile = require("winston-daily-rotate-file");
|
|
8
31
|
const path = require("path");
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { VscHistoryType, VscStatus } from '../interfaces/vsc';
|
|
2
|
+
export declare const VscUtils: {
|
|
3
|
+
checkStatus: (id: string, type: VscHistoryType) => Promise<VscStatus>;
|
|
4
|
+
waitForStatus: (id: string, type: VscHistoryType) => Promise<VscStatus>;
|
|
5
|
+
getOrganizedHistory: (username: string) => Promise<(import("../interfaces/vsc").VscTransfer | import("../interfaces/vsc").VscCall)[]>;
|
|
6
|
+
getAddressFromDid: (did: string) => string | undefined;
|
|
7
|
+
};
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.VscUtils = void 0;
|
|
16
|
+
const moment_1 = __importDefault(require("moment"));
|
|
17
|
+
const config_1 = __importDefault(require("../config"));
|
|
18
|
+
const vsc_1 = require("../interfaces/vsc");
|
|
19
|
+
const waitForStatus = (id, type) => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
|
+
const MAX_ITERATIONS = 20;
|
|
21
|
+
const WAIT_TIME = 500;
|
|
22
|
+
let iterations = 0;
|
|
23
|
+
let status = vsc_1.VscStatus.UNCONFIRMED;
|
|
24
|
+
while (iterations < MAX_ITERATIONS) {
|
|
25
|
+
status = yield checkStatus(id, type);
|
|
26
|
+
if (status === vsc_1.VscStatus.INCLUDED || status === vsc_1.VscStatus.CONFIRMED)
|
|
27
|
+
return status;
|
|
28
|
+
iterations++;
|
|
29
|
+
yield new Promise((resolve) => setTimeout(resolve, WAIT_TIME));
|
|
30
|
+
}
|
|
31
|
+
return status;
|
|
32
|
+
});
|
|
33
|
+
const checkStatus = (id, type) => {
|
|
34
|
+
let query;
|
|
35
|
+
if (type === vsc_1.VscHistoryType.CONTRACT_CALL) {
|
|
36
|
+
query = `{
|
|
37
|
+
findTransaction(
|
|
38
|
+
filterOptions: {byId:"${id}-0"}
|
|
39
|
+
) {
|
|
40
|
+
txs {
|
|
41
|
+
status
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}`;
|
|
45
|
+
}
|
|
46
|
+
else if (type === vsc_1.VscHistoryType.TRANSFER) {
|
|
47
|
+
query = `{
|
|
48
|
+
findLedgerTXs(filterOptions: {byTxId: "${id}-0"}) {
|
|
49
|
+
txs {
|
|
50
|
+
status
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
}`;
|
|
54
|
+
}
|
|
55
|
+
return fetchQuery(query).then((res) => {
|
|
56
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
57
|
+
return ((_d = (_c = (_b = (_a = res === null || res === void 0 ? void 0 : res.data) === null || _a === void 0 ? void 0 : _a.findTransaction) === null || _b === void 0 ? void 0 : _b.txs) === null || _c === void 0 ? void 0 : _c[0]) === null || _d === void 0 ? void 0 : _d.status) ||
|
|
58
|
+
((_h = (_g = (_f = (_e = res === null || res === void 0 ? void 0 : res.data) === null || _e === void 0 ? void 0 : _e.findLedgerTXs) === null || _f === void 0 ? void 0 : _f.txs) === null || _g === void 0 ? void 0 : _g[0]) === null || _h === void 0 ? void 0 : _h.status);
|
|
59
|
+
});
|
|
60
|
+
};
|
|
61
|
+
const fetchHistory = (username) => __awaiter(void 0, void 0, void 0, function* () {
|
|
62
|
+
const query = `{
|
|
63
|
+
findLedgerTXs(
|
|
64
|
+
filterOptions: {byToFrom: "hive:${username}"}
|
|
65
|
+
) {
|
|
66
|
+
txs {
|
|
67
|
+
amount
|
|
68
|
+
block_height
|
|
69
|
+
from
|
|
70
|
+
id
|
|
71
|
+
memo
|
|
72
|
+
owner
|
|
73
|
+
t
|
|
74
|
+
tk
|
|
75
|
+
status
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
findTransaction(filterOptions: {byAccount: "${username}"}) {
|
|
79
|
+
txs {
|
|
80
|
+
status
|
|
81
|
+
id
|
|
82
|
+
anchored_height
|
|
83
|
+
data {
|
|
84
|
+
action
|
|
85
|
+
contract_id
|
|
86
|
+
op
|
|
87
|
+
payload
|
|
88
|
+
}
|
|
89
|
+
required_auths{value}
|
|
90
|
+
}
|
|
91
|
+
}}
|
|
92
|
+
`;
|
|
93
|
+
return (yield fetchQuery(query)).data;
|
|
94
|
+
});
|
|
95
|
+
const getOrganizedHistory = (username) => __awaiter(void 0, void 0, void 0, function* () {
|
|
96
|
+
const history = yield fetchHistory(username);
|
|
97
|
+
const organizedHistory = [
|
|
98
|
+
...history.findLedgerTXs.txs.map((e) => {
|
|
99
|
+
e.type = vsc_1.VscHistoryType.TRANSFER;
|
|
100
|
+
e.timestamp = blockHeightToTimestamp(e.block_height);
|
|
101
|
+
return e;
|
|
102
|
+
}),
|
|
103
|
+
...history.findTransaction.txs.map((e) => {
|
|
104
|
+
e.type = vsc_1.VscHistoryType.CONTRACT_CALL;
|
|
105
|
+
e.timestamp = blockHeightToTimestamp(e.anchored_height);
|
|
106
|
+
return e;
|
|
107
|
+
}),
|
|
108
|
+
].sort((a, b) => {
|
|
109
|
+
return b.timestamp.getTime() - a.timestamp.getTime();
|
|
110
|
+
});
|
|
111
|
+
return organizedHistory;
|
|
112
|
+
});
|
|
113
|
+
const blockHeightToTimestamp = (height) => {
|
|
114
|
+
const START_BLOCK = 88079516;
|
|
115
|
+
const START_BLOCK_TIME = (0, moment_1.default)('2024-08-16T02:46:48Z');
|
|
116
|
+
return START_BLOCK_TIME.clone()
|
|
117
|
+
.add((height - START_BLOCK) * 3, 'seconds')
|
|
118
|
+
.toDate();
|
|
119
|
+
};
|
|
120
|
+
const getAddressFromDid = (did) => {
|
|
121
|
+
const regex = new RegExp(':([a-zA-Z0-9]*)$');
|
|
122
|
+
const matches = did.match(regex);
|
|
123
|
+
return matches === null || matches === void 0 ? void 0 : matches[matches.length - 1];
|
|
124
|
+
};
|
|
125
|
+
const fetchQuery = (query) => {
|
|
126
|
+
return fetch(config_1.default.vsc.API_URL, {
|
|
127
|
+
method: 'POST',
|
|
128
|
+
headers: {
|
|
129
|
+
'Content-Type': 'application/json',
|
|
130
|
+
},
|
|
131
|
+
body: JSON.stringify({
|
|
132
|
+
query,
|
|
133
|
+
}),
|
|
134
|
+
}).then((res) => res.json());
|
|
135
|
+
};
|
|
136
|
+
exports.VscUtils = {
|
|
137
|
+
checkStatus,
|
|
138
|
+
waitForStatus,
|
|
139
|
+
getOrganizedHistory,
|
|
140
|
+
getAddressFromDid,
|
|
141
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "hive-keychain-commons",
|
|
3
|
-
"version": "1.4.
|
|
3
|
+
"version": "1.4.3",
|
|
4
4
|
"description": "Platform-agnostic functions used in Hive Keychain mobile and extensions",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"types": "lib/index.d.ts",
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"@hiveio/dhive": "^1.2.8",
|
|
48
|
-
"moment": "^2.
|
|
48
|
+
"moment": "^2.30.1",
|
|
49
49
|
"winston": "^3.8.1",
|
|
50
50
|
"winston-daily-rotate-file": "^4.7.1"
|
|
51
51
|
}
|