-
Notifications
You must be signed in to change notification settings - Fork 1
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 #9 from magiclabs/guard-param-fix
from @amilano: 1) fix error in definition of receiving account guard.…
- Loading branch information
Showing
7 changed files
with
129 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { ChainId, Pact } from "@kadena/client"; | ||
import { NETWORK_ID } from "../utils/constants"; | ||
interface KeysetRefGuardTransaction { | ||
chainId: ChainId; | ||
keysetRefGuardName: string; | ||
} | ||
export const buildKeysetRefGuardTransaction = ({ | ||
chainId, | ||
keysetRefGuardName, | ||
}: KeysetRefGuardTransaction) => { | ||
return Pact.builder | ||
.execution(`(keyset-ref-guard "${keysetRefGuardName}")`) | ||
.setMeta({ chainId }) | ||
.setNetworkId(NETWORK_ID) | ||
.createTransaction(); | ||
}; |
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,15 @@ | ||
import { literal, readKeyset } from "@kadena/client"; | ||
import { checkPrincipalAccount } from "./check-principal-account"; | ||
import { accountProtocol } from "./account-protocol"; | ||
|
||
export const accountGuard = (accountName: string) => { | ||
if (checkPrincipalAccount(accountName)) { | ||
if (accountProtocol(accountName) === "r:") { | ||
return literal(`(keyset-ref-guard "${accountName.substring(2)}")`); | ||
} else { | ||
return readKeyset("receiverKeyset"); | ||
} | ||
} else { | ||
return readKeyset("receiverKeyset"); | ||
} | ||
}; |
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,8 @@ | ||
import { checkPrincipalAccount } from "./check-principal-account"; | ||
|
||
export const accountProtocol = (accountName: string) => { | ||
if (checkPrincipalAccount(accountName)) { | ||
return accountName.substring(0, 2); | ||
} | ||
return null; | ||
}; |
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,33 @@ | ||
import { ChainId } from "@kadena/types"; | ||
import { getKadenaClient } from "./client"; | ||
import { buildKeysetRefGuardTransaction } from "../pact/keyset-ref-guard"; | ||
|
||
export const checkKeysetRefExists = async ( | ||
keysetRefGuardName: string, | ||
chainId: ChainId | ||
) => { | ||
const kadenaClient = getKadenaClient(chainId); | ||
try { | ||
const transaction = buildKeysetRefGuardTransaction({ | ||
chainId, | ||
keysetRefGuardName, | ||
}); | ||
const response = await kadenaClient.dirtyRead(transaction); | ||
if (response.result.status === "failure") { | ||
console.error( | ||
"not able to check", | ||
(response.result.error as any).message | ||
); | ||
return false; | ||
} else { | ||
console.log(response.result.data); | ||
return true; | ||
} | ||
} catch (error) { | ||
console.error( | ||
`Failed to send transaction to check the keyset defined as "${keysetRefGuardName}" on chain ${chainId}` | ||
); | ||
console.error(error); | ||
return false; | ||
} | ||
}; |
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,7 @@ | ||
export const checkPrincipalAccount = (accountName: string): boolean => { | ||
return ( | ||
accountName.startsWith("k:") || | ||
accountName.startsWith("r:") || | ||
accountName.startsWith("w:") | ||
); | ||
}; |