-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharmory.sdk.ts
108 lines (95 loc) · 2.69 KB
/
armory.sdk.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import {
AuthAdminClient,
AuthConfig,
DataStoreConfig,
VaultAdminClient,
VaultConfig,
createHttpDataStore
} from '@narval-xyz/armory-sdk'
import { format } from 'date-fns'
import { v4 } from 'uuid'
import { generatePrivateKey } from 'viem/accounts'
import { Hex } from '../../packages/policy-engine-shared/src'
import { SigningAlg, buildSignerForAlg, getPublicKey, privateKeyToJwk } from '../../packages/signature/src'
const getAuthHost = () => 'http://localhost:3005'
const getAuthAdminApiKey = () => 'armory-admin-api-key'
const getVaultHost = () => 'http://localhost:3011'
const getVaultAdminApiKey = () => 'vault-admin-api-key'
const createClient = async () => {
const DATA_STORE_PRIVATE_KEY = privateKeyToJwk(generatePrivateKey())
const clientId = v4()
const authAdminClient = new AuthAdminClient({
host: getAuthHost(),
adminApiKey: getAuthAdminApiKey()
})
const vaultAdminClient = new VaultAdminClient({
host: getVaultHost(),
adminApiKey: getVaultAdminApiKey()
})
const publicKey = getPublicKey(DATA_STORE_PRIVATE_KEY)
const authClient = await authAdminClient.createClient({
id: clientId,
name: `Armory SDK E2E test ${format(new Date(), 'dd/MM/yyyy HH:mm:ss')}`,
dataStore: createHttpDataStore({
host: getAuthHost(),
clientId,
keys: [publicKey]
}),
useManagedDataStore: true
})
await vaultAdminClient.createClient({
clientId: authClient.id,
engineJwk: authClient.policyEngine.nodes[0].publicKey
})
return {
clientId,
DATA_STORE_PRIVATE_KEY
}
}
export const getArmoryConfig = async (ROOT_USER_CRED: Hex) => {
const authHost = getAuthHost()
const vaultHost = getVaultHost()
const { clientId, DATA_STORE_PRIVATE_KEY } = await createClient()
const auth: AuthConfig = {
host: authHost,
clientId,
signer: {
jwk: privateKeyToJwk(ROOT_USER_CRED),
alg: SigningAlg.ES256K,
sign: await buildSignerForAlg(privateKeyToJwk(ROOT_USER_CRED))
}
}
const vault: VaultConfig = {
host: vaultHost,
clientId,
signer: {
jwk: privateKeyToJwk(ROOT_USER_CRED),
alg: SigningAlg.ES256K,
sign: await buildSignerForAlg(privateKeyToJwk(ROOT_USER_CRED))
}
}
const entityStore: DataStoreConfig = {
host: authHost,
clientId,
signer: {
jwk: DATA_STORE_PRIVATE_KEY,
alg: SigningAlg.ES256K,
sign: await buildSignerForAlg(DATA_STORE_PRIVATE_KEY)
}
}
const policyStore: DataStoreConfig = {
host: authHost,
clientId,
signer: {
jwk: DATA_STORE_PRIVATE_KEY,
alg: SigningAlg.ES256K,
sign: await buildSignerForAlg(DATA_STORE_PRIVATE_KEY)
}
}
return {
auth,
vault,
entityStore,
policyStore
}
}