This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
xbsktball10x's profile picture

banking system help

Started by xbsktball10x, 30 June 2013 - 08:49 PM
xbsktball10x #1
Posted 30 June 2013 - 10:49 PM
Im working on a banking system and i just finished up the code to add a new account. My problem is that if someone chooses a pin that is the same as someone else it will return that they also have the same account # as the person that has the same pin as them. heres the code

client
term.clear()
term.setCursorPos(1,1)
rednet.open("left")
local successfullogin = false
print("welcome to the bank")
print("1] login")
print("2] create new account")
print("3] shutdown")
write(">")
local firstoption = read()
  if firstoption == "1" then
	rednet.send(51,"login", true)
	write("account number: ")
	local accountnumb = read()
	write("pin: ")
	local pinnumb = read("*")
	rednet.send(51, accountnumb, true)
	local senderId, message, distance = rednet.receive(5)
	print(message)
	senderId, message, distance = rednet.receive()
	  if pinnumb == message then
		successfullogin=true
	  end
  elseif firstoption == "2" then
	rednet.send(51, "create new account", true)
	write("Enter a 4 digit pin for your account: ")
	local pin = read("*")
	rednet.send(51,pin, true)
	localaccountsenderId, accountnumber, accountdistance = rednet.receive()
	write("Your account number is: "..accountnumber)
  sleep(2)
  elseif firstoption == "3" then
	print("shutting down...")
	sleep(1)
	os.shutdown()
	else
  print("unsuccessful login")
  sleep(1)
  end
function menu()
print("1) withdraw")
print("2) deposit")
print("3) wire tranfer")
print("4) balance inquiry")
print("5) logoff")
write("Enter a number: ")
local option = read()
   if option == "1" then
   print("Enter go back to go back to the menu")
   write("How much? ")
   local amount = read()
	 if amount == "go back" then
	 menu()
	 else
	 rednet.send(51, "withdraw", true)
	 rednet.send(51, amount, true)
	 print("withdrawing funds...")
	 print("gold will be in withdraw area")
	 sleep(1)
	 menu()
   end
   elseif option == "2" then
	 write("Do you want to deposit? y/n")
	 local deposit = read()
	   if deposit == "y" then
		 rednet.send(51,"deposit", true)
		 else
		 menu()
	   end  
   elseif option =="3" then
	 --wire transfer code
   elseif option == "4" then
	 rednet.send(51, "balance", true)
	 local senderidbalance, balancemessage, distancebalance = rednet.receive()
	 print(balancemessage)
   elseif option == "5" then
	 shell.run("startup")
   else
	 print("That isn't a selection")
	 sleep(1)
   end
   sleep(2)
   menu()
  end
if successfulogin == true then
rednet.send(51, "validuser", true)
menu()
else
term.clear()
term.setCursorPos(1,1)
shell.run("startup")
end

server

print("This is the account server")
print("If you need to add an account then please do so through the admin computer in the server rooom")
print("Do not restart ever")
print("If you restart then all of the accounts and pins will be lost")
local pins = ""
local accounts= ""
local accountsId = ""
local realaccount = false
local accountaccess = false
rednet.open("top")
local account = {566001,566002,566003,566004}
local accountId = {46,52,53,54}
local pin = {}
local accountbalance = {0,0,0,0}
function accountlogin() -- sets it so you can acces account functions
local senderId, message, distance = rednet.receive()
if message == "login" then
senderId, message, distance = rednet.receive()
for i,v in ipairs(account) do
  if message == v then
	accountnumb = message
	realaccount = true
	rednet.send(senderId, "accessing your account...", true)
	pins = (pin[i])
	accountsId = (accountID[i])
	rednet.send(senderId, pins, true)
  else
	rednet.send(senderId, "That account doesnt exist", true)  
  end
break
end
senderId, message, distance = rednet.receive()
  if message == "validuser" then
	accountaccess = true
  end
elseif message == "withdraw" then
  senderId, message, distance = rednet.receive()
  rednet.send(accountsId, message, true)
elseif message== "deposit" then
  senderId, message, distance = rednet.receive()
  rednet.send(accountsId, message, true)
elseif message == "create new account" then
  senderId, message, distance = rednet.receive()
  table.insert(pin, message)
  for i,v in ipairs(pin) do
	if message == v then
	local newaccount = (account[i])
	rednet.send(senderId, newaccount, true)
  end
end  
elseif message == "wire transfer" then
--wire transfer code
else
end
end
while true do
accountlogin()
end
Edited on 03 July 2013 - 10:50 PM
Tiin57 #2
Posted 30 June 2013 - 11:09 PM
Use
, please.
xbsktball10x #3
Posted 30 June 2013 - 11:15 PM
got it there you go
xbsktball10x #4
Posted 03 July 2013 - 08:44 PM
bump.. really need some help with this
darkrising #5
Posted 04 July 2013 - 05:15 AM
Looking at your code it would probably be easy to have your accounts on a unique index, You could store the index as a number and increase it each time or use table.insert to insert the account array into the next available number.

That way each one of your accounts would be unique and will not clash.


account = {}

function newUser(pin, account)
  local u = {}
  u.pin = pin
  u.account = account
  return u
end

table.insert(account, newUser(2394994, 234))

-- in this example the new user will have an index of 1, so account[1].pin  would be there pin and
-- account[1].account would be their account number



Another thing, try using a function to save and load a database that way your accounts and balances wont be lost.

function saveDB(filename, theTable)
  theTable = textutils.serialize(theTable)
  local file = fs.open(filename, "w")
  file.write(theTable)
  file.close()
end
function loadDB(filename)
  file = fs.open(filename, "r")
  local content = file.readAll()
  file.close()
  return textutils.unserialize(content)
end

to use the second one just put tablename = loadDB(filename)
replacing tablename and filename respectively

(Might want to check my coding I quickly wrote it on a phone)
TeamDman #6
Posted 05 July 2013 - 09:14 AM
Interesting idea for a banking system. Mind if I try to make a script like this?
xbsktball10x #7
Posted 06 July 2013 - 12:25 AM
Thank you so much guys this has been a frustrating process. and Team Dman go right ahead and write all you want im still working the bugs out with this one but if you come up with a script like this then please share it with me :D/>.
xbsktball10x #8
Posted 06 July 2013 - 03:03 AM
Looking at your code it would probably be easy to have your accounts on a unique index, You could store the index as a number and increase it each time or use table.insert to insert the account array into the next available number.

That way each one of your accounts would be unique and will not clash.


account = {}

function newUser(pin, account)
  local u = {}
  u.pin = pin
  u.account = account
  return u
end

table.insert(account, newUser(2394994, 234))

-- in this example the new user will have an index of 1, so account[1].pin  would be there pin and
-- account[1].account would be their account number


a little new to cc so how exactly would this work?
darkrising #9
Posted 06 July 2013 - 09:14 AM
-snip-
a little new to cc so how exactly would this work?

Basically the function returns a new table with the data you have passed it, and then it is inserted into the next available index in the table.

For example:


function newUser(pin, account)
  local u = {}
  u.pin = pin
  u.account = account
  return u
end
aTable = newUser(123, 456)

--aTable.pin would equal 123
--aTable.account would equal 456

--So when we insert it into our main table
mainTable = {}

table.insert(mainTable, aTable)
--mainTable[1].pin would equal 123
--mainTable[1].account would equal 456

-- if we then did this again
table.insert(mainTable, newUser(111, 222)

--mainTable[2].pin would equal 111
--mainTable[2].account would equal 222
TeamDman #10
Posted 06 July 2013 - 05:27 PM
Just trying to think of a way to make it 'impossible' to hack in…
albrat #11
Posted 06 July 2013 - 07:53 PM
basically I have this sort of system setup for my Server program for user logins. (except I do not allow account creation from users)
I wrote a system to input data into a table and read it back again accurately from a file / reference it against inputs…


local usrbase = {}
-- *************************
-- **  Functions listing  **
-- *************************

function newUser(user, pass) -- insert data
  local u = {}
  u[1] = user
  u[2] = pass
  return u
end

-- Save DataBase
function saveDB(filename, theTable)  -- call with saveDB (file, tablename)
  theTable = textutils.serialize(theTable)
  local file = fs.open(filename, "w")
  file.write(textutils.serialize(theTable))
  file.close()
end
-- append DataBase
function appDB(filename, theTable)  -- call with saveDB (file, tablename)
  theTable = textutils.serialize(theTable)
  local file = fs.open(filename, "a")
  file.write(textutils.serialize(theTable))
  file.close()
end
-- Load DataBase
function loadDB(filename)  --  Call with  tablename = loadDB(filename)
  file = fs.open(filename, "r")
  local content = file.readAll()
  file.close()
  return textutils.unserialize(content)
end

--[[ insert to tables. This is a one time run to insert my passwords
-- This is commented out in my actual program just add a - before the brackets to open the statement
print()
table.insert(usrbase, newUser("use1","pass3"))
print()
table.insert(usrbase, newUser("guest","pass1"))
table.insert(usrbase, newUser("user","pass2"))
saveDB("userbase", usrbase)
sleep(2)
error("data saved") -- end program so you can comment out the DB save
--]]
---[[ LoadDB  This loads the database into our system
usrbase = textutils.unserialize(loadDB("userbase"))
--]]

  for i= 1, #usrbase do
   if pas == usrbase[i][2] then  -- If the message matches a user password set our response to true
    valid = true 
    password = usrbase[i][2]
    break
   else
    valid = false  -- if no password matches...
   end
  end
  if valid then -- if we are valid respond the pass
   rednet.send(senderId, "valid", true)
  else
   rednet.send(senderId, "Not Valid", true)  -- otherwise respond Not Valid
  end
  end

I think I got all my code right in there…

basically this writes a table inside a table. It handles saving to a file and even works for retreiving the information back from the file. (which was difficult to achieve with some code setups)

to add more "fields" to your table just add another varible to the newUser() function and add a u[3] = {varible3name} into the code.
xbsktball10x #12
Posted 08 July 2013 - 03:26 AM
Dman what do you need to make impossible to hack into? the accounts? or like the gold storage area?
TeamDman #13
Posted 08 July 2013 - 07:51 PM
If your sending signals between 2 computers for the banking what is to prevent someone from just copying a signal and sending that?
bwhodle #14
Posted 09 July 2013 - 05:50 PM
If your sending signals between 2 computers for the banking what is to prevent someone from just copying a signal and sending that?
Off topic but…
One thing you could do is use wired modems and then the hacker would actually have to literally walk all the way to the computer to get into the signal sending thing.
But since wired modems also have drawbacks you could use the Rednet api instead of the modem api, in which case you can check to see which computer is sending a message (its id) and see if it's a trusted id.
One md
11. 1. 3
darkrising #15
Posted 10 July 2013 - 07:25 AM
If your sending signals between 2 computers for the banking what is to prevent someone from just copying a signal and sending that?
Off topic but…
One thing you could do is use wired modems and then the hacker would actually have to literally walk all the way to the computer to get into the signal sending thing.
But since wired modems also have drawbacks you could use the Rednet api instead of the modem api, in which case you can check to see which computer is sending a message (its id) and see if it's a trusted id.
One md
11. 1. 3

If you need to use rednet, just encrypt traffic between the computers, in my api I made encryption functions just for that purpose.
Cutecurtain #16
Posted 10 July 2013 - 09:40 AM
I really like the idea with bank system :)/> Never seen it before,
xbsktball10x #17
Posted 10 July 2013 - 11:21 PM
Yeah Dman you would have to make a table of all the trusted computer Id's and make it run through the table and if it returns that its in there then allow access
AgentE382 #18
Posted 10 July 2013 - 11:57 PM
Yeah Dman you would have to make a table of all the trusted computer Id's and make it run through the table and if it returns that its in there then allow access

Ahem… Why don't you just encrypt the traffic with my RC4 API ;)/>