Skip to content

Commit

Permalink
feat(icp): adapt to new signing method
Browse files Browse the repository at this point in the history
  • Loading branch information
imsk17 committed Aug 23, 2024
1 parent 44d5d73 commit ab4e241
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 47 deletions.
22 changes: 17 additions & 5 deletions src/contractsTypes/icp/bridge/bridge.did
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
type XPBridge =
service {
acceptCycles: () -> ();
add_validator: (record {
text;
principal;
}, vec SignerAndSignature) -> ();
add_validator: (AddValidator, vec SignerAndSignature) -> ();
availableCycles: () -> (nat) query;
blacklist_validator: (BlacklistValidator, vec SignerAndSignature) -> ();
claim_nft: (ClaimData, vec SignerAndSignature) -> (text);
claim_validator_rewards: (text, vec SignerAndSignature) -> ();
claim_validator_rewards: (text) -> (nat64, nat64);
encode_add_validator: (AddValidator) -> (blob) query;
encode_blacklist_validator: (BlacklistValidator) -> (blob) query;
encode_claim_data: (ClaimData) -> (blob) query;
get_blacklisted_validators: (text) -> (opt bool) query;
get_claimed_data: (text) -> (opt ClaimedEvent) query;
get_hash_from_nonce: (nat) -> (opt text) query;
get_locked_data: (text) -> (opt LockedEvent) query;
Expand Down Expand Up @@ -63,6 +65,16 @@ type ClaimData =
token_id: nat;
transaction_hash: text;
};
type BlacklistValidator =
record {
"principal": principal;
public_key: text;
};
type AddValidator =
record {
"principal": principal;
public_key: text;
};
service : (record {
chain_type: text;
collection_deployer: principal;
Expand Down
21 changes: 17 additions & 4 deletions src/contractsTypes/icp/bridge/bridge.did.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import type { Principal } from '@dfinity/principal';
import type { ActorMethod } from '@dfinity/agent';
import type { IDL } from '@dfinity/candid';

export interface AddValidator { 'principal' : Principal, 'public_key' : string }
export interface BlacklistValidator {
'principal' : Principal,
'public_key' : string,
}
export interface ClaimData {
'fee' : bigint,
'source_chain' : string,
Expand Down Expand Up @@ -40,15 +45,23 @@ export interface Validator { 'address' : Principal, 'pending_rewards' : bigint }
export interface XPBridge {
'acceptCycles' : ActorMethod<[], undefined>,
'add_validator' : ActorMethod<
[[string, Principal], Array<SignerAndSignature>],
[AddValidator, Array<SignerAndSignature>],
undefined
>,
'availableCycles' : ActorMethod<[], bigint>,
'claim_nft' : ActorMethod<[ClaimData, Array<SignerAndSignature>], string>,
'claim_validator_rewards' : ActorMethod<
[string, Array<SignerAndSignature>],
'blacklist_validator' : ActorMethod<
[BlacklistValidator, Array<SignerAndSignature>],
undefined
>,
'claim_nft' : ActorMethod<[ClaimData, Array<SignerAndSignature>], string>,
'claim_validator_rewards' : ActorMethod<[string], [bigint, bigint]>,
'encode_add_validator' : ActorMethod<[AddValidator], Uint8Array | number[]>,
'encode_blacklist_validator' : ActorMethod<
[BlacklistValidator],
Uint8Array | number[]
>,
'encode_claim_data' : ActorMethod<[ClaimData], Uint8Array | number[]>,
'get_blacklisted_validators' : ActorMethod<[string], [] | [boolean]>,
'get_claimed_data' : ActorMethod<[string], [] | [ClaimedEvent]>,
'get_hash_from_nonce' : ActorMethod<[bigint], [] | [string]>,
'get_locked_data' : ActorMethod<[string], [] | [LockedEvent]>,
Expand Down
35 changes: 32 additions & 3 deletions src/contractsTypes/icp/bridge/bridge.did.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
export const idlFactory = ({ IDL }) => {
const AddValidator = IDL.Record({
'principal' : IDL.Principal,
'public_key' : IDL.Text,
});
const SignerAndSignature = IDL.Record({
'signature' : IDL.Text,
'signer' : IDL.Text,
});
const BlacklistValidator = IDL.Record({
'principal' : IDL.Principal,
'public_key' : IDL.Text,
});
const ClaimData = IDL.Record({
'fee' : IDL.Nat64,
'source_chain' : IDL.Text,
Expand Down Expand Up @@ -43,21 +51,42 @@ export const idlFactory = ({ IDL }) => {
const XPBridge = IDL.Service({
'acceptCycles' : IDL.Func([], [], []),
'add_validator' : IDL.Func(
[IDL.Tuple(IDL.Text, IDL.Principal), IDL.Vec(SignerAndSignature)],
[AddValidator, IDL.Vec(SignerAndSignature)],
[],
[],
),
'availableCycles' : IDL.Func([], [IDL.Nat], ['query']),
'blacklist_validator' : IDL.Func(
[BlacklistValidator, IDL.Vec(SignerAndSignature)],
[],
[],
),
'claim_nft' : IDL.Func(
[ClaimData, IDL.Vec(SignerAndSignature)],
[IDL.Text],
[],
),
'claim_validator_rewards' : IDL.Func(
[IDL.Text, IDL.Vec(SignerAndSignature)],
[],
[IDL.Text],
[IDL.Nat64, IDL.Nat64],
[],
),
'encode_add_validator' : IDL.Func(
[AddValidator],
[IDL.Vec(IDL.Nat8)],
['query'],
),
'encode_blacklist_validator' : IDL.Func(
[BlacklistValidator],
[IDL.Vec(IDL.Nat8)],
['query'],
),
'encode_claim_data' : IDL.Func([ClaimData], [IDL.Vec(IDL.Nat8)], ['query']),
'get_blacklisted_validators' : IDL.Func(
[IDL.Text],
[IDL.Opt(IDL.Bool)],
['query'],
),
'get_claimed_data' : IDL.Func(
[IDL.Text],
[IDL.Opt(ClaimedEvent)],
Expand Down
8 changes: 5 additions & 3 deletions src/handler/icp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ export function icpHandler({
canisterId: Principal.fromText("ryjl3-tyaaa-aaaaa-aaaba-cai"),
});
return {
publicKey: Buffer.from(identity.getPublicKey().toRaw()).toString("hex"),
publicKey: `${identity.getPrincipal()},${Buffer.from(
identity.getPublicKey().toRaw(),
).toString("hex")}`,
pollForLockEvents: async (builder, cb) => {
serverLinkHandler
? pollForLockEvents(
Expand All @@ -54,13 +56,13 @@ export function icpHandler({
"Unreachable. Wont be called if serverLinkHandler is not present.",
);
},
signData: (buf) => signData(buf, identity),
signData: (buf) => signData(buf, identity, bc),
chainType,
initialFunds: initialFunds,
chainIdent,
currency: "ICP",
address: identity.getPrincipal().toString(),
signClaimData: (data) => signClaimData(data, identity),
signClaimData: (data) => signClaimData(data, identity, bc),
selfIsValidator: () => selfIsValidator(bc, identity),
listenForLockEvents: (cb, iter) =>
listenForLockEvents(cb, iter, lastBlock_, bc, em, logger),
Expand Down
5 changes: 4 additions & 1 deletion src/handler/icp/utils/addSelfAsValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ export default async function addSelfAsValidator(
);

await bridge.add_validator(
[publicKey, identity.getPrincipal()],
{
principal: identity.getPrincipal(),
public_key: newV,
},
signatures.map((e) => {
return {
signature: e.signature,
Expand Down
48 changes: 19 additions & 29 deletions src/handler/icp/utils/signClaimData.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,42 @@
import { IDL } from "@dfinity/candid";
import type { ActorSubclass } from "@dfinity/agent";
import type { Ed25519KeyIdentity } from "@dfinity/identity";
import { Principal } from "@dfinity/principal";
import * as ed from "@noble/ed25519";
import type { _SERVICE } from "../../../contractsTypes/icp/bridge/bridge.did";
import type { TNftTransferDetailsObject } from "../../types";

export default async function signClaimData(
data: TNftTransferDetailsObject,
identity: Ed25519KeyIdentity,
bc: ActorSubclass<_SERVICE>,
) {
const encoded = ClaimData.encodeValue({
const encoded = await bc.encode_claim_data({
destination_chain: data.destinationChain,
destination_user_address: Principal.fromText(data.destinationUserAddress),
fee: BigInt(data.fee),
source_chain: data.sourceChain,
lock_tx_chain: data.lockTxChain,
transaction_hash: data.transactionHash,
token_amount: BigInt(data.tokenAmount),
destination_chain: data.destinationChain,
token_id: data.tokenId,
source_nft_contract_address: data.sourceNftContractAddress,
metadata: data.metadata,
name: data.name,
nft_type: data.nftType,
royalty: BigInt(data.royalty),
royalty_receiver: Principal.fromText(data.royaltyReceiver),
destination_user_address: Principal.fromText(data.destinationUserAddress),
source_chain: data.sourceChain,
source_nft_contract_address: data.sourceNftContractAddress,
symbol: data.symbol,
token_amount: BigInt(data.tokenAmount),
token_id: BigInt(data.tokenId),
transaction_hash: data.transactionHash,
});

const signature = await identity.sign(encoded);
const signature = await ed.sign(
Buffer.from(encoded),
Buffer.from(identity.getKeyPair().secretKey),
);

return {
signer: Buffer.from(identity.getPublicKey().toRaw()).toString("hex"),
signer: Buffer.from(
await ed.getPublicKey(Buffer.from(identity.getKeyPair().secretKey)),
).toString("hex"),
signature: `0x${Buffer.from(signature).toString("hex")}`,
};
}

const ClaimData = IDL.Record({
fee: IDL.Nat64,
source_chain: IDL.Text,
lock_tx_chain: IDL.Text,
transaction_hash: IDL.Text,
token_amount: IDL.Nat,
destination_chain: IDL.Text,
token_id: IDL.Nat,
source_nft_contract_address: IDL.Text,
metadata: IDL.Text,
name: IDL.Text,
nft_type: IDL.Text,
royalty: IDL.Nat,
royalty_receiver: IDL.Principal,
destination_user_address: IDL.Principal,
symbol: IDL.Text,
});
19 changes: 17 additions & 2 deletions src/handler/icp/utils/signData.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import type { ActorSubclass } from "@dfinity/agent";
import type { Ed25519KeyIdentity } from "@dfinity/identity";
import { Principal } from "@dfinity/principal";
import * as ed from "@noble/ed25519";
import type { _SERVICE } from "../../../contractsTypes/icp/bridge/bridge.did";

export default async function signData(
buf: string,
identity: Ed25519KeyIdentity,
bc: ActorSubclass<_SERVICE>,
) {
const signtureBytes = await identity.sign(Buffer.from(buf, "hex"));
const [principal, pubk] = buf.split(",");
const body = await bc.encode_add_validator({
principal: Principal.fromText(principal),
public_key: pubk,
});
const signtureBytes = await ed.sign(
Buffer.from(body),
Buffer.from(identity.getKeyPair().secretKey),
);
const signature = Buffer.from(signtureBytes).toString("hex");
return {
signature,
signer: Buffer.from(identity.getPublicKey().toRaw()).toString("hex"),
signer: Buffer.from(
await ed.getPublicKey(Buffer.from(identity.getPublicKey().toRaw())),
).toString("hex"),
};
}

0 comments on commit ab4e241

Please sign in to comment.