Skip to content

Commit 89a565b

Browse files
committed
Merge branch 'main' into ucrypto-refactor
2 parents c636448 + 54aeb97 commit 89a565b

22 files changed

Lines changed: 433 additions & 3959 deletions

bun.lock

Lines changed: 0 additions & 3818 deletions
This file was deleted.

documentation/ucrypto.changelog.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This document outlines the changes that need to be made to existing code for it
44
> [!IMPORTANT]
55
> After switching to the new PQC-enabled SDK, the ed25519 keypairs for existing mnemonics will change. Connected wallets, and identities will need to be connected to the new address.
66
7-
## 1. Demos.connectWallet
7+
## 1. Demos.connectWallet
88

99
Instead of an ed25519 private key, `Demos.connectWallet` now accepts a master seed in the following two forms:
1010

@@ -68,6 +68,11 @@ const tx: Transaction = await returnsUnsignedTx()
6868
const signedTx = await demos.sign(tx)
6969
```
7070

71+
The `demos.tx.sign` method now points to `demos.sign` instead of `DemosTransactions.sign`.
72+
73+
> [!TIP]
74+
> If you are using `DemosTransactions` methods directly, it's recommended to replace them with their `Demos` instance equivalents for simpler integration.
75+
7176
## 4. Web2 Identities
7277

7378
The structure of the web2 payloads has changed to accomodate PQC. Payloads can now have either an ed25519 signatue or a PQC key signature.

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@kynesyslabs/demosdk",
3-
"version": "2.1.15",
3+
"version": "2.2.49",
44
"main": "build/index.js",
55
"types": "build/index.d.ts",
66
"author": "Kynesys Labs",
@@ -21,7 +21,8 @@
2121
"./abstraction": "./build/abstraction/index.js",
2222
"./l2ps": "./build/l2ps/index.js",
2323
"./utils": "./build/utils/index.js",
24-
"./encryption": "./build/encryption/index.js"
24+
"./encryption": "./build/encryption/index.js",
25+
"./bridge": "./build/bridge/index.js"
2526
},
2627
"scripts": {
2728
"build": "rm -rf build && tsc --skipLibCheck && resolve-tspaths && mv build/src/* build/ && rm -rf build/src",
@@ -59,6 +60,7 @@
5960
"@simplewebauthn/server": "^11.0.0",
6061
"@solana/buffer-layout": "^4.0.1",
6162
"@solana/web3.js": "^1.98.0",
63+
"@the-convocation/twitter-scraper": "^0.16.6",
6264
"@ton/core": "^0.56.3",
6365
"@ton/crypto": "^3.2.0",
6466
"@ton/ton": "^13.11.2",
@@ -95,14 +97,13 @@
9597
"socket.io-client": "^4.7.2",
9698
"sphincs": "^3.0.4",
9799
"superdilithium": "^2.0.6",
98-
"tweetnacl": "^1.0.3",
99-
"tweetnacl-util": "^0.15.1",
100-
"twitter-api-sdk": "^1.2.1",
101100
"web3": "^4.9.0",
102101
"xrpl": "^3.1.0"
103102
},
104103
"devDependencies": {
104+
"@kynesyslabs/demosdk": "^2.2.25",
105105
"@orbs-network/ton-access": "^2.3.3",
106+
"@types/cors": "^2.8.17",
106107
"@types/jest": "^29.5.12",
107108
"@types/node": "^20.12.4",
108109
"@types/node-forge": "^1.3.11",

src/abstraction/Identities.ts

Lines changed: 41 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ import {
1616
import { DemosTransactions } from "@/websdk"
1717
import { Demos } from "@/websdk/demosclass"
1818
import { uint8ArrayToHex, UnifiedCrypto } from "@/encryption"
19+
import axios from "axios"
1920

20-
export default class Identities {
21+
export class Identities {
2122
formats = {
2223
web2: {
2324
github: [
@@ -54,13 +55,12 @@ export default class Identities {
5455
*
5556
* @returns The validity data of the identity transaction.
5657
*/
57-
async inferIdentity(
58+
private async inferIdentity(
5859
demos: Demos,
5960
context: "xm" | "web2" | "pqc",
6061
payload: any,
6162
): Promise<RPCResponseWithValidityData> {
6263
if (context === "web2") {
63-
console.log
6464
if (
6565
!this.formats.web2[payload.context].some((format: string) =>
6666
payload.proof.startsWith(format),
@@ -107,7 +107,7 @@ export default class Identities {
107107
* @param payload The payload to remove the identity from.
108108
* @returns The response from the RPC call.
109109
*/
110-
async removeIdentity(
110+
private async removeIdentity(
111111
demos: Demos,
112112
context: "xm" | "web2" | "pqc",
113113
payload: any,
@@ -200,9 +200,18 @@ export default class Identities {
200200
* @returns The response from the RPC call.
201201
*/
202202
async addGithubIdentity(demos: Demos, payload: GithubProof) {
203+
const username = payload.split("/")[3]
204+
const ghUser = await axios.get(`https://api.github.com/users/${username}`)
205+
206+
if (!ghUser.data.login) {
207+
throw new Error("Failed to get github user")
208+
}
209+
203210
let githubPayload: InferFromGithubPayload = {
204211
context: "github",
205212
proof: payload,
213+
username: ghUser.data.login,
214+
userId: ghUser.data.id,
206215
}
207216

208217
return await this.inferIdentity(demos, "web2", githubPayload)
@@ -216,9 +225,17 @@ export default class Identities {
216225
* @returns The response from the RPC call.
217226
*/
218227
async addTwitterIdentity(demos: Demos, payload: TwitterProof) {
228+
const data = await demos.web2.getTweet(payload)
229+
230+
if (!data.success) {
231+
throw new Error(data.error)
232+
}
233+
219234
let twitterPayload: InferFromXPayload = {
220235
context: "twitter",
221236
proof: payload,
237+
username: data.tweet.username,
238+
userId: data.tweet.userId,
222239
}
223240

224241
return await this.inferIdentity(demos, "web2", twitterPayload)
@@ -342,4 +359,24 @@ export default class Identities {
342359
async getWeb2Identities(demos: Demos, address?: string) {
343360
return await this.getIdentities(demos, "getWeb2Identities", address)
344361
}
362+
363+
/**
364+
* Get the points associated with an identity
365+
*
366+
* @param demos A Demos instance to communicate with the RPC
367+
* @returns The points data for the identity
368+
*/
369+
async getUserPoints(demos: Demos): Promise<RPCResponseWithValidityData> {
370+
const request = {
371+
method: "gcr_routine",
372+
params: [
373+
{
374+
method: "getPoints",
375+
params: [demos.getAddress()],
376+
},
377+
],
378+
}
379+
380+
return await demos.rpcCall(request, true)
381+
}
345382
}

src/abstraction/index.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import { EvmCoinFinder } from "./EvmCoinFinder"
99
import { CoinFinder } from "./CoinFinder"
10-
import Identities from "./Identities"
10+
import { Identities } from "./Identities"
1111
import {
1212
InferFromWritePayload,
1313
InferFromSignaturePayload,
@@ -20,7 +20,8 @@ import {
2020
IdentityPayload,
2121
InferFromSignatureTargetIdentityPayload,
2222
PqcIdentityAssignPayload,
23-
PqcIdentityRemovePayload
23+
PqcIdentityRemovePayload,
24+
UserPoints,
2425
} from "@/types/abstraction"
2526

2627
export {
@@ -38,5 +39,6 @@ export {
3839
IdentityPayload,
3940
InferFromSignatureTargetIdentityPayload,
4041
PqcIdentityAssignPayload,
41-
PqcIdentityRemovePayload
42+
PqcIdentityRemovePayload,
43+
UserPoints,
4244
}

src/bridge/index.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
1-
import { BridgeTradePayload } from "@/types/bridge/bridgeTradePayload"
2-
import {
1+
export { BridgeTradePayload } from "@/types/bridge/bridgeTradePayload"
2+
export {
33
ChainProviders,
44
SupportedChains,
5-
SupportedTokens
5+
SupportedTokens,
66
} from "@/types/bridge/constants"
7+
import RubicBridge from "./rubicBridge"
8+
export {
9+
BLOCKCHAIN_NAME,
10+
CROSS_CHAIN_TRADE_TYPE,
11+
CrossChainTrade,
12+
RubicSdkError,
13+
WrappedCrossChainTrade,
14+
} from "rubic-sdk"
15+
16+
import { methods as NativeBridgeMethods } from "./nativeBridge"
17+
export { NativeBridgeMethods }
718

19+
// Export types from nativeBridgeTypes
820
export {
9-
BridgeTradePayload,
10-
ChainProviders,
11-
SupportedChains,
12-
SupportedTokens
13-
}
21+
BridgeOperation as NativeBridgeOperation,
22+
BridgeOperationCompiled as NativeBridgeOperationCompiled,
23+
SupportedChain as NativeBridgeSupportedChain,
24+
SupportedStablecoin as NativeBridgeSupportedStablecoin,
25+
SupportedEVMChain as NativeBridgeSupportedEVMChain,
26+
supportedChains as NativeBridgeSupportedChains,
27+
supportedStablecoins as NativeBridgeSupportedStablecoins,
28+
supportedEVMChains as NativeBridgeSupportedEVMChains,
29+
usdcContracts as NativeBridgeUSDCContracts,
30+
usdcAbi as NativeBridgeUSDCAbi,
31+
} from "./nativeBridgeTypes"
32+
export { RubicBridge }

src/bridge/nativeBridge.ts

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import { Cryptography, Hashing } from "@/encryption"
2+
import {
3+
BridgeOperation,
4+
BridgeOperationCompiled,
5+
SupportedChain,
6+
SupportedEVMChain,
7+
SupportedStablecoin,
8+
supportedEVMChains,
9+
SupportedNonEVMChain,
10+
supportedNonEVMChains,
11+
} from "./nativeBridgeTypes"
12+
import { Transaction } from "@/types/blockchain/Transaction"
13+
import { RPCRequest } from "@/types"
14+
15+
export const methods = {
16+
/**
17+
* Validates the chain
18+
* @param chain
19+
* @param chainType
20+
* @param isOrigin (useful for error messages)
21+
*/
22+
validateChain (
23+
chain: string,
24+
chainType: string,
25+
isOrigin: boolean,
26+
) {
27+
const chainTypeStr = isOrigin ? "origin" : "destination"
28+
if (chainType === "EVM") {
29+
if (!supportedEVMChains.includes(chain as SupportedEVMChain)) {
30+
throw new Error(
31+
`Invalid ${chainTypeStr} chain: ${chain} is not a supported EVM`,
32+
)
33+
}
34+
} else {
35+
if (
36+
!supportedNonEVMChains.includes(
37+
chain as SupportedNonEVMChain,
38+
)
39+
) {
40+
throw new Error(
41+
`Invalid ${chainTypeStr} chain: ${chain} is not a supported chain`,
42+
)
43+
}
44+
}
45+
},
46+
/**
47+
* Generates a new operation, ready to be sent to the node as a RPCRequest
48+
* TODO Implement the params
49+
* REVIEW Should we use the identity somehow or we keep using the private key?
50+
*/
51+
generateOperation(
52+
privateKey: string,
53+
publicKey: string,
54+
originChainType: SupportedChain,
55+
originChain: SupportedEVMChain | SupportedNonEVMChain,
56+
destinationChainType: SupportedChain,
57+
destinationChain: SupportedEVMChain | SupportedNonEVMChain,
58+
originAddress: string,
59+
destinationAddress: string,
60+
amount: string,
61+
token: SupportedStablecoin,
62+
): RPCRequest {
63+
// Ensuring the chains are valid: throw an error if not
64+
this.validateChain(originChain, originChainType, true)
65+
this.validateChain(destinationChain, destinationChainType, false)
66+
// Defining the operation
67+
const operation: BridgeOperation = {
68+
demoAddress: publicKey,
69+
originChainType: originChainType,
70+
originChain: originChain,
71+
destinationChainType: destinationChainType,
72+
destinationChain: destinationChain,
73+
originAddress: originAddress,
74+
destinationAddress: destinationAddress,
75+
amount: amount,
76+
token: token,
77+
txHash: "",
78+
status: "empty",
79+
}
80+
// REVIEW Sign the operation
81+
let opHash = Hashing.sha256(JSON.stringify(operation))
82+
let signature = Cryptography.sign(opHash, privateKey)
83+
let hexSignature = new TextDecoder().decode(signature)
84+
let nodeCallPayload: RPCRequest = {
85+
method: "nativeBridge",
86+
params: [operation, hexSignature],
87+
}
88+
return nodeCallPayload
89+
},
90+
91+
/**
92+
*
93+
* @param compiled operation
94+
* @param signature
95+
* @param rpc
96+
* @returns
97+
*/
98+
generateOperationTx(compiled: BridgeOperationCompiled): Transaction {
99+
// TODO Implement the transaction once we have the compiled operation
100+
// Preparing the known values for the transaction
101+
const tx: Transaction = {
102+
content: {
103+
type: "nativeBridge",
104+
data: ["nativeBridge", compiled],
105+
from: "", // TODO Implement from using the identity
106+
to: "", // TODO Same as from
107+
amount: 0,
108+
gcr_edits: [],
109+
nonce: 0,
110+
timestamp: Date.now(),
111+
transaction_fee: { // TODO Compile with the BridgeOperationCompiled object content
112+
network_fee: 0,
113+
rpc_fee: 0,
114+
additional_fee: 0,
115+
},
116+
},
117+
signature: {
118+
type: "ed25519",
119+
data: "",
120+
},
121+
hash: "",
122+
status: "empty",
123+
blockNumber: 0,
124+
}
125+
// TODO Hash and sign the transaction
126+
return tx
127+
},
128+
}

0 commit comments

Comments
 (0)