-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
166 additions
and
4 deletions.
There are no files selected for viewing
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
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
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,83 @@ | ||
import { Alert, Button, FileInput } from "flowbite-react"; | ||
import { useState } from "react"; | ||
import { importMultisigAddresses } from "./lib/multisig-address.js"; | ||
|
||
const ERROR_MESSAGE = "Opps, error occurs when processing the uploaded file"; | ||
|
||
export default function NewAddressPage({ addAddress, navigate }) { | ||
const [state, setState] = useState({ | ||
isProcessing: false, | ||
error: null, | ||
}); | ||
|
||
const submit = (e) => { | ||
e.preventDefault(); | ||
setState({ isProcessing: true, error: null }); | ||
|
||
try { | ||
const reader = new FileReader(); | ||
|
||
reader.addEventListener("load", (loaded) => { | ||
try { | ||
const addresses = importMultisigAddresses( | ||
JSON.parse(loaded.target.result), | ||
); | ||
addAddress(addresses); | ||
navigate("#/"); | ||
} catch (error) { | ||
setState({ | ||
isProcessing: false, | ||
error: `${ERROR_MESSAGE}: ${error}`, | ||
}); | ||
} | ||
}); | ||
reader.addEventListener("error", () => { | ||
setState({ isProcessing: false, error: ERROR_MESSAGE }); | ||
}); | ||
reader.addEventListener("abort", () => { | ||
setState({ isProcessing: false, error: "Uploading aborted" }); | ||
}); | ||
|
||
const fileInput = document.getElementById("file-upload"); | ||
reader.readAsText(fileInput.files[0]); | ||
} catch (error) { | ||
setState({ | ||
isProcessing: false, | ||
error: `${ERROR_MESSAGE}: ${error}`, | ||
}); | ||
} | ||
}; | ||
|
||
return ( | ||
<form onSubmit={submit} className="flex flex-col gap-4"> | ||
<h2 className="text-lg mb-4">Import Multisig Addresses</h2> | ||
{state.error ? ( | ||
<Alert className="mb-4" color="failure"> | ||
{state.error} | ||
</Alert> | ||
) : null} | ||
|
||
<div> | ||
<p className="mb-0">Supported formats:</p> | ||
<ul className="mb-4 list-disc list-inside"> | ||
<li> | ||
The JSON file generated by <code>ckb-cli tx init</code> | ||
</li> | ||
<li> | ||
The JSON file exported from Neuron via the Multisig Address Tool in | ||
the Tools menu. | ||
</li> | ||
</ul> | ||
<FileInput id="file-upload" name="file-upload" required /> | ||
</div> | ||
|
||
<Button | ||
isProcessing={state.isProcessing} | ||
disabled={state.isProcessing} | ||
type="submit" | ||
> | ||
Upload | ||
</Button> | ||
</form> | ||
); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { MultisigConfig } from "../schemas.js"; | ||
import { | ||
decodeDeprecatedSecp256k1Address, | ||
encodeCkbAddress, | ||
} from "./ckb-address.js"; | ||
|
||
export function convertDeprecatedSecp256k1Address(addresses) { | ||
return addresses.map((address) => { | ||
const decoded = decodeDeprecatedSecp256k1Address(address); | ||
return decoded === undefined | ||
? address | ||
: encodeCkbAddress(decoded, address.slice(0, 3)); | ||
}); | ||
} | ||
|
||
export function importMultisigAddresses(jsonContent) { | ||
const found = []; | ||
|
||
if ("multisig_configs" in jsonContent) { | ||
for (const [args, value] of Object.entries(jsonContent.multisig_configs)) { | ||
found.push( | ||
MultisigConfig.parse({ | ||
args, | ||
signers: convertDeprecatedSecp256k1Address(value.sighash_addresses), | ||
required: value.require_first_n, | ||
threshold: value.threshold, | ||
}), | ||
); | ||
} | ||
} | ||
|
||
if (found.length === 0) { | ||
throw new Error("No multisg configs found in the file"); | ||
} | ||
return found; | ||
} |
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