Skip to content

Commit fd0e179

Browse files
author
neo
committed
1. add some globel function
2. add token support 3. version update
1 parent 21dd338 commit fd0e179

File tree

12 files changed

+148
-38
lines changed

12 files changed

+148
-38
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "mixin-node-sdk",
3-
"version": "3.0.1",
3+
"version": "3.0.2",
44
"license": "MIT",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",
@@ -81,4 +81,4 @@
8181
"ws": "^8.2.0",
8282
"zlib": "^1.0.5"
8383
}
84-
}
84+
}

src/client/asset.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { AxiosInstance } from "axios";
2-
import { Asset, AssetClientRequest, ExchangeRate } from "../types";
2+
import { request } from "../services/request";
3+
import { Asset, AssetClientRequest, ExchangeRate, NetworkTicker } from "../types";
34

45

56
export class AssetClient implements AssetClientRequest {
@@ -20,4 +21,16 @@ export class AssetClient implements AssetClientRequest {
2021
readExchangeRates(): Promise<ExchangeRate[]> {
2122
return this.request.get('/fiats')
2223
}
23-
}
24+
25+
readAssetNetworkTicker(asset_id: string, offset?: string): Promise<NetworkTicker> {
26+
return this.request.get(`/network/ticker`, { params: { offset, asset_id } })
27+
}
28+
}
29+
30+
export const readAssets = (token: string): Promise<Asset[]> =>
31+
request(undefined, token).get('/assets')
32+
33+
export const readAsset = (token: string, assetID: string): Promise<Asset> =>
34+
request(undefined, token).get(`/assets/${assetID}`)
35+
36+

src/client/conversation.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { AxiosInstance } from "axios";
2+
import { request } from "../services/request";
23
import {
34
ConversationClientRequest, ConversationCreateParmas, Conversation, ConversationUpdateParams, Participant, ConversationAction, Keystore
45
} from '../types'
@@ -50,4 +51,7 @@ export class ConversationClient implements ConversationClientRequest {
5051
rotateConversation(conversationID: string): Promise<Conversation> {
5152
return this.request.post(`/conversations/${conversationID}/rotate`)
5253
}
53-
}
54+
}
55+
56+
export const readConversation = (token: string, conversation_id: string): Promise<Conversation> =>
57+
request(undefined, token).get(`conversations/${conversation_id}`)

src/client/index.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import crypto from 'crypto'
22
import { v4 as uuid } from 'uuid'
33
import { AxiosInstance } from 'axios'
4-
import { request } from '../services/request'
4+
import { mixinRequest, request } from '../services/request'
55
import { UserClient } from './user'
66
import { AddressClient } from './address'
77

88

99
import {
1010
AddressClientRequest, AddressCreateParams, Address,
1111
AppClientRequest, UpdateAppRequest, App, FavoriteApp,
12+
AssetClientRequest, Asset, ExchangeRate, NetworkTicker,
1213
Attachment, AttachmentClientRequest,
1314
ConversationClientRequest, ConversationCreateParmas, Conversation, ConversationUpdateParams, Participant, ConversationAction,
1415
MessageClientRequest, AcknowledgementRequest, MessageRequest,
1516
MultisigClientRequest, MultisigRequest, MultisigUTXO,
1617
PINClientRequest, Turn,
17-
SnapshotClientRequest, Snapshot,
18+
SnapshotClientRequest, Snapshot, SnapshotQuery,
1819
TransferClientRequest, TransferInput, Payment, GhostInput, GhostKeys, WithdrawInput, RawTransaction,
19-
UserClientRequest, User, UserRelationship, Keystore, AssetClientRequest, Asset, ExchangeRate
20+
UserClientRequest, User, UserRelationship, Keystore
2021
} from '../types'
2122
import { AppClient } from './app'
2223
import { AssetClient } from './asset'
@@ -65,6 +66,7 @@ export class Client implements
6566
readAsset!: (asset_id: string) => Promise<Asset>
6667
readAssets!: () => Promise<Asset[]>
6768
readAssetFee!: (asset_id: string) => Promise<number>
69+
readAssetNetworkTicker!: (asset_id: string, offset?: string) => Promise<NetworkTicker>
6870

6971
readExchangeRates!: () => Promise<ExchangeRate[]>
7072

@@ -107,14 +109,12 @@ export class Client implements
107109
readTurnServers!: () => Promise<Turn[]>
108110

109111
// Snapshot...
110-
111-
readSnapshots!: (asset_id?: string, offset?: string, order?: string, limit?: number) => Promise<Snapshot[]>
112-
readNetworkSnapshots!: (asset_id?: string, offset?: string, order?: string, limit?: number) => Promise<Snapshot[]>
112+
readSnapshots!: (params: SnapshotQuery) => Promise<Snapshot[]>
113+
readNetworkSnapshots!: (params: SnapshotQuery) => Promise<Snapshot[]>
113114
readSnapshot!: (snapshot_id: string) => Promise<Snapshot>
114115
readNetworkSnapshot!: (snapshot_id: string) => Promise<Snapshot>
115116

116117
// Transfer...
117-
118118
verifyPayment!: (params: TransferInput) => Promise<Payment>
119119
transfer!: (params: TransferInput, pin?: string) => Promise<Snapshot>
120120
readTransfer!: (trace_id: string) => Promise<Snapshot>
@@ -134,6 +134,17 @@ export class Client implements
134134
modifyRelationships!: (relationship: UserRelationship) => Promise<User>
135135
readBlockUsers!: () => Promise<User[]>
136136

137+
138+
// Oauth...
139+
authorizeToken(code: string, client_secret?: string, code_verifier?: string): Promise<{ access_token: string, scope: string }> {
140+
if (!client_secret) client_secret = this.keystore.client_secret
141+
if (!client_secret) return Promise.reject(new Error('client_secret required'))
142+
return this.request.post('/oauth/token', {
143+
client_secret, code, code_verifier,
144+
client_id: this.keystore.client_id,
145+
})
146+
}
147+
137148
newUUID(): string {
138149
return uuid()
139150
}
@@ -174,4 +185,7 @@ function _extends(origin: any, target: any) {
174185
for (const key in target.prototype) {
175186
origin.prototype[key] = target.prototype[key]
176187
};
177-
}
188+
}
189+
190+
export const authorizeToken = (client_id: string, code: string, client_secret: string, code_verifier?: string): Promise<{ access_token: string, scope: string }> =>
191+
mixinRequest.get('/oauth/token', { params: { client_id, code, code_verifier, client_secret } })

src/client/network.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,21 @@
1+
import { mixinRequest } from "../services/request";
2+
import { Asset, Snapshot, SnapshotQuery, Transaction } from "../types";
13

4+
export const readNetworkChains = (): Promise<Asset[]> => mixinRequest.get("/network/chains")
5+
6+
// only support limit/offset/asset/order
7+
export const readNetworkSnapshots = (params: SnapshotQuery): Promise<Snapshot[]> => mixinRequest.get("/network/snapshots", { params })
8+
9+
export const readNetworkSnapshot = (id: string): Promise<Snapshot> => mixinRequest.get(`/network/snapshots/${id}`)
10+
11+
export const readExternalTransactions = (params: SnapshotQuery): Promise<Transaction[]> => mixinRequest.get("/external/transactions", { params })
12+
13+
export const readNetworkAssetsTop = (): Promise<Asset[]> => mixinRequest.get("/network/assets/top")
14+
15+
export const readNetworkAssetsMultisig = (): Promise<Asset[]> => mixinRequest.get("/network/assets/multisig")
16+
17+
export const readNetworkAsset = (id: string): Promise<Asset> => mixinRequest.get(`/network/assets/${id}`)
18+
19+
export const searchNetworkAsset = (assetNameOrSymbol: string): Promise<Asset[]> => mixinRequest.get(`/network/assets/search/${assetNameOrSymbol}`)
20+
21+
export const readExternalAddressesCheck = (params: SnapshotQuery): Promise<boolean> => mixinRequest.get(`/external/addresses/check`, { params })

src/client/snapshot.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
import { AxiosInstance } from "axios";
2-
import { Snapshot, SnapshotClientRequest } from "../types";
2+
import { request } from "../services/request";
3+
import { Snapshot, SnapshotClientRequest, SnapshotQuery } from "../types";
34

45

56
export class SnapshotClient implements SnapshotClientRequest {
67
request!: AxiosInstance
78

8-
readSnapshots(asset_id?: string, offset?: string, order?: string, limit?: number): Promise<Snapshot[]> {
9-
return this.request.get(`/snapshots`, { params: { asset_id, offset, order, limit } })
9+
readSnapshots(params: SnapshotQuery): Promise<Snapshot[]> {
10+
return this.request.get(`/snapshots`, { params })
1011
}
11-
readNetworkSnapshots(asset_id?: string, offset?: string, order?: string, limit?: number): Promise<Snapshot[]> {
12-
return this.request.get(`/network/snapshots`, { params: { asset_id, offset, order, limit } })
12+
readNetworkSnapshots(params: SnapshotQuery): Promise<Snapshot[]> {
13+
return this.request.get(`/network/snapshots`, { params })
1314
}
1415
readSnapshot(snapshot_id: string): Promise<Snapshot> {
1516
return this.request.get(`/snapshots/${snapshot_id}`)
1617
}
1718
readNetworkSnapshot(snapshot_id: string): Promise<Snapshot> {
1819
return this.request.get(`/network/snapshots/${snapshot_id}`)
1920
}
20-
}
21+
}
22+
23+
export const readSnapshots = (token: string, params: SnapshotQuery): Promise<Snapshot[]> =>
24+
request(undefined, token).get("/snapshots", { params })
25+
26+
export const readSnapshot = (token: string, snapshot_id: string): Promise<Snapshot> =>
27+
request(undefined, token).get(`/snapshots/${snapshot_id}`)

src/client/user.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { AxiosInstance } from 'axios'
22
import { UserClientRequest, User, UserRelationship } from '../types/user'
33
import forge from 'node-forge'
4+
import { request } from '../services/request'
45

56
export class UserClient implements UserClientRequest {
67
request!: AxiosInstance
@@ -37,4 +38,13 @@ export class UserClient implements UserClientRequest {
3738
modifyRelationships(relationship: UserRelationship): Promise<User> {
3839
return this.request.post(`/relationships`, relationship)
3940
}
40-
}
41+
}
42+
43+
export const userMe = (token: string): Promise<User> =>
44+
request(undefined, token).get('/me')
45+
46+
export const readFriends = (token: string): Promise<User[]> =>
47+
request(undefined, token).get(`/friends`)
48+
49+
export const readBlockUsers = (token: string): Promise<User[]> =>
50+
request(undefined, token).get(`/blocking_users`)

src/index.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1-
import { Client } from './client'
2-
import { BlazeClient } from './client/blaze'
3-
export { Client, BlazeClient }
1+
export { Client } from './client'
2+
export { BlazeClient } from './client/blaze'
3+
export { readAsset, readAssets } from './client/asset'
4+
export { readConversation } from './client/conversation'
5+
export { readSnapshot, readSnapshots } from './client/snapshot'
6+
export { userMe, readBlockUsers, readFriends } from './client/user'
7+
export * from './client/network'
8+
export { request, mixinRequest } from './services/request'

src/services/request.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@ export const request = (keystore?: Keystore, token = ''): AxiosInstance => {
4747
return ins
4848
}
4949

50+
export const mixinRequest = request()

src/types/asset.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,29 @@ export interface Asset {
1414
destination: string
1515
tag: string
1616
confirmations: number
17-
capitalization: number
17+
capitalization?: number
18+
amount?: string
19+
fee?: string
20+
liquidity?: string
21+
snapshots_count: number
1822
}
1923

2024
export interface ExchangeRate {
2125
code: string
2226
rate: string
2327
}
2428

29+
export interface NetworkTicker {
30+
type: 'ticker'
31+
price_usd: string
32+
price_btc: string
33+
}
2534

2635
export interface AssetClientRequest {
2736
readAsset(asset_id: string): Promise<Asset>
2837
readAssets(): Promise<Asset[]>
2938
readAssetFee(asset_id: string): Promise<number>
39+
readAssetNetworkTicker(asset_id: string, offset?: string): Promise<NetworkTicker>
3040

3141
readExchangeRates(): Promise<ExchangeRate[]>
3242
}

src/types/snapshot.ts

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,48 @@
11
import { Asset } from './asset'
22
export interface Snapshot {
3+
type: string
34
snapshot_id: string
4-
created_at: string
55
trace_id: string
6-
user_id: string
6+
user_id?: string
77
asset_id: string
8-
chain_id: string
9-
opponent_id: string
8+
created_at: string
9+
opponent_id?: string
1010
source: string
1111
amount: string
12-
opening_balance: string
13-
closing_balance: string
1412
memo: string
15-
type: string
16-
sender: string
17-
receiver: string
18-
transaction_hash: string
13+
chain_id?: string
14+
opening_balance?: string
15+
closing_balance?: string
16+
sender?: string
17+
receiver?: string
18+
transaction_hash?: string
1919

2020
asset?: Asset
2121
data?: string
22+
fee?: {
23+
amount: string
24+
asset_id: string
25+
}
26+
}
27+
28+
export interface SnapshotQuery {
29+
limit?: number | string
30+
offset?: string
31+
asset?: string
32+
opponent?: string
33+
tag?: string
34+
destination?: string // query external transactions
2235
}
2336

2437

2538
export interface SnapshotClientRequest {
26-
readSnapshots(asset_id?: string, offset?: string, order?: string, limit?: number): Promise<Snapshot[]>
27-
readNetworkSnapshots(asset_id?: string, offset?: string, order?: string, limit?: number): Promise<Snapshot[]>
39+
readSnapshots(params?: SnapshotQuery): Promise<Snapshot[]>
40+
readNetworkSnapshots(params?: SnapshotQuery): Promise<Snapshot[]>
2841
readSnapshot(snapshot_id: string): Promise<Snapshot>
2942
readNetworkSnapshot(snapshot_id: string): Promise<Snapshot>
3043
}
3144

3245
export interface SnapshotRequest {
33-
ReadSnapshots(token: string, asset_id?: string, offset?: string, order?: string, limit?: number): Promise<Snapshot[]>
46+
ReadSnapshots(token: string, params?: SnapshotQuery): Promise<Snapshot[]>
3447
ReadSnapshot(token: string, snapshot_id: string): Promise<Snapshot>
3548
}

src/types/transfer.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,20 @@ export interface Payment {
1414
code_id: string
1515
}
1616

17-
export interface Transfer {
17+
export interface Transaction {
18+
type: 'transaction'
19+
transaction_id: string
20+
transaction_hash: string
21+
sender: string
22+
chain_id: string
23+
asset_id: string
24+
amount: string
25+
destination: string
26+
tag: string
27+
created_at: string
28+
output_index: number,
29+
confirmations: number,
30+
threshold: number,
1831
}
1932

2033
export interface RawTransaction {

0 commit comments

Comments
 (0)