Skip to content

Commit e06a230

Browse files
authored
Merge pull request #374 from csfloat/feature/inventory-handlers
Adds Inventory Notary Proofs and Bridge Handlers
2 parents 10fcaa6 + 74bf795 commit e06a230

5 files changed

Lines changed: 144 additions & 0 deletions

File tree

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
import {SimpleHandler} from './main';
2+
import {RequestType} from './types';
3+
import {getAccessToken} from '../../alarms/access_token';
4+
5+
export interface FetchSteamPoweredInventoryRequest {
6+
// steam_id needs to correspond to the logged in user
7+
steam_id: string;
8+
app_id: number;
9+
context_id: number;
10+
start_assetid?: string;
11+
count?: number;
12+
get_descriptions?: boolean;
13+
for_trade_offer_verification?: boolean;
14+
language?: string;
15+
get_asset_properties?: boolean;
16+
}
17+
18+
export interface InventoryAsset {
19+
assetid: string;
20+
classid: string;
21+
instanceid: string;
22+
}
23+
24+
export interface InventoryDescriptionItem {
25+
value: string;
26+
name: string;
27+
}
28+
29+
export interface InventoryTag {
30+
category: string;
31+
internal_name: string;
32+
localized_category_name: string;
33+
localized_tag_name: string;
34+
}
35+
36+
export interface AssetProperty {
37+
propertyid: number;
38+
int_value?: string;
39+
float_value?: string;
40+
string_value?: string;
41+
}
42+
43+
export interface AssetPropertyWrapper {
44+
appid: number;
45+
asset_properties: AssetProperty[];
46+
assetid: string;
47+
contextid: string;
48+
}
49+
50+
export interface InventoryDescription {
51+
name: string;
52+
market_hash_name: string;
53+
icon_url: string;
54+
icon_url_large: string;
55+
tradable: boolean;
56+
type: string;
57+
name_color: string;
58+
commodity: boolean;
59+
inspect_link?: string;
60+
classid: string;
61+
instanceid: string;
62+
marketable: boolean;
63+
descriptions: InventoryDescriptionItem[];
64+
tags: InventoryTag[];
65+
}
66+
67+
export interface FetchSteamPoweredInventoryResponse {
68+
assets: InventoryAsset[];
69+
descriptions: InventoryDescription[];
70+
asset_properties?: AssetPropertyWrapper[];
71+
last_assetid?: string;
72+
total_inventory_count: number;
73+
}
74+
75+
export const FetchSteamPoweredInventory = new SimpleHandler<
76+
FetchSteamPoweredInventoryRequest,
77+
FetchSteamPoweredInventoryResponse
78+
>(RequestType.FETCH_STEAM_POWERED_INVENTORY, async (req) => {
79+
const accessToken = await getAccessToken(req.steam_id);
80+
81+
const params = new URLSearchParams({
82+
access_token: accessToken.token,
83+
steamid: req.steam_id,
84+
appid: req.app_id.toString(),
85+
contextid: req.context_id.toString(),
86+
});
87+
88+
if (req.start_assetid) {
89+
params.set('start_assetid', req.start_assetid);
90+
}
91+
92+
if (req.count) {
93+
params.set('count', req.count.toString());
94+
}
95+
96+
if (req.get_descriptions !== undefined) {
97+
params.set('get_descriptions', req.get_descriptions.toString());
98+
}
99+
100+
if (req.for_trade_offer_verification !== undefined) {
101+
params.set('for_trade_offer_verification', req.for_trade_offer_verification.toString());
102+
}
103+
104+
if (req.language) {
105+
params.set('language', req.language);
106+
}
107+
108+
if (req.get_asset_properties !== undefined) {
109+
params.set('get_asset_properties', req.get_asset_properties.toString());
110+
}
111+
112+
const resp = await fetch(
113+
`https://api.steampowered.com/IEconService/GetInventoryItemsWithDescriptions/v1/?${params.toString()}`
114+
);
115+
116+
if (!resp.ok) {
117+
throw new Error(`Invalid response code: ${resp.status}`);
118+
}
119+
120+
const data = await resp.json();
121+
122+
if (!data.response) {
123+
throw new Error('Invalid response from Steam API');
124+
}
125+
126+
return data.response as FetchSteamPoweredInventoryResponse;
127+
});

src/lib/bridge/handlers/handlers.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ import {FetchSlimTrades} from './fetch_slim_trades';
3636
import {NotaryProve} from './notary_prove';
3737
import {FetchNotaryMeta} from './fetch_notary_meta';
3838
import {FetchNotaryToken} from './fetch_notary_token';
39+
import {FetchSteamPoweredInventory} from './fetch_steam_inventory';
3940

4041
export const HANDLERS_MAP: {[key in RequestType]: RequestHandler<any, any>} = {
4142
[RequestType.EXECUTE_SCRIPT_ON_PAGE]: ExecuteScriptOnPage,
@@ -74,4 +75,5 @@ export const HANDLERS_MAP: {[key in RequestType]: RequestHandler<any, any>} = {
7475
[RequestType.NOTARY_PROVE]: NotaryProve,
7576
[RequestType.FETCH_NOTARY_META]: FetchNotaryMeta,
7677
[RequestType.FETCH_NOTARY_TOKEN]: FetchNotaryToken,
78+
[RequestType.FETCH_STEAM_POWERED_INVENTORY]: FetchSteamPoweredInventory,
7779
};

src/lib/bridge/handlers/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ export enum RequestType {
3535
NOTARY_PROVE = 33,
3636
FETCH_NOTARY_META = 34,
3737
FETCH_NOTARY_TOKEN = 35,
38+
FETCH_STEAM_POWERED_INVENTORY = 36,
3839
}

src/lib/notary/types.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ export enum ProofType {
33
TRADE_OFFER = 'trade_offer',
44
TRADE_HISTORY = 'trade_history',
55
TRADE_STATUS = 'trade_status',
6+
INVENTORY = 'inventory',
67
}
78

89
interface ProveRequestPayloads {
@@ -38,6 +39,18 @@ interface ProveRequestPayloads {
3839
get_descriptions?: boolean;
3940
language?: string;
4041
};
42+
// IEconService/GetInventoryItemsWithDescriptions/v1/
43+
[ProofType.INVENTORY]: {
44+
steamid: string;
45+
appid: number;
46+
contextid: number;
47+
get_descriptions?: boolean;
48+
for_trade_offer_verification?: boolean;
49+
language?: string;
50+
start_assetid?: string;
51+
count?: number;
52+
get_asset_properties?: boolean;
53+
};
4154
}
4255

4356
export type NotaryProveRequest = {

src/lib/notary/utils.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const PROOF_BASE_URLS: Record<ProofType, string> = {
66
[ProofType.TRADE_OFFER]: 'https://api.steampowered.com/IEconService/GetTradeOffer/v1/',
77
[ProofType.TRADE_HISTORY]: 'https://api.steampowered.com/IEconService/GetTradeHistory/v1/',
88
[ProofType.TRADE_STATUS]: 'https://api.steampowered.com/IEconService/GetTradeStatus/v1/',
9+
[ProofType.INVENTORY]: 'https://api.steampowered.com/IEconService/GetInventoryItemsWithDescriptions/v1/',
910
};
1011

1112
function buildQueryString(params: Record<string, any>): string {

0 commit comments

Comments
 (0)