Skip to content

Commit 26ee228

Browse files
committed
feat(sdk): expose data contract from json
1 parent f9d09e7 commit 26ee228

File tree

4 files changed

+142
-2
lines changed

4 files changed

+142
-2
lines changed

packages/js-evo-sdk/src/sdk.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,3 +145,4 @@ export { GroupFacade } from './group/facade.js';
145145
export { VotingFacade } from './voting/facade.js';
146146
export { wallet } from './wallet/functions.js';
147147
export { verifyIdentityResponse, verifyDataContract, verifyDocuments, start } from './wasm.js';
148+
export { DataContract } from './wasm.js';

packages/wasm-sdk/src/dpp.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ impl IdentityWasm {
294294
// }
295295
// }
296296

297-
#[wasm_bindgen]
297+
#[wasm_bindgen(js_name=DataContract)]
298298
pub struct DataContractWasm(DataContract);
299299

300300
impl From<DataContract> for DataContractWasm {
@@ -303,12 +303,30 @@ impl From<DataContract> for DataContractWasm {
303303
}
304304
}
305305

306-
#[wasm_bindgen]
306+
#[wasm_bindgen(js_class=DataContract)]
307307
impl DataContractWasm {
308308
pub fn id(&self) -> String {
309309
self.0.id().to_string(Encoding::Base58)
310310
}
311311

312+
#[wasm_bindgen(js_name=fromJSON)]
313+
pub fn from_json(json: &JsValue, platform_version: u32) -> Result<DataContractWasm, WasmSdkError> {
314+
let platform_version = &PlatformVersion::get(platform_version).map_err(|e| {
315+
WasmSdkError::invalid_argument(format!(
316+
"unknown platform version {platform_version}: {e}"
317+
))
318+
})?;
319+
320+
let data_contract = DataContract::from_json(serde_wasm_bindgen::from_value(json.clone()).map_err(|e| {
321+
WasmSdkError::serialization(format!("failed to convert json: {}", e))
322+
})?, true, platform_version)
323+
.map_err(|e| {
324+
WasmSdkError::serialization(format!("failed to create DataContract from json: {}", e))
325+
})?;
326+
327+
Ok(data_contract.into())
328+
}
329+
312330
#[wasm_bindgen(js_name=toJSON)]
313331
pub fn to_json(&self) -> Result<JsValue, WasmSdkError> {
314332
let platform_version = PlatformVersion::first();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import init, * as sdk from '../../dist/sdk.compressed.js';
2+
import contractFixture from './fixtures/data-contract-crypto-card-game.mjs';
3+
4+
const PLATFORM_VERSION = 1;
5+
6+
describe('DataContract', () => {
7+
before(async () => {
8+
await init();
9+
});
10+
11+
it('should create a contract from JSON and expose identifiers', async () => {
12+
const contract = sdk.DataContract.fromJSON(contractFixture, PLATFORM_VERSION);
13+
14+
expect(contract).to.be.ok();
15+
expect(contract.id()).to.equal(contractFixture.id);
16+
17+
const roundTripped = contract.toJSON();
18+
expect(roundTripped).to.be.an('object');
19+
expect(roundTripped.id).to.equal(contractFixture.id);
20+
21+
contract.free();
22+
});
23+
});
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
const contract = {
2+
$format_version: '0',
3+
id: '86LHvdC1Tqx5P97LQUSibGFqf2vnKFpB6VkqQ7oso86e',
4+
ownerId: '2QjL594djCH2NyDsn45vd6yQjEDHupMKo7CEGVTHtQxU',
5+
version: 1,
6+
config: {
7+
$format_version: '0',
8+
canBeDeleted: false,
9+
readonly: false,
10+
keepsHistory: false,
11+
documentsKeepHistoryContractDefault: false,
12+
documentsMutableContractDefault: true,
13+
documentsCanBeDeletedContractDefault: true,
14+
requiresIdentityEncryptionBoundedKey: null,
15+
requiresIdentityDecryptionBoundedKey: null,
16+
},
17+
documentSchemas: {
18+
card: {
19+
type: 'object',
20+
documentsMutable: false,
21+
canBeDeleted: true,
22+
transferable: 1,
23+
tradeMode: 1,
24+
properties: {
25+
name: {
26+
type: 'string',
27+
description: 'Name of the card',
28+
maxLength: 63,
29+
position: 0,
30+
},
31+
description: {
32+
type: 'string',
33+
description: 'Description of the card',
34+
maxLength: 256,
35+
position: 1,
36+
},
37+
imageUrl: {
38+
type: 'string',
39+
description: 'URL of the image associated with the card',
40+
maxLength: 2048,
41+
format: 'uri',
42+
position: 2,
43+
},
44+
imageHash: {
45+
type: 'array',
46+
description: 'SHA256 hash of the bytes of the image specified by imageUrl',
47+
byteArray: true,
48+
minItems: 32,
49+
maxItems: 32,
50+
position: 3,
51+
},
52+
rarity: {
53+
type: 'string',
54+
description: 'Rarity level of the card',
55+
enum: [
56+
'common',
57+
'uncommon',
58+
'rare',
59+
'legendary',
60+
],
61+
position: 4,
62+
},
63+
},
64+
required: [
65+
'$createdAt',
66+
'$updatedAt',
67+
'name',
68+
'description',
69+
'imageUrl',
70+
'imageHash',
71+
'rarity',
72+
],
73+
additionalProperties: false,
74+
indices: [
75+
{
76+
name: 'name',
77+
properties: [
78+
{
79+
name: 'asc',
80+
},
81+
],
82+
unique: true,
83+
},
84+
{
85+
name: 'rarity',
86+
properties: [
87+
{
88+
rarity: 'asc',
89+
},
90+
],
91+
unique: false,
92+
},
93+
],
94+
},
95+
},
96+
};
97+
98+
export default contract;

0 commit comments

Comments
 (0)