Skip to content

Commit ecc8b7e

Browse files
authored
[Seal] SDK v0.3 (#134)
1 parent 0e9a29a commit ecc8b7e

35 files changed

+3364
-10
lines changed

.changeset/famous-jokes-rhyme.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@mysten/seal': minor
3+
---
4+
5+
New interfaces

packages/docs/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@mysten/enoki": "workspace:*",
2323
"@mysten/graphql-transport": "workspace:*",
2424
"@mysten/kiosk": "workspace:*",
25+
"@mysten/seal": "workspace:*",
2526
"@mysten/signers": "workspace:*",
2627
"@mysten/sui": "workspace:*",
2728
"@mysten/suins": "workspace:*",

packages/docs/typedoc.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"../enoki",
88
"../graphql-transport",
99
"../kiosk",
10+
"../seal",
1011
"../signers",
1112
"../suins",
1213
"../suins-toolkit",

packages/seal/CHANGELOG.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# @mysten/seal
2+
3+
## 0.2.0
4+
5+
### Minor Changes
6+
7+
- 0886b3e: export KeyServer type
8+
9+
## 0.1.0
10+
11+
### Minor Changes
12+
13+
- 8ca8df3: First publish
14+
15+
### Patch Changes
16+
17+
- Updated dependencies [95b1ea5]
18+
- @mysten/bcs@1.4.0
19+
- @mysten/sui@1.21.2

packages/seal/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# `@mysten/seal`
2+
3+
**Warning**: This is a DEVELOPER PREVIEW of Seal. It is EXPERIMENTAL and has NO GUARANTEES of uptime
4+
or correctness. USE AT YOUR OWN RISK.

packages/seal/genversion.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright (c) Mysten Labs, Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { readFile, writeFile } from 'fs/promises';
5+
6+
const LICENSE = '// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\n';
7+
8+
const WARNING = '// This file is generated by genversion.mjs. Do not edit it directly.\n\n';
9+
10+
async function main() {
11+
const pkg = JSON.parse(await readFile('./package.json', 'utf8'));
12+
await writeFile(
13+
'src/version.ts',
14+
LICENSE + WARNING + `export const PACKAGE_VERSION = '${pkg.version}';\n`,
15+
);
16+
}
17+
18+
await main();

packages/seal/package.json

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
{
2+
"name": "@mysten/seal",
3+
"version": "0.2.0",
4+
"description": "Seal SDK",
5+
"license": "Apache-2.0",
6+
"author": "Mysten Labs <[email protected]>",
7+
"type": "commonjs",
8+
"main": "./dist/cjs/index.js",
9+
"module": "./dist/esm/index.js",
10+
"types": "./dist/cjs/index.d.ts",
11+
"exports": {
12+
".": {
13+
"import": "./dist/esm/index.js",
14+
"require": "./dist/cjs/index.js"
15+
}
16+
},
17+
"sideEffects": false,
18+
"files": [
19+
"CHANGELOG.md",
20+
"dist"
21+
],
22+
"scripts": {
23+
"clean": "rm -rf tsconfig.tsbuildinfo ./dist",
24+
"build": "node genversion.mjs && build-package",
25+
"codegen:version": "node genversion.mjs",
26+
"vitest": "vitest",
27+
"test": "pnpm test:typecheck && pnpm test:unit",
28+
"test:typecheck": "tsc -p ./test",
29+
"test:unit": "vitest run unit __tests__",
30+
"prepublishOnly": "pnpm build",
31+
"prettier:check": "prettier -c --ignore-unknown .",
32+
"prettier:fix": "prettier -w --ignore-unknown .",
33+
"eslint:check": "eslint --max-warnings=0 .",
34+
"eslint:fix": "pnpm run eslint:check --fix",
35+
"lint": "pnpm run eslint:check && pnpm run prettier:check",
36+
"lint:fix": "pnpm run eslint:fix && pnpm run prettier:fix"
37+
},
38+
"repository": {
39+
"type": "git",
40+
"url": "git+https://github.com/mystenlabs/ts-sdks.git"
41+
},
42+
"bugs": {
43+
"url": "https://github.com/mystenlabs/ts-sdks/issues"
44+
},
45+
"homepage": "https://github.com/mystenlabs/ts-sdks#readme",
46+
"devDependencies": {
47+
"@hey-api/openapi-ts": "^0.61.2",
48+
"@mysten/build-scripts": "workspace:*",
49+
"@types/node": "^22.10.5",
50+
"typescript": "^5.7.2",
51+
"vitest": "^2.1.8"
52+
},
53+
"dependencies": {
54+
"@mysten/bcs": "workspace:*",
55+
"@mysten/sui": "workspace:*",
56+
"@noble/curves": "^1.4.2",
57+
"@noble/hashes": "^1.4.0",
58+
"shamir-secret-sharing": "0.0.3"
59+
}
60+
}

packages/seal/src/bcs.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// Copyright (c) Mysten Labs, Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { fromHex, toHex } from '@mysten/bcs';
5+
import { bcs } from '@mysten/sui/bcs';
6+
7+
export const IBEEncryptions = bcs.enum('IBEEncryptions', {
8+
BonehFranklinBLS12381: bcs.struct('BonehFranklinBLS12381', {
9+
nonce: bcs.bytes(96),
10+
encryptedShares: bcs.vector(bcs.bytes(32)),
11+
encryptedRandomness: bcs.bytes(32),
12+
}),
13+
});
14+
15+
export const Ciphertext = bcs.enum('Ciphertext', {
16+
Aes256Gcm: bcs.struct('Aes256Gcm', {
17+
blob: bcs.vector(bcs.U8),
18+
aad: bcs.option(bcs.vector(bcs.U8)),
19+
}),
20+
Hmac256Ctr: bcs.struct('Hmac256Ctr', {
21+
blob: bcs.vector(bcs.U8),
22+
aad: bcs.option(bcs.vector(bcs.U8)),
23+
mac: bcs.bytes(32),
24+
}),
25+
Plain: bcs.struct('Plain', {}),
26+
});
27+
28+
/**
29+
* The encrypted object format. Should be aligned with the Rust implementation.
30+
*/
31+
export const EncryptedObject = bcs.struct('EncryptedObject', {
32+
version: bcs.U8,
33+
packageId: bcs.Address,
34+
id: bcs.vector(bcs.U8).transform({
35+
output: (val) => toHex(new Uint8Array(val)),
36+
input: (val: string) => fromHex(val),
37+
}),
38+
services: bcs.vector(bcs.tuple([bcs.Address, bcs.U8])),
39+
threshold: bcs.U8,
40+
encryptedShares: IBEEncryptions,
41+
ciphertext: Ciphertext,
42+
});
43+
44+
/**
45+
* The Move struct for the KeyServer object.
46+
*/
47+
export const KeyServerMove = bcs.struct('KeyServer', {
48+
id: bcs.Address,
49+
name: bcs.string(),
50+
url: bcs.string(),
51+
keyType: bcs.u8(),
52+
pk: bcs.vector(bcs.u8()),
53+
});

packages/seal/src/bls12381.ts

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
// Copyright (c) Mysten Labs, Inc.
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
import { toHex } from '@mysten/bcs';
5+
import type { Fp2, Fp12 } from '@noble/curves/abstract/tower';
6+
import type { ProjPointType } from '@noble/curves/abstract/weierstrass';
7+
import { bls12_381 } from '@noble/curves/bls12-381';
8+
9+
export class G1Element {
10+
point: ProjPointType<bigint>;
11+
12+
constructor(point: ProjPointType<bigint>) {
13+
this.point = point;
14+
}
15+
16+
static generator(): G1Element {
17+
return new G1Element(bls12_381.G1.ProjectivePoint.BASE);
18+
}
19+
20+
static fromBytes(bytes: Uint8Array): G1Element {
21+
return new G1Element(bls12_381.G1.ProjectivePoint.fromHex(toHex(bytes)));
22+
}
23+
24+
toBytes(): Uint8Array {
25+
return this.point.toRawBytes();
26+
}
27+
28+
multiply(scalar: Scalar): G1Element {
29+
return new G1Element(this.point.multiply(scalar.scalar));
30+
}
31+
32+
add(other: G1Element): G1Element {
33+
return new G1Element(this.point.add(other.point));
34+
}
35+
36+
subtract(other: G1Element): G1Element {
37+
return new G1Element(this.point.subtract(other.point));
38+
}
39+
40+
static hashToCurve(data: Uint8Array): G1Element {
41+
return new G1Element(
42+
bls12_381.G1.ProjectivePoint.fromAffine(bls12_381.G1.hashToCurve(data).toAffine()),
43+
);
44+
}
45+
46+
pairing(other: G2Element): GTElement {
47+
return new GTElement(bls12_381.pairing(this.point, other.point));
48+
}
49+
}
50+
51+
export class G2Element {
52+
point: ProjPointType<Fp2>;
53+
54+
constructor(point: ProjPointType<Fp2>) {
55+
this.point = point;
56+
}
57+
58+
static generator(): G2Element {
59+
return new G2Element(bls12_381.G2.ProjectivePoint.BASE);
60+
}
61+
62+
static fromBytes(bytes: Uint8Array): G2Element {
63+
return new G2Element(bls12_381.G2.ProjectivePoint.fromHex(toHex(bytes)));
64+
}
65+
66+
toBytes(): Uint8Array {
67+
return this.point.toRawBytes();
68+
}
69+
70+
multiply(scalar: Scalar): G2Element {
71+
return new G2Element(this.point.multiply(scalar.scalar));
72+
}
73+
74+
add(other: G2Element): G2Element {
75+
return new G2Element(this.point.add(other.point));
76+
}
77+
78+
hashToCurve(data: Uint8Array): G2Element {
79+
return new G2Element(
80+
bls12_381.G2.ProjectivePoint.fromAffine(bls12_381.G2.hashToCurve(data).toAffine()),
81+
);
82+
}
83+
}
84+
85+
export class GTElement {
86+
element: Fp12;
87+
88+
constructor(element: Fp12) {
89+
this.element = element;
90+
}
91+
92+
toBytes(): Uint8Array {
93+
return bls12_381.fields.Fp12.toBytes(this.element);
94+
}
95+
}
96+
97+
export class Scalar {
98+
scalar: bigint;
99+
100+
constructor(scalar: bigint) {
101+
this.scalar = scalar;
102+
}
103+
104+
static random(): Scalar {
105+
return Scalar.fromBytes(bls12_381.utils.randomPrivateKey());
106+
}
107+
108+
toBytes(): Uint8Array {
109+
return new Uint8Array(bls12_381.fields.Fr.toBytes(this.scalar));
110+
}
111+
112+
static fromBytes(bytes: Uint8Array): Scalar {
113+
return new Scalar(bls12_381.fields.Fr.fromBytes(bytes));
114+
}
115+
116+
static fromNumber(num: number): Scalar {
117+
return new Scalar(BigInt(num));
118+
}
119+
}

0 commit comments

Comments
 (0)