-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #219 from radixdlt/update-rcfm-flow
Update-rcfm-flow
- Loading branch information
Showing
16 changed files
with
441 additions
and
523 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
packages/dapp-toolkit/src/modules/wallet-request/crypto/blake2b.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { describe, expect, it } from 'vitest' | ||
import { blake2b } from './blake2b' | ||
|
||
describe('blake2b', () => { | ||
it('should hash a string', async () => { | ||
const result = blake2b(Buffer.from('test')) | ||
|
||
if (result.isErr()) throw result.error | ||
|
||
expect(result.value.toString('hex')).toBe( | ||
'928b20366943e2afd11ebc0eae2e53a93bf177a4fcf35bcc64d503704e65e202', | ||
) | ||
}) | ||
|
||
it('should generate an array of blake2b hashes', async () => { | ||
const vectors = new Array(100).fill(null).map(() => { | ||
const buffer = Buffer.from(crypto.getRandomValues(new Uint8Array(64))) | ||
const blake2bHash = blake2b(buffer)._unsafeUnwrap().toString('hex') | ||
const message = buffer.toString('hex') | ||
return { message, blake2bHash } | ||
}) | ||
}) | ||
}) |
26 changes: 26 additions & 0 deletions
26
packages/dapp-toolkit/src/modules/wallet-request/crypto/blake2b.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { Result } from 'neverthrow' | ||
import { err, ok } from 'neverthrow' | ||
import blake from 'blakejs' | ||
import { Buffer } from 'buffer' | ||
|
||
const bufferToArrayBuffer = (buffer: Buffer): ArrayBuffer => { | ||
const arrayBuffer = new ArrayBuffer(buffer.length) | ||
const view = new Uint8Array(arrayBuffer) | ||
for (let i = 0; i < buffer.length; ++i) { | ||
view[i] = buffer[i] | ||
} | ||
return arrayBuffer | ||
} | ||
|
||
const bufferToUnit8Array = (buffer: Buffer): Uint8Array => | ||
new Uint8Array(bufferToArrayBuffer(buffer)) | ||
|
||
export const blake2b = (input: Buffer): Result<Buffer, Error> => { | ||
try { | ||
return ok(blake.blake2bHex(bufferToUnit8Array(input), undefined, 32)).map( | ||
(hex) => Buffer.from(hex, 'hex'), | ||
) | ||
} catch (error) { | ||
return err(error as Error) | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
packages/dapp-toolkit/src/modules/wallet-request/crypto/create-signature-message.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { Buffer } from 'buffer' | ||
import type { Result } from 'neverthrow' | ||
import { blake2b } from './blake2b' | ||
import { Logger } from '../../../helpers' | ||
|
||
export const createSignatureMessage = ({ | ||
interactionId, | ||
dAppDefinitionAddress, | ||
origin, | ||
logger, | ||
}: { | ||
logger?: Logger | ||
interactionId: string | ||
dAppDefinitionAddress: string | ||
origin: string | ||
}): Result<string, { reason: string; jsError: Error }> => { | ||
const prefix = 'C' | ||
const prefixBuffer = Buffer.from('C', 'ascii') | ||
const lengthOfDappDefAddress = dAppDefinitionAddress.length | ||
const lengthOfDappDefAddressBuffer = Buffer.from( | ||
lengthOfDappDefAddress.toString(16), | ||
'hex', | ||
) | ||
const dappDefAddressBuffer = Buffer.from(dAppDefinitionAddress, 'utf-8') | ||
const originBuffer = Buffer.from(origin, 'utf-8') | ||
const interactionIdBuffer = Buffer.from(interactionId, 'utf-8') | ||
|
||
const messageBuffer = Buffer.concat([ | ||
prefixBuffer, | ||
interactionIdBuffer, | ||
lengthOfDappDefAddressBuffer, | ||
dappDefAddressBuffer, | ||
originBuffer, | ||
]) | ||
|
||
const blake2bHash = blake2b(messageBuffer) | ||
.map((hash) => { | ||
logger?.debug({ | ||
method: 'createSignatureMessage', | ||
messagePartsRaw: [ | ||
prefix, | ||
interactionId, | ||
lengthOfDappDefAddress, | ||
dAppDefinitionAddress, | ||
origin, | ||
], | ||
messageParts: [ | ||
prefixBuffer.toString('hex'), | ||
interactionIdBuffer.toString('hex'), | ||
lengthOfDappDefAddressBuffer.toString('hex'), | ||
dappDefAddressBuffer.toString('hex'), | ||
originBuffer.toString('hex'), | ||
], | ||
message: messageBuffer.toString('hex'), | ||
blake2bHash: hash.toString('hex'), | ||
}) | ||
return Buffer.from(hash).toString('hex') | ||
}) | ||
.mapErr((jsError) => ({ reason: 'couldNotHashMessage', jsError })) | ||
|
||
return blake2bHash | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 1 addition & 0 deletions
1
packages/dapp-toolkit/src/modules/wallet-request/crypto/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './curve25519' | ||
export * from './blake2b' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.