Repository navigation
Expand file tree
/
Copy pathplayground.ts
More file actions
150 lines (129 loc) · 5.74 KB
/
Copy pathplayground.ts
File metadata and controls
150 lines (129 loc) · 5.74 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import { Client } from 'xrpl'
import { encode, XrplDefinitions } from 'ripple-binary-codec'
// import { verify } from 'ripple-keypairs'
import cbor from 'cbor'
import { p256 } from '@noble/curves/p256'
import { sha256 } from '@noble/hashes/sha256'
// const result = verify(
// '',
// '304402203E4F7A18DACD45F8FDCC77ED3C2AE46B7F78DC4C6DA2BC9999C51124ECE24F6B022003E84D608C5C5A8388C382A9B811C87B44C2F3B9A0B1186E880BC4CCCA648EFB',
// 'A5010203262001215820A106DEC8E5477C96CCB93A3059EC1619F55052EF9AAC63BC3D258BF3F15235E7225820A76664CECDF1C59EDE8579F6EB39FC12915CEBB3873B51B2A10680613417109F'
// )
// console.log(result)
// const coseStruct = cbor.decodeFirstSync(
// Buffer.from(
// 'A5010203262001215820A106DEC8E5477C96CCB93A3059EC1619F55052EF9AAC63BC3D258BF3F15235E7225820A76664CECDF1C59EDE8579F6EB39FC12915CEBB3873B51B2A10680613417109F',
// 'hex'
// )
// )
// const alg = coseStruct.get(3) // 3 is the COSE key for 'alg'
// console.log(alg)
function base64urlToBuffer(hex: string): string {
let b64 = Buffer.from(hex, 'hex').toString('base64')
b64 = b64.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '')
return b64
}
export function main() {
const hex = '5135BB143171C4BC89F83195D697DF9FB345D977'
console.log(base64urlToBuffer(hex))
// UTW7FDFxxLyJ+DGV1pffn7NF2Xc=
// UTW7FDFxxLyJ-DGV1pffn7NF2Xc
}
export async function main1() {
const client = new Client('ws://localhost:6006')
await client.connect()
const liveDefinitions = await client.request({
command: 'server_definitions',
})
const _liveDefinitions = JSON.parse(JSON.stringify(liveDefinitions.result))
const definitions = new XrplDefinitions(_liveDefinitions)
const transaction = {
TransactionType: 'Payment',
Account: 'rfQuwvSNDQXwZQGMtrLVDxUyYG7MFdZKou',
Fee: '12',
Destination: 'rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK',
Amount: '1000000',
Flags: 0,
NetworkID: 21339,
Sequence: 8,
LastLedgerSequence: 29,
PasskeySignature: {
PasskeyID: '523E557C554DD4454A8BC4413194E3B178519919',
AuthenticatorData:
'49960DE5880E8C687434170F6476605B8FE4AEB9A28632C7995CF3BA831D97631D00000000',
ClientDataJSON:
'7B2274797065223A22776562617574686E2E676574222C226368616C6C656E6765223A224D5559774D54497852444D3452454D7A4F444244516A55324D6A6B354F445578525445324D5455324D4459304E6B46434E305A424D555A44526B4535517A55334F554E464D446B784D6A64444E444A444E3059795177222C226F726967696E223A22687474703A2F2F6C6F63616C686F73743A33303030222C2263726F73734F726967696E223A66616C73657D',
Signature:
'304402203E4F7A18DACD45F8FDCC77ED3C2AE46B7F78DC4C6DA2BC9999C51124ECE24F6B022003E84D608C5C5A8388C382A9B811C87B44C2F3B9A0B1186E880BC4CCCA648EFB',
// Algorithm: -7,
},
SigningPubKey:
'A5010203262001215820A106DEC8E5477C96CCB93A3059EC1619F55052EF9AAC63BC3D258BF3F15235E7225820A76664CECDF1C59EDE8579F6EB39FC12915CEBB3873B51B2A10680613417109F',
}
const txBlob = encode(transaction, definitions)
const submit = await client.request({
command: 'submit',
tx_blob: txBlob,
})
console.log(submit)
if (client.connection.getUrl() === 'ws://localhost:6006') {
await client.request({
// @ts-ignore -- ignore
command: 'ledger_accept',
})
}
}
export async function main2() {
const authenticatorDataHex =
'49960DE5880E8C687434170F6476605B8FE4AEB9A28632C7995CF3BA831D97631D00000000'
const clientDataJSONHex =
'7B2274797065223A22776562617574686E2E676574222C226368616C6C656E6765223A224877456830343344674D74574B5A685234574657426B6172663648382D7078586E4F43524A385173667977222C226F726967696E223A22687474703A2F2F6C6F63616C686F73743A33303030222C2263726F73734F726967696E223A66616C73652C226F746865725F6B6579735F63616E5F62655F61646465645F68657265223A22646F206E6F7420636F6D7061726520636C69656E74446174614A534F4E20616761696E737420612074656D706C6174652E205365652068747470733A2F2F676F6F2E676C2F796162506578227D'
const pubkeyHex =
'A5010203262001215820A106DEC8E5477C96CCB93A3059EC1619F55052EF9AAC63BC3D258BF3F15235E7225820A76664CECDF1C59EDE8579F6EB39FC12915CEBB3873B51B2A10680613417109F'
const signatureHex =
'3046022100BDCB647A630BCDF6BB6BF8C5A14E188F6432D986CACE92AF05B464DCC3DA8C15022100A2B94D6B860101D721C4B28E1BF55BD0AA1CC22D2AF915B004C427EB48B660A2'
const coseBuf = Buffer.from(pubkeyHex, 'hex')
// Decode COSE_Key
const coseStruct = cbor.decodeFirstSync(coseBuf)
// Get x and y
const x = coseStruct.get(-2) // This should be x
const y = coseStruct.get(-3) // This should be y
// Compose uncompressed public key
// const uncompressed = Buffer.concat([Buffer.from([0x04]), x, y]) // 65 bytes
const yBigInt = BigInt('0x' + y.toString('hex'))
const isYEven = yBigInt % 2n === 0n
const compressedPrefix = isYEven ? 0x02 : 0x03
const compressed = Buffer.concat([Buffer.from([compressedPrefix]), x])
// const point = p256.ProjectivePoint.fromHex(uncompressed)
console.log('Compressed Public Key:', compressed.toString('hex').length)
const authenticatorData = Buffer.from(authenticatorDataHex, 'hex')
const clientDataJSON = Buffer.from(clientDataJSONHex, 'hex')
// Step 1: Hash clientDataJSON
const clientDataHash = sha256(clientDataJSON)
// Step 2: Concatenate authenticatorData and clientDataHash
const message = Buffer.concat([
authenticatorData,
Buffer.from(clientDataHash),
])
// const rawSignature = p256.Signature.fromDER(signatureHex)
// const isValid = p256.verify(
// rawSignature.toCompactRawBytes(),
// message,
// uncompressed,
// {
// prehash: true,
// }
// )
const signature = Buffer.from(signatureHex, 'hex')
const isValid = p256.verify(signature, message, compressed, {
prehash: true,
})
if (isValid) {
console.log('Signature is valid')
} else {
console.log('Signature is NOT valid')
}
}
// main()
// main1()
main2()