I am thinking of easy account security Developers - wavesplatform

If there is "FreezeToken" in the wallet, freeze the wallet.
And if there is "ThawToken" in the wallet, it cancels the freeze.
This code is an example of failure.
let FreezeToken = base58'GwmXnsF3Z5tANgHwmDW7Lf4SwyYNEp4S3QZM3aBFLHwS'
let ThawToken = base58'GK7ZV8xFbh1Qz14Cnr6mLkV93svaZGrurVuaBZocwLZZ'
match tx {
case m : ExchangeTransaction. =>
if (assetBalance(e.sender,ThawToken) >= 1 ) then{true}
else if (assetBalance(e.sender,ThawToken) >= 1 ) then{false}
else true
case _ => false
}
TransferTransaction succeeded but ExchangeTransaction failed.
How do I change this code? please tell me.

I am guessing you are creating a smart account?
From what I see in the code, is that you used 2 times ThawToken, one time this results to true and another time to false. My guess is that you want one from the 2 be replaced by FreezeToken
Also in your example you have "ExchangeTransaction.", this dot doesn't seem needed here from what I can understand from your code.
Also according to the examples it seems that your whole second if structure should be included into {}. Example: https://github.com/wavesplatform/ride-examples/blob/bc8db2342f53fe1554a10dc5aaa211b1542a5ca1/smart-assets/HotPotatoToken.ride#L41
However I think this issue could be solved with a && statement, and as following the second if-then-else is not needed anymore.
What I propose is a check that does following:
Check if ThawToken not in wallet and if that is the case, check if freezetoken is in wallet.
If ThawToken is and FreezeToken is also => wallet free since ThawToken frees it.
If ThawToken is not and FreezeToken is 1 or more => wallet locked since only FreezeToken.
If ThawToken is not and FreezeToken is not => wallet free since no FreezeToken
if (assetBalance(e.sender,ThawToken) == 0 &&
assetBalance(e.sender,FreezeToken) >= 1 ) then{
false
}else{
true
}
Also to block all transactions and indeed freeze the wallet, you would need to filter on another type, for all types use: Transaction, be careful this also disables the option to change the script in case you locked your account.
To block transfer transaction use: TransferTransaction.
All types can be found here:
https://docs.wavesplatform.com/en/smart-contracts/ride-language/standard-library.html

Related

Implementing an increment(+1) filed security using security rules

I want to be able to protect my increment logic from being abused from the front end
my current frond end code
// Initial Payload
const surfacePayload: SurfacePayload = {
gender: selected.gender,
unit: unit.current,
// will increment the count in db by +1
count: increment(+1),
src: currentUser?.uid!,
};
await setDoc(doc(db, docRef, surfacePayload);
my security rules logic is like the following :
// requestData.count types
request.resource.data.count is number && // if the incoming count is a number
request.resource.data.count <= 12 && // this field cannot exceed 12
my big concern however is the this counter is set as a protector for creating docs in a related sub-collection, and it is really ease to keep sending 0 or 1 there is no real check whether the incoming data are only (increment +1)added to the current state of the count in doc... is there any other check that I can implement to make it rock solid, I tried to check again the current state using only (resource.data) but since this check is a part of a bigger check and it is chained so it has failed..
For the case where your document might not exists at first place, you can write separate rules for create and update as shown below:
allow create: if request.resource.data.count == 1; // default value 1
allow update: if request.resource.data.count == resource.data.count + 1;

Is there an Operation to block onComplete?

I am trying to learn reactive programming, so forgive me if I ask a silly question. I'm also open to advice on changing my design.
I am working in scala-swing to display the results of a simulator. With one setting, a chart is displayed as a histogram; with the other setting the chart is displayed as the cumulative sum. (I'm probably using the wrong word; in the first setting you might have bin1=2, bin2=5, bin3=3; in the second setting the first height is 2, the second is 2 + 5, the third is 2 + 5 + 3, etc.). The simulator can be slow, so I originally used a Future to compute it, and the set the data into the chart. I decided to try a reactive approach, so my requirements are: 1. I don't want to recreate the data when I change the display mode, and 2. I want to set the Observable once for the chart and have the chart listen to the same Observable permanently.
I got this to work when I started the chain with a PublishSubject and the Future set the data into the start of the chain. When the display mode changed, I created a new PublishSubject().map(newRenderingLogic).subscribe(theChartsObservable). I am now trying to do what looks like the "right way," but it's not working correctly. I've tried to simplify what I have done:
val textObservable: Subject[String] = PublishSubject()
textObservable.subscribe(text => {
println(s"Text: ${text}")
})
var textSubscription: Option[Subscription] = None
val start = Observable.from(Future {
"Base text"
}).cache
var i = 0
val button = new Button() {
text = "Click"
reactions += {
case event => {
i += 1
if (textSubscription.isDefined) {
textSubscription.get.unsubscribe()
}
textSubscription = Some(start.map(((j: Int) => { (base: String) => s"${base} ${j}" })(i)).subscribe(textObservable))
}
}
}
On start, an Observable is created and logic to print some text is added to it. Then, an Observable with the generated data is created and a cache is added so that the result is replayed if the next subscription comes in after its results are generated. Then, a button is created. Then on button clicks a middle observable is chained with unique logic (it's a function that creates a function to append the value of i into the string, run with the current value of i; I tried to make something that couldn't just be reused) that is supposed to change with each click. Then the first Observable is subscribed to it so that the results of the whole chain end up being printed.
In theory, the cache operation takes care of not regenerating the data, and this works once, but onComplete is called on textObservable and then it can't be used again. It works if I subscribe it like this:
textSubscription = Some(start.map(((j: Int) => { (base: String) => s"${base} ${j}" })(i)).subscribe(text => textObservable.onNext(text)))
because the call to onComplete is intercepted, but this looks wrong and I wanted to know if there was a more typical way to do this, or architect it. It makes me think that I don't understand how this is supposed to be done if there isn't an out-of-the-box operation to do this.
Thank you.
I'm not 100% sure if I got the essence of your question right, but: if you have an Observable that may complete and you want to turn it into an Observable that never completes, you can just concatenate it with Observable.never.
For example:
// will complete after emitting those three elements:
val completes = Observable.from(List(1, 2, 3))
// will emit those three elements, but will never complete:
val wontComplete = completes ++ Observable.never

Counter for re-opened cases - Salesforce

I'm traying to create a trigger that count each time a case is re-opened.
What I need is that changing the Status "Cerrado" to "Asignado" add +1
I've this trigger but it doesn't working at all.
Reperturas__c a type number and in the formula I've a 0.
"Cerrado" close the case.
Any solution guys?. Thanks!
trigger caseReOpen on Case (before update) {
for(Case c:trigger.new){
if(trigger.Oldmap.get(c.Id).Status=='Cerrado'){
c.Reaperturas__c ++;
}
}
}
Should work, weird. How's this?
trigger caseReOpen on Case (before update) {
for(Case c: trigger.new){
Case old = trigger.oldMap.get(c.Id);
if(c.Status == 'Asignado' && old.Status == 'Cerrado'){
++c.Reaperturas__c;
}
}
}
Compiled and saved ok? Can you put field history tracking on it, maybe you have something else that resets the counter? Are the actual picklist values like that or are these just labels? You need to use API values in Apex so if they're in English and you just have labels translated - your code won't work.
What if you base the check on !c.IsClosed && old.IsClosed ? It's a computed checkbox, can't be edited directly but it's bit more portable... If in future you add more statuses counting as closing (closed completed, closed cancelled etc?). https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_objects_case.htm

Erlang variable pattern matching

I have a service_echo function in a simple chat application which uses SockJS for implementing multi-user private chat. I created an ETS table for the list of online users. By storing SockJS session, I thought to send message to that Connection whenever I receive a message from a different Connection.
Here is my service_echo code.
service_echo(Conn, {recv, Data}, state) ->
Obj = mochijson2:decode(Data),
{struct, JsonData} = Obj,
Name = proplists:get_value(<<"name">>, JsonData),
A = ets:lookup(username,Name),
io:format("~p",[Conn]),
if
length(A) =:= 0 ->
ets:insert(username,{Name,Conn});
true ->
[{AA,BB}] = ets:lookup(username,Name),
BB:send(Data)
end,
io:format("hello");
Even though Conn and BB are same, still Conn:send(data) sends a valid data to the browser while BB:send(Data) does nothing and even does not show an error.
Since I'm a new to Erlang, please excuse me for any unintented mistakes.
First of all, let me advise you on never using length(A) =:= 0 for testing whether the list A is empty or not; if A a long list, counting its elements will cost you a lot, although the result will not actually be used. Use A =:= [] instead, simpler and better.
I don't understand why you're saying that Conn and BB are the same. This does not follow from the code that you have posted here. If Name is not in the table, you insert an entry {Name, Conn}. Otherwise, if Name exists in the table and is related to a single object BB, you assume that this BB is a module and you call the send function defined therein.
It could be that you're reading wrong the semantics of if --- if that's the case, don't let the true guard confuse you, this is how an if-then-else is written in Erlang. Maybe you wanted to have something like:
...
A = ets:lookup(username,Name),
if
A =:= [] ->
ets:insert(username,{Name,Conn})
end,
[{_,BB}] = ets:lookup(username,Name),
BB:send(Data)
...
or even better:
...
A = ets:lookup(T,Name),
if
A =:= [] ->
ets:insert(T,{Name,Conn}),
BB = Conn;
true ->
[{_,BB}] = A
end,
BB:send(Data)
...
On the other hand, it could be that I misunderstood what you're trying to do. If that's the case, please clarify.

mIRC bot - copy/paste lines in 2 channels

I’m a noob in mirc scripting, and I need some help.
there’s 2 irc channels. let’s call then #channel1 and #channel2;
There’s 2 bots. One is mine, let’s call him “mybot” (my bot is in both channels). The other bot is from a third person, let’s call him “otherBot”;
What I need is… let me make an example to better explain.
a) in #channel1 some user type:
[14:38:48] <#someuser> !user xpto
At this time, “mybot” is in both channels. he reads the command “!user*” and copy/paste it in #channel2, where the “otherBot” will recognize the command “!user*” and will paste some information about this command.
b) so, in #channel2 it will append something like:
[14:38:50] <# mybot > !user xpto
[14:38:52] <# otherBot > User name is xpto and he likes popatos.
Now I want that “mybot” reads the information provided by the “otherBot” and then paste it on #channel1
c) so, in #channel1:
[14:38:54] <# mybot > User name is xpto and he likes popatos.
So far I have the fowling code in my remote:
on *:TEXT:!user*:#channel1 {
/msg # channel2 $1-
}
on *:TEXT:User name*:#channel2 {
if $address($nick,2) == *!*#otherBot.users.gameea {
/msg # channel1 $1-
}
}
This works fine, but have a problem: if someone else ( not “mybot” ) type “!user kakaka” in #channel2, “mybot” will also copy/paste the information provided by the “otherBot” and then paste it on #channel1. And I only want that “mybot” copy/paste only the information that “mybot” ask to “otherBot”.
A very simple (but not a particularly nice) way of doing this could be to set a global variable when someone types !user in #channel1, and check whether or not this is set in the other part which is listening on #channel2. For example:
on *:TEXT:!user *:#channel1: {
set %repeatUser 1
msg channel2 $1-
}
on *:TEXT:User name*:#channel2: {
if ($address($nick,2) == *!*#otherBot.users.gameea && %repeatUser == 1) {
unset %repeatUser
msg #channel1 $1-
}
}
This isn't a perfect solution, since if the bot says something else between the time it takes for the script to send '!user' to the other channel and for the bot to respond, then it will print out that reply instead of the one for your request, but this is only relevant if #channel2 is ridiculously busy, otherbot is very laggy, or it just so happens that both your bot and someone else type !user on #channel2 within a fraction of a second of eachother.