-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathaccountAuth.ts
124 lines (110 loc) · 2.56 KB
/
accountAuth.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
import {WalletUtils} from "@onflow/fcl"
import {ConnectedAppConfig} from "hooks/useConnectedAppConfig"
import {Account} from "src/accounts"
import {sign} from "src/crypto"
import {buildServices} from "./services"
import {isBackchannel, updatePollingSession} from "./utils"
type AccountProofData = {
address: string
nonce: string | undefined
appIdentifier: string | undefined
}
const getSignature = (key: string, accountProofData: AccountProofData) => {
return sign(key, WalletUtils.encodeAccountProof(accountProofData))
}
function proveAuthn(
flowAccountPrivateKey: string,
address: string,
keyId: number,
nonce: string | undefined,
appIdentifier: string | undefined
) {
return {
addr: address,
keyId: keyId,
signature: getSignature(flowAccountPrivateKey, {
address,
nonce,
appIdentifier,
}),
}
}
export async function refreshAuthn(
baseUrl: string,
flowAccountPrivateKey: string,
address: string,
keyId: number,
scopes: Set<string>,
nonce: string | undefined,
appIdentifier: string | undefined
) {
const signature = getSignature(flowAccountPrivateKey, {
address,
nonce,
appIdentifier,
})
const compSig = new WalletUtils.CompositeSignature(address, keyId, signature)
const services = buildServices({
baseUrl,
address,
nonce,
scopes,
compSig,
keyId,
includeRefresh: false,
})
WalletUtils.approve({
f_type: "PollingResponse",
f_vsn: "1.0.0",
addr: address,
services,
})
}
export async function chooseAccount(
baseUrl: string,
flowAccountPrivateKey: string,
account: Account,
scopes: Set<string>,
connectedAppConfig: ConnectedAppConfig
) {
const {address, keyId} = account
const {nonce, appIdentifier} = connectedAppConfig.body
const {client} = connectedAppConfig.config
let compSig
if (nonce) {
const {addr, signature} = proveAuthn(
flowAccountPrivateKey,
address,
keyId!,
nonce,
appIdentifier
)
compSig = new WalletUtils.CompositeSignature(addr, keyId, signature)
}
const services = buildServices({
baseUrl,
address,
nonce,
scopes,
compSig,
keyId,
includeRefresh: false,
client,
})
localStorage.setItem("connectedAppConfig", JSON.stringify(connectedAppConfig))
const data = {
addr: address,
services,
}
const message = {
f_type: "PollingResponse",
f_vsn: "1.0.0",
status: "APPROVED",
data,
}
if (isBackchannel()) {
updatePollingSession(baseUrl, message)
} else {
WalletUtils.sendMsgToFCL("FCL:VIEW:RESPONSE", message)
}
}