Skip to content

Commit b3e84fe

Browse files
committed
Merge branch 'main' of github.com-mateuszmlc:polybase/docs into eng-363-allow-array-collection-type-delegates
2 parents 3fe457d + d01cfff commit b3e84fe

File tree

10 files changed

+197
-18
lines changed

10 files changed

+197
-18
lines changed

api-reference/call-function.mdx

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Call a function"
3-
api: "POST https://testnet.polybase.xyz/v0/collections/<collection_path>/records/<record_id>/call/<function_name>"
3+
api: "POST https://testnet.polybase.xyz/v0/collections/{collection_path}/records/{record_id}/call/{function_name}"
44
---
55

66
<RequestExample>
@@ -9,6 +9,9 @@ api: "POST https://testnet.polybase.xyz/v0/collections/<collection_path>/records
99
curl --request POST \
1010
--url https://testnet.polybase.xyz/v0/collections/polybase%2Fcore%2Fusers/records/id123/call/functionName\
1111
--header 'Accept: application/json'
12+
-d '{ "args": ["Polybase"] }'
13+
# optional (if supplied ctx.publicKey will be populated)
14+
--header 'X-Polybase-Signature: v=0,t=1671884992,h=eth-personal-sign,sig=0x288db6271d92253ae19983a8e5110f1bc1bef1911210127ccbf657d85428ba9917aa9457f0f8e7f300a36b106525d3a3471e63ae265af4898742040a377e1da11c'
1215
```
1316

1417
</RequestExample>
@@ -32,4 +35,32 @@ curl --request POST \
3235

3336
<ParamField body="args" type="array" required>
3437
An array of arguments that should be passed to the the function being called on the collection, as defined in the collection schema.
38+
39+
For example, if the collection schema is as follows:
40+
41+
```js
42+
collection Users {
43+
id: string;
44+
name: string;
45+
46+
constructor (name: string) {
47+
this.id = PublicKey;
48+
this.name = name;
49+
}
50+
51+
setName(name: string) {
52+
this.name = name;
53+
}
54+
}
55+
```
56+
57+
The args would be:
58+
```json
59+
{
60+
args: [
61+
// This is the 1st parameter name: string
62+
"Polybase"
63+
]
64+
}
65+
```
3566
</ParamField>

api-reference/create-record.mdx

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Create a record"
3-
api: "POST https://testnet.polybase.xyz/v0/collections/<collection_path>/records"
3+
api: "POST https://testnet.polybase.xyz/v0/collections/{collection_path}/records"
44
---
55

66
<RequestExample>
@@ -9,6 +9,9 @@ api: "POST https://testnet.polybase.xyz/v0/collections/<collection_path>/records
99
curl --request POST \
1010
--url https://testnet.polybase.xyz/v0/collections/polybase%2Fcore%2Fusers/records\
1111
--header 'Accept: application/json'
12+
-d '{ "args": ["Polybase"] }'
13+
# optional (if supplied ctx.publicKey will be populated)
14+
--header 'X-Polybase-Signature: v=0,t=1671884992,h=eth-personal-sign,sig=0x288db6271d92253ae19983a8e5110f1bc1bef1911210127ccbf657d85428ba9917aa9457f0f8e7f300a36b106525d3a3471e63ae265af4898742040a377e1da11c'
1215
```
1316

1417
</RequestExample>
@@ -22,5 +25,31 @@ curl --request POST \
2225
## Body Params
2326

2427
<ParamField body="args" type="array" required>
25-
An array of arguments that should be passed to the the constructor of the collection, as defined in the collection schema.
28+
An array of arguments that should be passed to the the `constructor` of the collection, as defined in the collection schema. For example, if the collection schema is as follows:
29+
30+
```js
31+
collection Users {
32+
id: string;
33+
name: string;
34+
age: number;
35+
36+
constructor (name: string, age: number) {
37+
this.id = PublicKey;
38+
this.name = name;
39+
this.age = age;
40+
}
41+
}
42+
```
43+
44+
The args would be:
45+
```json
46+
{
47+
args: [
48+
// This is the 1st parameter name: string
49+
"Polybase",
50+
// This is the 2nd parameter age: number
51+
1
52+
]
53+
}
54+
```
2655
</ParamField>

api-reference/get-record.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "Get record"
3-
api: "GET https://testnet.polybase.xyz/v0/collections/<collection_path>/records/<record_id>"
3+
api: "GET https://testnet.polybase.xyz/v0/collections/{collection_path}/records/<record_id>"
44
---
55

66
<RequestExample>

api-reference/list-records.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "List records"
3-
api: "GET https://testnet.polybase.xyz/v0/collections/<collection_path>/records"
3+
api: "GET https://testnet.polybase.xyz/v0/collections/{collection_path}/records"
44
---
55

66
<RequestExample>

api-reference/overview.mdx

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,10 @@ The Polybase Gatweay API uses public/private key pair for authentication. That m
1515
Authentication is composed of:
1616

1717
- `v` the version being used, should be `v=0`
18-
- `t` current unix timestamp in seconds
18+
- `t` current unix timestamp in milliseconds
1919
- `h` type of hash signing being used `eth-personal-sign` is currently the only allowed option
20-
- `sig` the signature of the data
20+
- `sig` the signature of the data (see below)
21+
- `pk` **optional** 65 byte public key (secp256k1 algorithm)
2122

2223

2324
An example of a valid authroization:
@@ -26,4 +27,24 @@ An example of a valid authroization:
2627
v=0,t=1671884992,h=eth-personal-sign,sig=0x288db6271d92253ae19983a8e5110f1bc1bef1911210127ccbf657d85428ba9917aa9457f0f8e7f300a36b106525d3a3471e63ae265af4898742040a377e1da11c
2728
```
2829

29-
The token is passed in the `X-Polybase-Signature` header of the request.
30+
The token is passed in the `X-Polybase-Signature` header of the request.
31+
32+
33+
### Signature (`sig`)
34+
35+
To generate `sig` you must sign a string in the format `<timestamp>.<body_json>`. Where `timestamp` is the `t` you provided above, and `body_json` is the body of the request (as a JSON string).
36+
37+
You can use the [@polybase/util](https://www.npmjs.com/package/@polybase/util) package to generate the signature from any 32 bytes private key, and the sign function to create the signature.
38+
39+
```typescript
40+
import { secp256k1 } from '@polybase/util'
41+
42+
// You can use any 32 byte private key from any library,
43+
// here is an example from @polybase/util
44+
let private_key = secp256k1.generatePrivateKey();
45+
46+
function createSig (timestamp, body) {
47+
const str_to_sign = `${timestamp}.${JSON.stringify(body)}`;
48+
return secp256k1.sign(privateKey, str_to_sign);
49+
}
50+
```

changelog.mdx

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,37 @@ breaking changes.
1818
- Array can be of Collection type
1919
- Array of Collection type can be used with read/call/delegate directives
2020

21+
## \[0.3.26] - 2023-03-07
22+
23+
### Fixed
24+
25+
- Fixed "Failed to fetch metadata" in the explorer
26+
27+
### Changed
28+
29+
- The polybase-ts client libraries are now natively web-compatible
30+
2131
## \[0.3.23\] - 2023-01-26
2232

2333
### Fixed
2434

2535
- Fixed invalid dependencies in CDN bundles
2636

2737

38+
## \[0.3.23\] - 2023-01-21
39+
40+
### Added
41+
42+
- Authorization rules using `@public`, `@read`, `@call` and `@delegate`
43+
44+
45+
## \[0.3.23\] - 2023-01-21
46+
47+
### Added
48+
49+
- New collection interface AST that is now stored in the database. Clients do not need to parse collection code anymore
50+
51+
2852
## \[0.3.22\] - 2023-01-17
2953

3054
### Added

collections.mdx

Lines changed: 73 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,19 @@ db.signer((data) => {
4141
})
4242

4343
const createResponse = await db.applySchema(`
44+
@public
4445
collection CollectionName {
4546
id: string;
4647
country?: string;
47-
publicKey?: string;
48+
publicKey?: PublicKey;
4849
4950
constructor (id: string, country: string) {
5051
this.id = id;
5152
this.country = country;
5253
5354
// Assign the public key of the user making the request to this record
54-
this.publicKey = ctx.publicKey;
55+
if (ctx.publicKey)
56+
this.publicKey = ctx.publicKey;
5557
}
5658
}
5759
`, 'your-namespace') // your-namespace is optional if you have defined a default namespace
@@ -76,6 +78,7 @@ indexes for your collection records. The following is a valid collection
7678
definition.
7779

7880
```js
81+
@public
7982
collection CollectionName {
8083
id: string;
8184
name?: string;
@@ -107,6 +110,7 @@ at the top most part of your collection. A collection should always have a `id`
107110
field (`id` is unique). By default, all fields are required.
108111

109112
```js
113+
@public
110114
collection CollectionName {
111115
id: string;
112116
age: number;
@@ -137,6 +141,7 @@ All fields are required by default, if you want to make a field optional simply
137141
append `?` after the field name and before the `:`. For example:
138142

139143
```js
144+
@public
140145
collection CollectionName {
141146
id: string;
142147
required: number;
@@ -151,14 +156,15 @@ collection CollectionName {
151156
Field values can only be modified using functions.
152157

153158
```js
159+
@public
154160
collection Account {
155161
id: string;
156162
name: string;
157163
balance: number;
158-
publicKey: string;
164+
publicKey: PublicKey;
159165

160166
constructor (name: string) {
161-
this.id = ctx.publicKey;
167+
this.id = ctx.publicKey.toHex();
162168
this.name = name;
163169
this.publicKey = ctx.publicKey;
164170
this.balance = 0;
@@ -223,11 +229,12 @@ it against the records own `publicKey`:
223229
collection CollectionName {
224230
id: string;
225231
name?: string;
226-
publicKey?: string;
232+
publicKey?: PublicKey;
227233

228234
constructor (id: string) {
229235
this.id = id;
230-
this.publicKey = ctx.publicKey;
236+
if (ctx.publicKey)
237+
this.publicKey = ctx.publicKey;
231238
}
232239

233240
setName (name: string) {
@@ -260,6 +267,7 @@ const records = await collectionReference
260267
You would need the following schema:
261268

262269
```js
270+
@public
263271
collection cities {
264272
name: string;
265273
country: string;
@@ -284,6 +292,7 @@ const records = await collectionReference
284292
You would need the following schema:
285293

286294
```js
295+
@public
287296
collection cities {
288297
name: string;
289298
country: string;
@@ -293,6 +302,64 @@ collection cities {
293302
}
294303
```
295304

305+
## Authorization
306+
307+
By default, collections in Polybase are private and their records cannot be accessed.
308+
To make a collection private, remove the `@public` directive.
309+
310+
To allow users to access and manipulate their records, you can use the following directives:
311+
312+
### `@read`
313+
314+
Allows anyone who can sign using the specified public key to read the record.
315+
316+
```js
317+
collection Response {
318+
@read
319+
publicKey: PublicKey;
320+
...
321+
}
322+
```
323+
324+
### `@call`
325+
326+
Allows anyone who can sign using the specified public key to call a given function.
327+
328+
```js
329+
collection Form {
330+
@read
331+
creator: PublicKey;
332+
333+
@call(creator);
334+
update () {
335+
...
336+
}
337+
}
338+
```
339+
340+
### `@delegate`
341+
342+
Allows anyone who can sign using the specified public key to read the response data, via delegated permissions.
343+
344+
Example of delegating `Response``Form``User``publicKey`:
345+
346+
```js
347+
collection Response {
348+
@read
349+
form: Form;
350+
}
351+
352+
collection Form {
353+
@delegate
354+
creator: User;
355+
}
356+
357+
collection User {
358+
@delegate
359+
publicKey: PublicKey;
360+
}
361+
```
362+
296363
## Reference a collection
297364

298365
To reference a collection, you can call `.collection("collection-name")` on your

delete-data.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ The following provides an example of a delete function that only allows the
1010
owner of the record to delete it.
1111

1212
```js
13+
@public
1314
collection Place {
1415
country: string;
1516
owner: id;
1617

1718
constructor (id: string) {
1819
this.id = id;
19-
this.owner = this.ctx.publicKey;
20+
this.owner = ctx.publicKey.toHex();
2021
}
2122

2223
del () {

get-started.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ In this example we use the JavaScript SDK to create a collection called `City` t
6262
6363
```js
6464
await db.applySchema(`
65+
@public
6566
collection City {
6667
id: string;
6768
name: string;

0 commit comments

Comments
 (0)