-
Notifications
You must be signed in to change notification settings - Fork 36
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 #139 from nautls/remove-own-serializer
Replace own serializer with Fleet's
- Loading branch information
Showing
16 changed files
with
119 additions
and
202 deletions.
There are no files selected for viewing
This file was deleted.
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
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,12 +1,12 @@ | ||
import { hex, randomBytes } from "@fleet-sdk/crypto"; | ||
|
||
/** | ||
* Creates a EIP-28 signing message | ||
* @param message | ||
* @param origin | ||
* @returns the signing message formatted as "signingMessage;origin;timestamp;randomBytes" | ||
*/ | ||
export function buildEip28ResponseMessage(message: string, origin: string): string { | ||
const buffer = Buffer.from(new Uint8Array(32)); | ||
crypto.getRandomValues(buffer); | ||
|
||
return `${message};${origin};${Math.floor(Date.now() / 1000)};${buffer.toString("hex")}`; | ||
const rand = hex.encode(randomBytes(32)); | ||
return `${message};${origin};${Math.floor(Date.now() / 1000)};${rand}`; | ||
} |
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,65 @@ | ||
import { P2PK_TREE_PREFIX, PK_HEX_LENGTH, SIGMA_CONSTANT_PK_MATCHER } from "@/constants/ergo"; | ||
import { Registers } from "@/types/connector"; | ||
import { ErgoTree } from "ergo-lib-wasm-browser"; | ||
import { Box, isEmpty, uniq } from "@fleet-sdk/common"; | ||
import { addressFromErgoTree, addressFromPk } from "./addresses"; | ||
|
||
export function extractPksFromRegisters(registers: Registers): string[] { | ||
const pks: string[] = []; | ||
for (const register of Object.values(registers)) { | ||
const pk = extractPkFromSigmaConstant(register); | ||
if (pk) pks.push(pk); | ||
} | ||
|
||
return pks; | ||
} | ||
|
||
export function extractPksFromP2SErgoTree(ergoTree: string): string[] { | ||
const pks: string[] = []; | ||
const tree = ErgoTree.from_base16_bytes(ergoTree); | ||
const len = tree.constants_len(); | ||
for (let i = 0; i < len; i++) { | ||
const constant = tree.get_constant(i)?.encode_to_base16(); | ||
const pk = extractPkFromSigmaConstant(constant); | ||
if (pk) pks.push(pk); | ||
} | ||
|
||
return pks; | ||
} | ||
|
||
export function extractPkFromSigmaConstant(constant?: string): string | undefined { | ||
if (!constant) return; | ||
|
||
const result = SIGMA_CONSTANT_PK_MATCHER.exec(constant); | ||
if (!result) return; | ||
|
||
for (let i = 0; i < result.length; i++) { | ||
if (result[i] && result[i].length === PK_HEX_LENGTH) { | ||
return result[i]; | ||
} | ||
} | ||
} | ||
|
||
function extractAddressesFromInput(input: Box): string[] { | ||
if (input.ergoTree.startsWith(P2PK_TREE_PREFIX)) { | ||
return [addressFromErgoTree(input.ergoTree)]; | ||
} | ||
|
||
let pks = extractPksFromP2SErgoTree(input.ergoTree); | ||
if (input.additionalRegisters) { | ||
pks = pks.concat(extractPksFromRegisters(input.additionalRegisters)); | ||
} | ||
|
||
if (isEmpty(pks)) return []; | ||
|
||
const addresses: string[] = []; | ||
for (const pk of uniq(pks)) { | ||
addresses.push(addressFromPk(pk)); | ||
} | ||
|
||
return addresses; | ||
} | ||
|
||
export function extractAddressesFromInputs(inputs: Box[]) { | ||
return inputs.map((input) => extractAddressesFromInput(input)).flat(); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { isUndefined } from "@fleet-sdk/common"; | ||
import { Coder } from "@fleet-sdk/crypto"; | ||
import { parse } from "@fleet-sdk/serializer"; | ||
|
||
export function sigmaDecode<T>(value: string, coder?: Coder<unknown, T>) { | ||
const v = parse<T>(value, "safe"); | ||
if (isUndefined(v)) return; | ||
|
||
return coder ? coder.encode(v) : v; | ||
} |
This file was deleted.
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
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.