@canton-network/core-signing-internal 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/controller.d.ts +20 -0
- package/dist/controller.d.ts.map +1 -0
- package/dist/controller.js +108 -0
- package/dist/controller.test.d.ts +2 -0
- package/dist/controller.test.d.ts.map +1 -0
- package/dist/controller.test.js +56 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1 -0
- package/package.json +39 -0
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { PartyMode, SigningDriverInterface, SigningProvider } from '@canton-network/core-signing-lib';
|
|
2
|
+
import { AuthContext } from '@canton-network/core-wallet-auth';
|
|
3
|
+
export declare class InternalSigningDriver implements SigningDriverInterface {
|
|
4
|
+
private signer;
|
|
5
|
+
private signerByPublicKey;
|
|
6
|
+
private transactions;
|
|
7
|
+
partyMode: PartyMode;
|
|
8
|
+
signingProvider: SigningProvider;
|
|
9
|
+
controller: (_userId: AuthContext["userId"] | undefined) => {
|
|
10
|
+
signTransaction: import("@canton-network/core-signing-lib").SignTransaction;
|
|
11
|
+
getTransaction: import("@canton-network/core-signing-lib").GetTransaction;
|
|
12
|
+
getTransactions: import("@canton-network/core-signing-lib").GetTransactions;
|
|
13
|
+
getKeys: import("@canton-network/core-signing-lib").GetKeys;
|
|
14
|
+
createKey: import("@canton-network/core-signing-lib").CreateKey;
|
|
15
|
+
getConfiguration: import("@canton-network/core-signing-lib").GetConfiguration;
|
|
16
|
+
setConfiguration: import("@canton-network/core-signing-lib").SetConfiguration;
|
|
17
|
+
subscribeTransactions: import("@canton-network/core-signing-lib").SubscribeTransactions;
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=controller.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"controller.d.ts","sourceRoot":"","sources":["../src/controller.ts"],"names":[],"mappings":"AAEA,OAAO,EAEH,SAAS,EACT,sBAAsB,EACtB,eAAe,EAGlB,MAAM,kCAAkC,CAAA;AAoBzC,OAAO,EAAE,WAAW,EAAE,MAAM,kCAAkC,CAAA;AAyB9D,qBAAa,qBAAsB,YAAW,sBAAsB;IAChE,OAAO,CAAC,MAAM,CAAsC;IACpD,OAAO,CAAC,iBAAiB,CAAsC;IAC/D,OAAO,CAAC,YAAY,CAA8C;IAE3D,SAAS,YAAqB;IAC9B,eAAe,kBAAgC;IAE/C,UAAU,GAAI,SAAS,WAAW,CAAC,QAAQ,CAAC,GAAG,SAAS;;;;;;;;;MAgIzD;CACT"}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// Disabled unused vars rule to allow for future implementations
|
|
2
|
+
/* eslint-disable @typescript-eslint/no-unused-vars */
|
|
3
|
+
import { buildController, PartyMode, SigningProvider, signTransactionHash, createKeyPair, } from '@canton-network/core-signing-lib';
|
|
4
|
+
import { randomUUID } from 'node:crypto';
|
|
5
|
+
const convertInternalTransaction = (tx) => {
|
|
6
|
+
return {
|
|
7
|
+
txId: tx.id,
|
|
8
|
+
status: 'signed',
|
|
9
|
+
signature: tx.signature,
|
|
10
|
+
publicKey: tx.publicKey,
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
export class InternalSigningDriver {
|
|
14
|
+
signer = new Map();
|
|
15
|
+
signerByPublicKey = new Map();
|
|
16
|
+
transactions = new Map();
|
|
17
|
+
partyMode = PartyMode.EXTERNAL;
|
|
18
|
+
signingProvider = SigningProvider.WALLET_KERNEL;
|
|
19
|
+
controller = (_userId) => buildController({
|
|
20
|
+
signTransaction: async (params) => {
|
|
21
|
+
// TODO: validate transaction here
|
|
22
|
+
const key = this.signerByPublicKey.get(params.publicKey);
|
|
23
|
+
if (key) {
|
|
24
|
+
const txId = randomUUID();
|
|
25
|
+
const signature = signTransactionHash(params.txHash, key.privateKey);
|
|
26
|
+
const internalTransaction = {
|
|
27
|
+
id: txId,
|
|
28
|
+
hash: params.txHash,
|
|
29
|
+
signature,
|
|
30
|
+
publicKey: params.publicKey,
|
|
31
|
+
createdAt: new Date(),
|
|
32
|
+
};
|
|
33
|
+
this.transactions.set(txId, internalTransaction);
|
|
34
|
+
return Promise.resolve({
|
|
35
|
+
txId,
|
|
36
|
+
status: 'signed',
|
|
37
|
+
signature,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
return Promise.resolve({
|
|
42
|
+
error: 'key_not_found',
|
|
43
|
+
error_description: 'The provided public key does not exist in the signing.',
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
},
|
|
47
|
+
getTransaction: async (params) => {
|
|
48
|
+
const tx = this.transactions.get(params.txId);
|
|
49
|
+
if (tx) {
|
|
50
|
+
return Promise.resolve({
|
|
51
|
+
txId: tx.id,
|
|
52
|
+
status: 'signed',
|
|
53
|
+
signature: tx.signature,
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
else {
|
|
57
|
+
return Promise.resolve({
|
|
58
|
+
error: 'transaction_not_found',
|
|
59
|
+
error_description: 'The requested transaction does not exist.',
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
},
|
|
63
|
+
getTransactions: async (params) => {
|
|
64
|
+
if (params.publicKeys || params.txIds) {
|
|
65
|
+
const transactions = Array.from(this.transactions.values()).filter((tx) => params.txIds?.includes(tx.id) ||
|
|
66
|
+
params.publicKeys?.includes(tx.publicKey));
|
|
67
|
+
return Promise.resolve({
|
|
68
|
+
transactions: transactions.map(convertInternalTransaction),
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
else {
|
|
72
|
+
return Promise.resolve({
|
|
73
|
+
error: 'bad_arguments',
|
|
74
|
+
error_description: 'either public key or txIds must be supplied',
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
},
|
|
78
|
+
getKeys: async () => Promise.resolve({
|
|
79
|
+
keys: Array.from(this.signer.values()).map((key) => ({
|
|
80
|
+
id: key.id,
|
|
81
|
+
name: key.name,
|
|
82
|
+
publicKey: key.publicKey,
|
|
83
|
+
})),
|
|
84
|
+
}),
|
|
85
|
+
createKey: async (params) => {
|
|
86
|
+
const { publicKey, privateKey } = createKeyPair();
|
|
87
|
+
const id = randomUUID();
|
|
88
|
+
const internalKey = {
|
|
89
|
+
id,
|
|
90
|
+
name: params.name,
|
|
91
|
+
publicKey,
|
|
92
|
+
privateKey,
|
|
93
|
+
};
|
|
94
|
+
this.signer.set(id, internalKey);
|
|
95
|
+
this.signerByPublicKey.set(publicKey, internalKey);
|
|
96
|
+
return {
|
|
97
|
+
id,
|
|
98
|
+
publicKey,
|
|
99
|
+
name: params.name,
|
|
100
|
+
};
|
|
101
|
+
},
|
|
102
|
+
getConfiguration: async () => Promise.resolve({}),
|
|
103
|
+
setConfiguration: async (params) => Promise.resolve({}),
|
|
104
|
+
// TODO: implement subscribeTransactions - we will need to figure out how to handle subscriptions
|
|
105
|
+
// when the controller is not running in a server context
|
|
106
|
+
subscribeTransactions: async (params) => Promise.resolve({}),
|
|
107
|
+
});
|
|
108
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"controller.test.d.ts","sourceRoot":"","sources":["../src/controller.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { expect, test } from '@jest/globals';
|
|
2
|
+
// import request from 'supertest'
|
|
3
|
+
import { InternalSigningDriver } from './controller.js';
|
|
4
|
+
import { isRpcError, } from '@canton-network/core-signing-lib';
|
|
5
|
+
import nacl from 'tweetnacl';
|
|
6
|
+
import naclUtil from 'tweetnacl-util';
|
|
7
|
+
const TEST_KEY_NAME = 'test-key-name';
|
|
8
|
+
const TEST_TRANSACTION = 'test-tx';
|
|
9
|
+
const TEST_TRANSACTION_HASH = '88beb0783e394f6128699bad42906374ab64197d260db05bb0cfeeb518ba3ac2';
|
|
10
|
+
const authContext = {
|
|
11
|
+
userId: 'test-user-id',
|
|
12
|
+
accessToken: 'test-access-token',
|
|
13
|
+
};
|
|
14
|
+
async function setupTest(keyName = TEST_KEY_NAME) {
|
|
15
|
+
const signingDriver = new InternalSigningDriver();
|
|
16
|
+
const controller = signingDriver.controller(authContext.userId);
|
|
17
|
+
const key = await controller.createKey({ name: keyName });
|
|
18
|
+
return {
|
|
19
|
+
signingDriver,
|
|
20
|
+
key,
|
|
21
|
+
controller,
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
test('key creation', async () => {
|
|
25
|
+
const { controller, key } = await setupTest();
|
|
26
|
+
const keys = await controller.getKeys();
|
|
27
|
+
expect(keys.keys?.find((k) => k.id === key.id && k.publicKey === key.publicKey)).toBeDefined();
|
|
28
|
+
});
|
|
29
|
+
test('transaction signature', async () => {
|
|
30
|
+
const { controller, key } = await setupTest();
|
|
31
|
+
const tx = await controller.signTransaction({
|
|
32
|
+
tx: TEST_TRANSACTION,
|
|
33
|
+
txHash: TEST_TRANSACTION_HASH,
|
|
34
|
+
publicKey: key.publicKey,
|
|
35
|
+
});
|
|
36
|
+
if (isRpcError(tx)) {
|
|
37
|
+
throw new Error(`Expected a valid transaction, but got an error: ${tx.error_description}`);
|
|
38
|
+
}
|
|
39
|
+
expect(tx.status).toBe('signed');
|
|
40
|
+
expect(tx.signature).toBeDefined();
|
|
41
|
+
expect(nacl.sign.detached.verify(naclUtil.decodeBase64(TEST_TRANSACTION_HASH), naclUtil.decodeBase64(tx.signature || ''), naclUtil.decodeBase64(key.publicKey))).toBe(true);
|
|
42
|
+
const transactionsByKey = await controller.getTransactions({
|
|
43
|
+
publicKeys: [key.publicKey],
|
|
44
|
+
});
|
|
45
|
+
if (isRpcError(transactionsByKey)) {
|
|
46
|
+
throw new Error(`Expected valid transactions, but got an error: ${transactionsByKey.error_description}`);
|
|
47
|
+
}
|
|
48
|
+
expect(transactionsByKey.transactions?.find((t) => t.txId === tx.txId)).toBeDefined();
|
|
49
|
+
const transactionsById = await controller.getTransactions({
|
|
50
|
+
txIds: [tx.txId],
|
|
51
|
+
});
|
|
52
|
+
if (isRpcError(transactionsById)) {
|
|
53
|
+
throw new Error(`Expected valid transactions, but got an error: ${transactionsById.error_description}`);
|
|
54
|
+
}
|
|
55
|
+
expect(transactionsById.transactions?.find((t) => t.txId === tx.txId)).toBeDefined();
|
|
56
|
+
});
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,iBAAiB,CAAA"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './controller.js';
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"packageManager": "yarn@4.9.2",
|
|
3
|
+
"name": "@canton-network/core-signing-internal",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"types": "dist/index.d.ts",
|
|
8
|
+
"scripts": {
|
|
9
|
+
"build": "tsc -b",
|
|
10
|
+
"clean": "tsc -b --clean && rm -rf ./dist",
|
|
11
|
+
"test": "jest"
|
|
12
|
+
},
|
|
13
|
+
"dependencies": {
|
|
14
|
+
"@canton-network/core-signing-lib": "^0.1.0",
|
|
15
|
+
"@canton-network/core-wallet-auth": "^0.1.0",
|
|
16
|
+
"fs-extra": "^11.3.0",
|
|
17
|
+
"lodash": "^4.17.21"
|
|
18
|
+
},
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"@jest/globals": "^29.0.0",
|
|
21
|
+
"@swc/core": "^1.11.31",
|
|
22
|
+
"@swc/jest": "^0.2.38",
|
|
23
|
+
"@types/fs-extra": "^11",
|
|
24
|
+
"@types/jest": "^29.5.14",
|
|
25
|
+
"@types/lodash": "^4.17.17",
|
|
26
|
+
"@types/node": "^22.15.29",
|
|
27
|
+
"jest": "^29.7.0",
|
|
28
|
+
"ts-jest-resolver": "^2.0.1",
|
|
29
|
+
"tweetnacl": "^1.0.3",
|
|
30
|
+
"tweetnacl-util": "^0.15.1",
|
|
31
|
+
"typescript": "^5.8.3"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"dist/*"
|
|
35
|
+
],
|
|
36
|
+
"publishConfig": {
|
|
37
|
+
"access": "public"
|
|
38
|
+
}
|
|
39
|
+
}
|